3

I have a content type which I don't want a user to be able to edit once it has been created. but I want them to be able to change 1 value and the way I want them to do that is through a selection box on a comment. For example, on the content type there is a priority field. When a user leaves a comment I have a selection box added to the comment with the same options.

How can I get that option box to sync with the option in the node itself?

Thanks

Kieran

0

Assuming your content type is called your_content_type and your module is called yourmodulename, you can use the following code:

It will set some state when a none-priviledged user posts a comment, and show a state selection UI for priviledged users. The state will be saved to the CCK field_erledigt_warten in the node in either case.

<?php
/**
 * Implementation of hook_comment().
 * Fill a CCK field  in Drupal 6
 */
function yourmodulename_comment(&$comment, $op) {
  global $user;

  if (is_array($comment)) {
    $node = node_load($comment['nid']);
  }
  else {
    $node = node_load($comment->nid);
  }

  if ($node->type == 'your_content_type') {
    switch ($op) {
      case 'insert':
      case 'update':
        if (user_access('edit field_erledigt_warten')) {
          // QD-Mitarbeiter
          if (isset($comment['field_erledigt_warten']) && $comment['field_erledigt_warten']) {
            if ($node->field_erledigt_warten[0]['value'] != $comment['field_erledigt_warten']) {
              $node->field_erledigt_warten[0]['value'] = $comment['field_erledigt_warten'];
              drupal_set_message(t('Status wurde auf %v ge&auml;ndert.', array('%v' => $comment['field_erledigt_warten'])));
              node_save($node);
            }
          }
        } else {
          // Kundenaccount
          $node->field_erledigt_warten[0]['value'] = 'neu';
          node_save($node);
        }
        break;
    }
  }
}

/**
    Implementation of hook_form_alter
 * Add a input box to the comment form
 */
function yourmodulename_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'comment_form') {
    if (is_array($form) && isset($form['nid']) && is_array($form['nid'])) {
      $node = node_load($form['nid']['#value']);
    }
    if (isset($node) && is_object($node) && isset($node->type) &&
      $node->type == 'your_content_type' && user_access('edit field_erledigt_warten')) {
      $states = array_flip(array('some', 'values', 'come', 'here'));
      foreach($states as $text=>$index) $states[$text] = $text;
      $status = $node->field_erledigt_warten[0]['value'];
      $form['field_erledigt_warten'] = array(
        '#type' => 'select',
        '#title' => 'neuer Status dieser Forumseite',
        '#default_value' => $status,
        '#options' => $states,
      );
    } 
  }
}
2
  • Is this the same for Drupal 7?
    – Wheelz
    Dec 27 '11 at 23:26
  • Very likely not. You'd have to go throu one of the many 'what has changed in D7' module update guides. So my code above can only serve as a starting point.
    – Gogowitsch
    Jan 7 '12 at 20:46
0

This was what the Comment Driven module was supposed to do, but they seem to have hit a wall in terms of a D7 version.

http://drupal.org/project/comment_driven

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.