Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Implement hook_permission
- * Defines a new permission that can be selected per role at the page
- * admin/people/permissions
- */
- function MY_MODULE_permission() {
- return array(
- 'skip required comment approval' => array(
- 'title' => t('Skip "Require comment approval"'),
- 'description' => t('For nodes with "Require comment approval" set, this user's comments skip moderation'),
- ),
- );
- }
- /**
- * Implement hook_form_alter for BASE_FORM_ID node_form
- * This modifies all node add/edit forms
- */
- function MY_MODULE_form_node_form_alter(&$form, &$form_state) {
- // Move 'Require comment approval' field into the comment settings fieldset
- if (isset($form['field_require_comment_approval'])) {
- $form['comment_settings']['field_require_comment_approval'] = $form['field_require_comment_approval'];
- unset($form['field_require_comment_approval']);
- }
- }
- /**
- * Implement hook_comment_presave
- * This hook runs before a comment is saved to the database, allowing us to
- * modify the comment as it is being saved
- */
- function MY_MODULE_comment_presave($comment) {
- // If the parent node has the field "Require comment approval" checkbox set,
- // make the new comment's status set to "unpublished"
- if (empty($comment->cid)) {
- $node = node_load($comment->nid);
- if ($node && ($items = field_get_items('node', $node, 'field_require_comment_approval'))) {
- $item = !empty($items) ? reset($items) : array('value' => FALSE);
- if ($item['value']) {
- $account = user_load($comment->uid);
- // Comments for users with the 'skip required comment approval' permission
- // skip this step
- if ($account && !user_access('skip required comment approval', $account)) {
- $comment->status = COMMENT_NOT_PUBLISHED;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement