You are here

function fivestar_field_settings in Fivestar 6

Same name and namespace in other branches
  1. 5 fivestar_field.inc \fivestar_field_settings()
  2. 6.2 includes/fivestar.field.inc \fivestar_field_settings()

Implementation of hook_field_settings().

File

./fivestar_field.inc, line 23
Provides CCK integration for fivestar module

Code

function fivestar_field_settings($op, $field) {
  switch ($op) {
    case 'form':

      // Multiple is not supported with Fivestar.
      $form['multiple'] = array(
        '#type' => 'value',
        '#value' => 0,
      );
      $form['stars'] = array(
        '#type' => 'select',
        '#title' => $field['widget']['type'] == 'stars' ? t('Number of Stars') : t('Number of Options'),
        '#options' => drupal_map_assoc(range(1, 10)),
        '#default_value' => $field['stars'] ? $field['stars'] : 5,
      );
      $dynamic_options = array();
      if (module_exists('nodecomment')) {
        $dynamic_options['comment_target_nid'] = t('Node Comment Parent');
      }
      if (module_exists('nodereference')) {
        $fields = content_fields();
        foreach ($fields as $field_name => $content_field) {

          // Make sure that this field exists for this type.
          if ($content_field['type'] == 'nodereference' && ($content_field = content_fields($field_name, $field['type_name']))) {
            $dynamic_options[$content_field['field_name']] = t('Node reference: @field', array(
              '@field' => $content_field['field_name'],
            ));
          }
        }
      }
      if (empty($dynamic_options)) {
        drupal_set_message(t('No potential target fields are available for the %type type. Create a node reference field in this node type to make it easier to assign a vote to a node.', array(
          '%type' => node_get_types('name', $field['type_name']),
        )), 'warning');
      }
      $dynamic_options = array(
        '' => '<' . t('none') . '>',
      ) + $dynamic_options;
      $form['dynamic_target'] = array(
        '#title' => t('Voting target'),
        '#type' => 'select',
        '#default_value' => $field['dynamic_target'],
        '#options' => $dynamic_options,
        '#description' => t('The voting target will make the value of this field cast a vote on another node. Use node reference fields (part of CCK core) or <a href="http://drupal.org/project/nodecomment">Node Comments</a> module to create advanced reviews. More information available on the <a href="http://drupal.org/handbook/modules/fivestar">Fivestar handbook page</a>.'),
      );
      if (user_access('use PHP for fivestar target')) {
        $form['php_target'] = array(
          '#type' => 'fieldset',
          '#title' => t('Voting target PHP code'),
          '#collapsible' => TRUE,
          '#collapsed' => empty($field['php_target']),
        );
        $form['php_target']['php_target'] = array(
          '#title' => t('Code'),
          '#type' => 'textarea',
          '#default_value' => $field['php_target'] ? $field['php_target'] : '',
          '#description' => t('Advanced usage only: PHP code that returns a target node ID. Should not include &lt;?php ?&gt; delimiters. If this field is filled out, the value returned by this code will override any value specified above. Note that executing incorrect PHP-code can break your Drupal site.'),
        );
      }
      else {
        $form['php_target'] = array(
          '#type' => 'value',
          '#value' => $field['php_target'] ? $field['php_target'] : '',
        );
      }
      $form['axis'] = array(
        '#type' => 'textfield',
        '#title' => 'Voting Axis',
        '#description' => t('The axis this rating will affect. Enter a property on which that this rating will affect, such as <em>quality</em>, <em>satisfaction</em>, <em>overall</em>, etc. If no axis is entered, the default axis <em>vote</em> will be used. Warning: changing this value will not update existing votes to the new axis.'),
        '#default_value' => $field['axis'],
      );
      return $form;
    case 'save':
      return array(
        'stars',
        'dynamic_target',
        'php_target',
        'axis',
      );
    case 'database columns':
      return array(
        'rating' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
          'sortable' => TRUE,
        ),
        'target' => array(
          'type' => 'int',
          'unsigned' => TRUE,
          'not null' => FALSE,
        ),
      );
  }
}