SHARE
TWEET

Untitled

a guest Jul 17th, 2017 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /**
  3.  * Implement hook_permission
  4.  * Defines a new permission that can be selected per role at the page
  5.  * admin/people/permissions
  6.  */
  7. function MY_MODULE_permission() {
  8.   return array(
  9.     'skip required comment approval' => array(
  10.       'title' => t('Skip "Require comment approval"'),
  11.       'description' => t('For nodes with "Require comment approval" set, this user's comments skip moderation'),
  12.     ),
  13.   );
  14. }
  15.  
  16.  
  17.  
  18. /**
  19.  * Implement hook_form_alter for BASE_FORM_ID node_form
  20.  * This modifies all node add/edit forms
  21.  */
  22. function MY_MODULE_form_node_form_alter(&$form, &$form_state) {
  23.   // Move 'Require comment approval' field into the comment settings fieldset
  24.   if (isset($form['field_require_comment_approval'])) {
  25.     $form['comment_settings']['field_require_comment_approval'] = $form['field_require_comment_approval'];
  26.     unset($form['field_require_comment_approval']);
  27.   }
  28. }
  29.  
  30.  
  31.  
  32. /**
  33.  * Implement hook_comment_presave
  34.  * This hook runs before a comment is saved to the database, allowing us to
  35.  * modify the comment as it is being saved
  36.  */
  37. function MY_MODULE_comment_presave($comment) {
  38.   // If the parent node has the field "Require comment approval" checkbox set,
  39.   // make the new comment's status set to "unpublished"
  40.   if (empty($comment->cid)) {
  41.     $node = node_load($comment->nid);
  42.     if ($node && ($items = field_get_items('node', $node, 'field_require_comment_approval'))) {
  43.       $item = !empty($items) ? reset($items) : array('value' => FALSE);
  44.       if ($item['value']) {
  45.         $account = user_load($comment->uid);
  46.         // Comments for users with the 'skip required comment approval' permission
  47.         // skip this step
  48.         if ($account && !user_access('skip required comment approval', $account)) {
  49.           $comment->status = COMMENT_NOT_PUBLISHED;
  50.         }
  51.       }
  52.     }
  53.   }
  54. }
RAW Paste Data
Top