You are here

class metatag_views_plugin_display_extender_metatags in Metatag 7

Custom display extender plugin for Views.

Hierarchy

Expanded class hierarchy of metatag_views_plugin_display_extender_metatags

1 string reference to 'metatag_views_plugin_display_extender_metatags'
metatag_views_views_plugins in metatag_views/metatag_views.views.inc
Implements hook_views_plugins().

File

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

View source
class metatag_views_plugin_display_extender_metatags extends views_plugin_display_extender {

  /**
   * {@inheritdoc}
   */
  public function options_definition() {
    $options = parent::option_definition();
    $options['metatags'] = array(
      'default' => '',
    );
    $options['metatags_tokenize'] = array(
      'bool' => TRUE,
      'default' => FALSE,
    );
    return $options;
  }

  /**
   * {@inheritdoc}
   */
  public function options_definition_alter(&$options) {
    $options['metatags'] = array(
      'default' => array(),
    );
    $options['metatags_tokenize'] = array(
      'bool' => TRUE,
      'default' => FALSE,
    );
  }

  /**
   * {@inheritdoc}
   */
  public function options_summary(&$categories, &$options) {

    // Defines where within the Views admin UI the new settings will be visible.
    $categories['metatags'] = array(
      'title' => t('Meta tags'),
      'column' => 'second',
    );
    $options['metatags'] = array(
      'category' => 'metatags',
      'title' => t('Meta tags'),
      'value' => $this
        ->has_metatags() ? t('Overridden') : t('Using defaults'),
    );
  }

  /**
   * {@inheritdoc}
   */
  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;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function options_submit(&$form, &$form_state) {

    // Save the form values.
    if ($form_state['section'] == 'metatags') {
      $metatags = $form_state['values']['metatags'];

      // Leave some possibility for future versions to support translation.
      foreach ($metatags as $langcode => $values) {
        if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
          metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
        }
      }
      $this->display
        ->set_option('metatags', $metatags);
      $this->display
        ->set_option('metatags_tokenize', $form_state['values']['tokenize']);

      // Update the i18n strings.
      if (!empty($metatags[LANGUAGE_NONE]) && $this->definition['enabled'] && module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
        metatag_translations_update($metatags[LANGUAGE_NONE], 'metatag_views:' . $this->view->name . METATAG_VIEWS_CONTEXT_SEPARATOR . $this->view->current_display);
      }
    }
  }

  /**
   * Identify whether or not the current display has custom meta tags defined.
   *
   * @return bool
   *   Indicates whether the current display has custom meta tags defined.
   */
  protected function has_metatags() {
    $metatags = $this
      ->get_metatags();
    return !empty($metatags[LANGUAGE_NONE]);
  }

  /**
   * Get the Metatag configuration for this display.
   *
   * @return array
   *   The meta tag values, keys by language (default LANGUAGE_NONE).
   */
  private function get_metatags() {
    $metatags = $this->display
      ->get_option('metatags');

    // Leave some possibility for future versions to support translation.
    if (empty($metatags)) {
      $metatags = array(
        LANGUAGE_NONE => array(),
      );
    }
    if (!isset($metatags[LANGUAGE_NONE])) {
      $metatags = array(
        LANGUAGE_NONE => $metatags,
      );
    }
    return $metatags;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
metatag_views_plugin_display_extender_metatags::get_metatags private function Get the Metatag configuration for this display.
metatag_views_plugin_display_extender_metatags::has_metatags protected function Identify whether or not the current display has custom meta tags defined.
metatag_views_plugin_display_extender_metatags::options_definition public function
metatag_views_plugin_display_extender_metatags::options_definition_alter public function Provide a form to edit options for this plugin. Overrides views_plugin_display_extender::options_definition_alter
metatag_views_plugin_display_extender_metatags::options_form public function Provide a form to edit options for this plugin. Overrides views_plugin_display_extender::options_form
metatag_views_plugin_display_extender_metatags::options_submit public function Handle any special handling on the validate form. Overrides views_plugin_display_extender::options_submit
metatag_views_plugin_display_extender_metatags::options_summary public function Provide the default summary for options in the views UI. Overrides views_plugin_display_extender::options_summary
views_object::$definition public property Handler's definition.
views_object::$options public property Except for displays, options for the object will be held here. 1
views_object::altered_option_definition function Collect this handler's option definition and alter them, ready for use.
views_object::construct public function Views handlers use a special construct function. 4
views_object::destroy public function Destructor. 2
views_object::export_option public function 1
views_object::export_options public function
views_object::export_option_always public function Always exports the option, regardless of the default value.
views_object::options Deprecated public function Set default options on this object. 1
views_object::option_definition public function Information about options for all kinds of purposes will be held here. 13
views_object::set_default_options public function Set default options.
views_object::set_definition public function Let the handler know what its full definition is.
views_object::unpack_options public function Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away.
views_object::unpack_translatable public function Unpack a single option definition.
views_object::unpack_translatables public function Unpacks each handler to store translatable texts.
views_object::_set_option_defaults public function
views_plugin::$display public property The current used views display.
views_plugin::$plugin_name public property The plugin name of this plugin, for example table or full.
views_plugin::$plugin_type public property The plugin type of this plugin, for example style or query.
views_plugin::$view public property The top object of a view. Overrides views_object::$view 1
views_plugin::additional_theme_functions public function Provide a list of additional theme functions for the theme info page.
views_plugin::plugin_title public function Return the human readable name of the display.
views_plugin::summary_title public function Returns the summary of the settings in the display. 8
views_plugin::theme_functions public function Provide a full list of possible theme templates used by this style.
views_plugin::validate public function Validate that the plugin is correct and can be saved. 3
views_plugin_display_extender::defaultable_sections public function Static member function to list which sections are defaultable and what items each section contains.
views_plugin_display_extender::init public function
views_plugin_display_extender::options_validate public function Validate the options form. Overrides views_plugin::options_validate
views_plugin_display_extender::pre_execute public function Set up any variables on the view prior to execution.
views_plugin_display_extender::query public function Inject anything into the query that the display_extender handler needs. Overrides views_plugin::query