You are here

function calendar_plugin_row::options_form in Calendar 7.3

Provide a form for setting options.

Overrides views_plugin_row_fields::options_form

File

includes/calendar_plugin_row.inc, line 62
Contains the Calendar row style plugin.

Class

calendar_plugin_row
Plugin which creates a view on the resulting object and formats it as a Calendar node.

Code

function options_form(&$form, &$form_state) {
  parent::options_form($form, $form_state);
  $form['markup']['#markup'] = t("The calendar row plugin will format view results as calendar items. Make sure this display has a 'Calendar' format and uses a 'Date' contextual filter, or this plugin will not work correctly.");
  $form['calendar_date_link'] = array(
    '#title' => t('Add new date link'),
    '#type' => 'select',
    '#default_value' => $this->options['calendar_date_link'],
    '#options' => array(
      '' => t('No link'),
    ) + node_type_get_names(),
    '#description' => t('Display a link to add a new date of the specified content type. Displayed only to users with appropriate permissions.'),
  );
  $form['colors'] = array(
    '#type' => 'fieldset',
    '#title' => t('Legend Colors'),
    '#description' => t('Set a hex color value (like #ffffff) to use in the calendar legend for each content type. Items with empty values will have no stripe in the calendar and will not be added to the legend.'),
  );
  $options = array(
    '' => t('None'),
  );
  if ($this->view->base_table == 'node') {
    $options['type'] = t('Based on Content Type');
  }
  if (module_exists('taxonomy')) {
    $options['taxonomy'] = t('Based on Taxonomy');
  }
  if (module_exists('og')) {
    $options['group'] = t('Based on Organic Group');
  }

  // If none of the options but the None option is available, stop here.
  if (count($options) == 1) {
    return;
  }
  $form['colors']['legend'] = array(
    '#title' => t('Stripes'),
    '#description' => t('Add stripes to calendar items.'),
    '#type' => 'select',
    '#options' => $options,
    '#default_value' => $this->options['colors']['legend'],
  );
  if ($this->view->base_table == 'node') {
    $colors = $this->options['colors']['calendar_colors_type'];
    $type_names = node_type_get_names();
    foreach ($type_names as $key => $name) {
      $form['colors']['calendar_colors_type'][$key] = array(
        '#title' => check_plain($name),
        '#default_value' => isset($colors[$key]) ? $colors[$key] : CALENDAR_EMPTY_STRIPE,
        '#dependency' => array(
          'edit-row-options-colors-legend' => array(
            'type',
          ),
        ),
        '#type' => 'textfield',
        '#size' => 7,
        '#maxlength' => 7,
        '#element_validate' => array(
          'calendar_validate_hex_color',
        ),
        '#prefix' => '<div class="calendar-colorpicker-wrapper">',
        '#suffix' => '<div class="calendar-colorpicker"></div></div>',
        '#attributes' => array(
          'class' => array(
            'edit-calendar-colorpicker',
          ),
        ),
        '#attached' => array(
          // Add Farbtastic color picker.
          'library' => array(
            array(
              'system',
              'farbtastic',
            ),
          ),
          // Add javascript to trigger the colorpicker.
          'js' => array(
            drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js',
          ),
        ),
      );
    }
  }
  if (module_exists('taxonomy')) {

    // Get the display's field names of taxonomy fields.
    $vocab_field_options = array();
    $fields = $this->display->handler
      ->get_option('fields');
    foreach ($fields as $name => $field_info) {
      $field = field_info_field($name);

      // Select the proper field type.
      if (!empty($field['type']) && $field['type'] == 'taxonomy_term_reference') {
        if (isset($field_info['label']) && !empty($field_info['label'])) {
          $vocab_field_options[$name] = $field_info['label'];
        }
        else {
          $bundles = field_info_bundles('taxonomy_term');
          $vocab = $field['settings']['allowed_values'][0]['vocabulary'];
          $vocab_field_options[$name] = $bundles[$vocab]['label'];
        }
      }
    }
    $form['colors']['taxonomy_field'] = array(
      '#title' => t('Term field'),
      '#type' => !empty($vocab_field_options) ? 'select' : 'hidden',
      '#default_value' => $this->options['colors']['taxonomy_field'],
      '#description' => t("Select the taxonomy term field to use when setting stripe colors. This works best for vocabularies with only a limited number of possible terms."),
      '#options' => $vocab_field_options,
      '#dependency' => array(
        'edit-row-options-colors-legend' => array(
          'taxonomy',
        ),
      ),
    );
    if (empty($vocab_field_options)) {
      $form['colors']['taxonomy_field']['#options'] = array(
        '' => '',
      );
      $form['colors']['taxonomy_field']['#suffix'] = t('You must add a term field to this view to use taxonomy stripe values. This works best for vocabularies with only a limited number of possible terms.');
    }

    // Get the Vocabulary names.
    $vocab_names = array();
    foreach ($vocab_field_options as $field_name => $label) {
      $taxonomy_field = field_info_field($field_name);
      foreach ((array) $taxonomy_field['settings']['allowed_values'] as $delta => $options) {
        $vocab_names[$field_name] = $options['vocabulary'];
      }
    }

    // Get the Vocabulary id's.
    $vocab_vids = array();
    foreach ($vocab_names as $field_name => $vocab_name) {
      $vocab = taxonomy_vocabulary_machine_name_load($vocab_name);
      $vocab_vids[$field_name] = $vocab->vid;
    }
    $this->options['colors']['calendar_colors_vocabulary'] = $vocab_vids;
    $form['colors']['calendar_colors_vocabulary'] = array(
      '#title' => t('Vocabulary Legend Types'),
      '#type' => 'value',
      '#value' => $vocab_vids,
    );

    // Get the Vocabulary term id's and map to colors.
    $term_colors = $this->options['colors']['calendar_colors_taxonomy'];
    foreach ($vocab_vids as $field_name => $vid) {
      $vocab = taxonomy_get_tree($vid);
      foreach ($vocab as $key => $term) {
        $form['colors']['calendar_colors_taxonomy'][$term->tid] = array(
          '#title' => check_plain(t($term->name)),
          '#default_value' => isset($term_colors[$term->tid]) ? $term_colors[$term->tid] : CALENDAR_EMPTY_STRIPE,
          '#access' => !empty($vocab_field_options),
          '#dependency_count' => 2,
          // All 2 of the following #dependencies must be met.
          '#dependency' => array(
            'edit-row-options-colors-legend' => array(
              'taxonomy',
            ),
            'edit-row-options-colors-taxonomy-field' => array(
              $field_name,
            ),
          ),
          '#type' => 'textfield',
          '#size' => 7,
          '#maxlength' => 7,
          '#element_validate' => array(
            'calendar_validate_hex_color',
          ),
          '#prefix' => '<div class="calendar-colorpicker-wrapper">',
          '#suffix' => '<div class="calendar-colorpicker"></div></div>',
          '#attributes' => array(
            'class' => array(
              'edit-calendar-colorpicker',
            ),
          ),
          '#attached' => array(
            // Add Farbtastic color picker.
            'library' => array(
              array(
                'system',
                'farbtastic',
              ),
            ),
            // Add javascript to trigger the colorpicker.
            'js' => array(
              drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js',
            ),
          ),
        );
      }
    }
  }
  if (module_exists('og')) {
    $colors_group = $this->options['colors']['calendar_colors_group'];
    $options = array();

    // The 7.1 version of OG.
    if (function_exists('og_label')) {
      $gids = og_get_all_group();
      foreach ($gids as $gid) {
        $options[$gid] = check_plain(t(og_label($gid)));
      }
    }
    else {
      $types = og_get_all_group_entity();
      foreach ($types as $entity_type => $name) {
        $gids = og_get_all_group($entity_type);
        $entities = entity_load($entity_type, $gids);
        foreach ($entities as $entity) {
          list($gid) = entity_extract_ids($entity_type, $entity);
          $options[$gid] = check_plain(t(entity_label($entity_type, $entity)));
        }
      }
    }
    foreach ($options as $gid => $title) {
      $form['colors']['calendar_colors_group'][$gid] = array(
        '#title' => $title,
        '#default_value' => isset($colors_group[$gid]) ? $colors_group[$gid] : CALENDAR_EMPTY_STRIPE,
        '#dependency' => array(
          'edit-row-options-colors-legend' => array(
            'group',
          ),
        ),
        '#type' => 'textfield',
        '#size' => 7,
        '#maxlength' => 7,
        '#element_validate' => array(
          'calendar_validate_hex_color',
        ),
        '#prefix' => '<div class="calendar-colorpicker-wrapper">',
        '#suffix' => '<div class="calendar-colorpicker"></div></div>',
        '#attributes' => array(
          'class' => array(
            'edit-calendar-colorpicker',
          ),
        ),
        '#attached' => array(
          // Add Farbtastic color picker.
          'library' => array(
            array(
              'system',
              'farbtastic',
            ),
          ),
          // Add javascript to trigger the colorpicker.
          'js' => array(
            drupal_get_path('module', 'calendar') . '/js/calendar_colorpicker.js',
          ),
        ),
      );
    }
  }
}