You are here

function fivestar_form_alter in Fivestar 5

Same name and namespace in other branches
  1. 6 fivestar.module \fivestar_form_alter()

Implementation of hook_form_alter().

Adds fivestar enaable and position to the node-type configuration form.

File

./fivestar.module, line 98
A simple n-star voting widget, usable in other forms.

Code

function fivestar_form_alter($form_id, &$form) {
  if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {

    // Goofy hack to get the buttons at the end of the array.
    $form['workflow']['#weight'] = isset($form['workflow']['#weight']) ? $form['workflow']['#weight'] + 1 : 1;
    $form['submit']['#weight'] = isset($form['submit']['#weight']) ? $form['submit']['#weight'] + 1 : 1;
    $form['delete']['#weight'] = isset($form['delete']['#weight']) ? $form['delete']['#weight'] + 1 : 1;
    $form['fivestar'] = array(
      '#type' => 'fieldset',
      '#title' => t('Fivestar ratings'),
      '#collapsible' => TRUE,
      '#collapsed' => !variable_get('fivestar_' . $form['#node_type']->type, 0),
      '#description' => t('To rate this content, enable Fivestar rating below. These settings will be used for both comments (if available) and direct rating.'),
      '#theme' => 'fivestar_node_type_form',
      '#attributes' => array(
        'id' => 'fivestar-node-type-form',
      ),
    );
    $form['fivestar']['fivestar'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable Fivestar rating'),
      '#default_value' => variable_get('fivestar_' . $form['#node_type']->type, 0),
      '#return_value' => 1,
      '#weight' => -5,
    );
    $form['fivestar']['fivestar_stars'] = array(
      '#type' => 'select',
      '#title' => t('Number of stars'),
      '#options' => drupal_map_assoc(range(1, 10)),
      '#default_value' => variable_get('fivestar_stars_' . $form['#node_type']->type, 5),
      '#weight' => -4,
    );
    $form['fivestar']['labels'] = array(
      '#type' => 'fieldset',
      '#title' => t('Star Labels'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#description' => t('These star labels appear as the link title when javascript is enabled as well as the select list options when javascript is disabled.'),
    );
    $form['fivestar']['labels']['fivestar_labels_enable'] = array(
      '#type' => 'checkbox',
      '#title' => t('Display labels on mouse over'),
      '#default_value' => variable_get('fivestar_labels_enable_' . $form['#node_type']->type, 1),
      '#return_value' => 1,
      '#weight' => -5,
      '#description' => t('When enabled, the star labels will dynamically appear underneath the stars as the user hovers over each star to provide a more descriptive qualitative meaning for each star value.'),
    );

    // Create the Mouseover text forms for each of the rating options
    // This form depends on the number of stars, and these extra textfields will be hidden with javascript
    $star_count = variable_get('fivestar_stars_' . $form['#node_type']->type, 5);
    $labels = variable_get('fivestar_labels_' . $form['#node_type']->type, array());
    for ($n = 0; $n <= 10; $n++) {
      if ($star_count == 5 && $n <= 5) {

        // If the default 5 stars are chosen, then use these five default label values.
        $default_labels = array(
          t('Cancel rating'),
          t('Poor'),
          t('Okay'),
          t('Good'),
          t('Great'),
          t('Awesome'),
        );
      }
      elseif ($n == 0) {
        $default_labels[$n] = t('Cancel rating');
      }
      else {
        $default_labels[$n] = t('Give it @star/@count');
      }
      $form['fivestar']['labels']['fivestar_label_' . $n] = array(
        '#type' => 'textfield',
        '#title' => $n > 0 ? t('Star @star label', array(
          '@star' => $n,
        )) : t('Cancel label'),
        '#default_value' => isset($labels[$n]) ? $labels[$n] : $default_labels[$n],
        '#prefix' => '<div id="fivestar-label-' . $n . '" class="fivestar-label">',
        '#suffix' => '</div>',
        '#size' => 30,
      );
    }
    $form['fivestar']['direct'] = array(
      '#type' => 'fieldset',
      '#title' => t('Direct rating widget'),
      '#collapsible' => FALSE,
      '#description' => t('These settings allow you to display a rating widget to your users while they are viewing content of this type. Rating will immediately register a vote for that piece of content.'),
      '#weight' => 2,
    );
    $form['fivestar']['direct']['fivestar_style'] = array(
      '#type' => 'select',
      '#title' => t('Star display style'),
      '#default_value' => variable_get('fivestar_style_' . $form['#node_type']->type, 'average'),
      '#options' => array(
        'average' => t('Display average vote value'),
        'user' => t('Display user vote value'),
        'smart' => t('User vote if available, average otherwise'),
        'dual' => t('Both user and average vote'),
      ),
    );
    $form['fivestar']['direct']['fivestar_text'] = array(
      '#type' => 'select',
      '#title' => t('Text display style'),
      '#default_value' => variable_get('fivestar_text_' . $form['#node_type']->type, 'dual'),
      '#options' => array(
        'none' => t('Display no text beneath stars'),
        'average' => t('Current average in text'),
        'user' => t('User current vote in text'),
        'smart' => t('User vote if available, average otherwise'),
        'dual' => t('Both user and average vote'),
      ),
    );
    $form['fivestar']['direct']['fivestar_title'] = array(
      '#type' => 'checkbox',
      '#title' => t('Show widget title'),
      '#default_value' => variable_get('fivestar_title_' . $form['#node_type']->type, 1),
      '#return_value' => 1,
    );
    $form['fivestar']['direct']['fivestar_feedback'] = array(
      '#type' => 'checkbox',
      '#title' => t('Enable confirmations to inform a vote was saved or deleted.'),
      '#default_value' => variable_get('fivestar_feedback_' . $form['#node_type']->type, 1),
      '#return_value' => 1,
    );
    $form['fivestar']['direct']['fivestar_unvote'] = array(
      '#type' => 'checkbox',
      '#title' => t('Allow users to undo their votes'),
      '#default_value' => variable_get('fivestar_unvote_' . $form['#node_type']->type, 0),
      '#return_value' => 1,
    );
    $form['fivestar']['direct']['fivestar_position_teaser'] = array(
      '#type' => 'select',
      '#title' => t('Teaser display'),
      '#default_value' => variable_get('fivestar_position_teaser_' . $form['#node_type']->type, 'hidden'),
      '#options' => array(
        'above' => t('Clickable widget above teaser'),
        'below' => t('Clickable widget below teaser'),
        'above_static' => t('Static display above teaser'),
        'below_static' => t('Static display below teaser'),
        'link' => t('Teaser link to full node widget'),
        'hidden' => '<' . t('hidden') . '>',
      ),
    );
    $form['fivestar']['direct']['fivestar_position'] = array(
      '#type' => 'select',
      '#title' => t('Full node display'),
      '#default_value' => variable_get('fivestar_position_' . $form['#node_type']->type, 'below'),
      '#options' => array(
        'above' => t('Clickable widget above node body'),
        'below' => t('Clickable widget below node body'),
        'above_static' => t('Static display above node body'),
        'below_static' => t('Static display below node body'),
        'hidden' => '<' . t('hidden') . '>',
      ),
    );
    $form['fivestar']['direct']['fivestar_direct_preview'] = array(
      '#type' => 'item',
      '#title' => t('Direct rating widget preview'),
      '#value' => theme('fivestar_preview', $form['fivestar']['direct']['fivestar_style']['#default_value'], $form['fivestar']['direct']['fivestar_text']['#default_value'], $form['fivestar']['fivestar_stars']['#default_value'], $form['fivestar']['direct']['fivestar_unvote']['#default_value'], $form['fivestar']['direct']['fivestar_title']['#default_value'] ? NULL : FALSE, $form['fivestar']['labels']['fivestar_labels_enable']['#default_value'], variable_get('fivestar_labels_' . $form['#node_type']->type, array())),
    );
    if (!$form['fivestar']['fivestar']['#default_value']) {
      $form['fivestar']['direct']['fivestar_direct_preview']['#value'] = theme('fivestar_preview_wrapper', '');
    }
    else {
      $form['fivestar']['direct']['fivestar_direct_preview']['#value'] = theme('fivestar_preview_wrapper', $form['fivestar']['direct']['fivestar_direct_preview']['#value']);
    }
    $form['#submit']['fivestar_node_type_form_submit'] = array();
  }
}