You are here

public function metatag_views_plugin_display_extender_metatags::options_form in Metatag 7

Provide a form to edit options for this plugin.

Overrides views_plugin_display_extender::options_form

File

metatag_views/metatag_views_plugin_display_extender_metatags.inc, line 50
Custom display extender plugin for Views.

Class

metatag_views_plugin_display_extender_metatags
Custom display extender plugin for Views.

Code

public function options_form(&$form, &$form_state) {

  // Defines the form.
  if ($form_state['section'] == 'metatags') {
    $form['#title'] .= t('The meta tags for this display');
    $metatags = $this
      ->get_metatags();

    // Build/inject the Metatag form.
    $instance = 'view:' . $form_state['view']->name;
    $options['token types'] = array(
      'view',
    );
    $options['context'] = 'view';
    metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
    $form['metatags']['#type'] = 'container';

    // Code for replacement tokens from first row taken from
    // views_handler_area_text::options_form().
    $form['tokenize'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use replacement tokens from the first row'),
      '#default_value' => $this->display
        ->get_option('metatags_tokenize'),
      '#weight' => 50,
    );

    // Get a list of the available fields and arguments for token replacement.
    $options = array();
    foreach ($this->view->display_handler
      ->get_handlers('field') as $field => $handler) {
      $options[t('Fields')]["[{$field}]"] = $handler
        ->ui_name();
    }
    $count = 0;

    // This lets us prepare the key as we want it printed.
    foreach ($this->view->display_handler
      ->get_handlers('argument') as $arg => $handler) {
      $options[t('Arguments')]['%' . ++$count] = t('@argument title', array(
        '@argument' => $handler
          ->ui_name(),
      ));
      $options[t('Arguments')]['!' . $count] = t('@argument input', array(
        '@argument' => $handler
          ->ui_name(),
      ));
    }
    if (!empty($options)) {
      $output = '<p>' . t("The following tokens are available. If you would like to have the characters '[' and ']' please use the html entity codes '%5B' or '%5D' or they will get replaced with empty space.") . '</p>';
      foreach (array_keys($options) as $type) {
        if (!empty($options[$type])) {
          $items = array();
          foreach ($options[$type] as $key => $value) {
            $items[] = $key . ' == ' . check_plain($value);
          }
          $output .= theme('item_list', array(
            'items' => $items,
            'type' => $type,
          ));
        }
      }
      $form['token_help'] = array(
        '#type' => 'fieldset',
        '#title' => t('Replacement patterns'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
        '#value' => $output,
        '#id' => 'edit-options-token-help',
        '#dependency' => array(
          'edit-options-tokenize' => array(
            1,
          ),
        ),
        '#prefix' => '<div>',
        '#suffix' => '</div>',
        '#weight' => 51,
      );
    }

    // Basic tags fieldset should not collapse to display in a larger popup.
    // @see https://www.drupal.org/node/1294478
    // @see https://www.drupal.org/node/2624020
    $form['metatags'][LANGUAGE_NONE]['basic']['#collapsible'] = FALSE;
  }
}