You are here

function fivestar_comment_form_alter in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar_comment.module \fivestar_comment_form_alter()
  2. 6.2 fivestar_comment.module \fivestar_comment_form_alter()

Form alter specification for comments.

File

./fivestar_comment.module, line 21

Code

function fivestar_comment_form_alter(&$form, &$form_state, $form_id) {

  // Comment settings.
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
    $form['fivestar']['comment'] = array(
      '#type' => 'fieldset',
      '#title' => t('Comment widget'),
      '#description' => t('Enabling Fivestar for comments will display a rating widget when a user posts a comment. The rating of the comment will affect its parent content.'),
      '#weight' => 1,
    );
    $form['fivestar']['comment']['fivestar_comment'] = array(
      '#type' => 'radios',
      '#title' => t('Fivestar comment settings'),
      '#options' => array(
        FIVESTAR_COMMENT_DISABLED => t('Disabled'),
        FIVESTAR_COMMENT_OPTIONAL => t('Optional rating'),
        FIVESTAR_COMMENT_REQUIRED => t('Required rating'),
      ),
      '#default_value' => variable_get('fivestar_comment_' . $form['#node_type']->type, FIVESTAR_COMMENT_DISABLED),
    );
    $form['fivestar']['comment']['fivestar_comment_preview'] = array(
      '#type' => 'item',
      '#title' => t('Comment widget preview'),
      '#value' => theme('fivestar_preview', 'compact', 'none', $form['fivestar']['fivestar_stars']['#default_value'], $form['fivestar']['comment']['fivestar_comment']['#default_value'] == 1 ? 1 : 0),
    );
    if (!$form['fivestar']['fivestar']['#default_value'] || !$form['fivestar']['comment']['fivestar_comment']['#default_value']) {
      $form['fivestar']['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', '', 'comment');
    }
    else {
      $form['fivestar']['comment']['fivestar_comment_preview']['#value'] = theme('fivestar_preview_wrapper', $form['fivestar']['comment']['fivestar_comment_preview']['#value'], 'comment');
    }
  }

  // Comment form. Do not allow ratings inside of threads.
  if ($form_id == 'comment_form' && empty($form['pid']['#value']) && user_access('rate content')) {
    $node = node_load($form['nid']['#value']);
    if (variable_get('fivestar_comment_' . $node->type, FIVESTAR_COMMENT_DISABLED)) {

      // Splice in the fivestar right before the body.
      $new_form = array();
      foreach ($form as $key => $element) {
        if ($key == 'comment_filter') {
          if ($form['cid']['#value']) {
            $current_rating = fivestar_comment_load($form['cid']['#value'], $form['nid']['#value']);
            $default_value = $current_rating['value'];
          }
          else {
            $votes = fivestar_get_votes('node', $form['nid']['#value']);
            $default_value = isset($votes['user']['value']) ? $votes['user']['value'] : 0;
          }
          $new_form['fivestar_rating'] = array(
            '#type' => 'fivestar',
            '#title' => t('Rating'),
            '#stars' => variable_get('fivestar_stars_' . $node->type, 5),
            '#allow_clear' => variable_get('fivestar_comment_' . $node->type, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_OPTIONAL ? 1 : 0,
            '#content_id' => $node->nid,
            '#required' => variable_get('fivestar_comment_' . $node->type, FIVESTAR_COMMENT_DISABLED) == FIVESTAR_COMMENT_REQUIRED ? 1 : 0,
            '#default_value' => $default_value,
            '#labels' => variable_get('fivestar_labels_' . $node->type, array()),
          );
        }
        $new_form[$key] = $element;
      }
      if ($new_form['fivestar_rating']) {
        $form = $new_form;
      }
    }
  }
}