You are here

function fivestar_custom_widget in Fivestar 7.2

Same name and namespace in other branches
  1. 5 fivestar.module \fivestar_custom_widget()
  2. 6.2 fivestar.module \fivestar_custom_widget()
  3. 6 fivestar.module \fivestar_custom_widget()

Form builder; Build a custom Fivestar rating widget with arbitrary settings.

This function is usually not called directly, instead call drupal_get_form('fivestar_custom_widget', $values, $settings) when wanting to display a widget.

Parameters

$form_state: The form state provided by Form API.

$values: An array of current vote values from 0 to 100, with the following array keys:

  • user: The user's current vote.
  • average: The average vote value.
  • count: The total number of votes so far on this content.

$settings: An array of settings that configure the properties of the rating widget. Available keys for the settings include:

  • content_type: The type of content which will be voted upon.
  • content_id: The content ID which will be voted upon.
  • stars: The number of stars to display in this widget, from 2 to 10. Defaults to 5.
  • autosubmit: Whether the form should be submitted upon star selection. Defaults to TRUE.
  • allow_clear: Whether or not to show the "Clear current vote" icon when showing the widget. Defaults to FALSE.
  • required: Whether this field is required before the form can be submitted. Defaults to FALSE.
  • tag: The VotingAPI tag that will be registered by this widget. Defaults to "vote".
2 string references to 'fivestar_custom_widget'
fivestar_field_formatter_view in includes/fivestar.field.inc
Implements hook_field_formatter_view().
theme_fivestar_preview in includes/fivestar.theme.inc

File

./fivestar.module, line 470

Code

function fivestar_custom_widget($form, &$form_state, $values, $settings) {
  $form = array(
    '#attributes' => array(
      'class' => array(
        'fivestar-widget',
      ),
    ),
  );
  $form['#submit'][] = 'fivestar_form_submit';
  $form_state['settings'] = $settings;

  // Define default settings.
  $default_settings = array(
    'allow_clear' => FALSE,
    // Taken from installation file.
    'allow_ownvote' => TRUE,
    'allow_revote' => TRUE,
    'autosubmit' => TRUE,
    'description' => '',
    'required' => FALSE,
    'stars' => 5,
    'tag' => 'vote',
    'widget' => array(
      'name' => 'default',
      'css' => 'default',
    ),
  );

  // Merge default settings.
  $settings = $settings + $default_settings;
  $form['vote'] = array(
    '#type' => 'fivestar',
    '#stars' => $settings['stars'],
    '#auto_submit' => $settings['autosubmit'],
    '#allow_clear' => $settings['allow_clear'],
    '#allow_revote' => $settings['allow_revote'],
    '#allow_ownvote' => $settings['allow_ownvote'],
    '#required' => $settings['required'],
    '#widget' => $settings['widget'],
    '#values' => $values,
    '#settings' => $settings,
    '#description' => $settings['description'],
  );
  $form['fivestar_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Rate'),
    '#attributes' => array(
      'class' => array(
        'fivestar-submit',
      ),
    ),
  );
  return $form;
}