metatag_views_plugin_display_extender_metatags.inc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /**
  3. * @file
  4. * Custom display extender plugin for Views.
  5. */
  6. /**
  7. * Custom display extender plugin for Views.
  8. */
  9. class metatag_views_plugin_display_extender_metatags extends views_plugin_display_extender {
  10. /**
  11. * {@inheritdoc}
  12. */
  13. function options_definition() {
  14. $options = parent::option_definition();
  15. $options['metatags'] = array('default' => '');
  16. $options['metatags_tokenize'] = array('bool' => TRUE, 'default' => FALSE);
  17. return $options;
  18. }
  19. /**
  20. * {@inheritdoc}
  21. */
  22. function options_definition_alter(&$options) {
  23. $options['metatags'] = array('default' => array());
  24. $options['metatags_tokenize'] = array('bool' => TRUE, 'default' => FALSE);
  25. }
  26. /**
  27. * {@inheritdoc}
  28. */
  29. function options_summary(&$categories, &$options) {
  30. // Defines where within the Views admin UI the new settings will be visible.
  31. $categories['metatags'] = array(
  32. 'title' => t('Meta tags'),
  33. 'column' => 'second',
  34. );
  35. $options['metatags'] = array(
  36. 'category' => 'metatags',
  37. 'title' => t('Meta tags'),
  38. 'value' => $this->has_metatags() ? t('Overridden') : t('Using defaults'),
  39. );
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. function options_form(&$form, &$form_state) {
  45. // Defines the form.
  46. if ($form_state['section'] == 'metatags') {
  47. $form['#title'] .= t('The meta tags for this display');
  48. $metatags = $this->get_metatags();
  49. // Build/inject the Metatag form.
  50. $instance = 'view:' . $form_state['view']->name;
  51. $options['token types'] = array('view');
  52. $options['context'] = 'view';
  53. metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
  54. $form['metatags']['#type'] = 'container';
  55. // Code for replacement tokens from first row taken from
  56. // views_handler_area_text::options_form().
  57. $form['tokenize'] = array(
  58. '#type' => 'checkbox',
  59. '#title' => t('Use replacement tokens from the first row'),
  60. '#default_value' => $this->display->get_option('metatags_tokenize'),
  61. '#weight' => 50,
  62. );
  63. // Get a list of the available fields and arguments for token replacement.
  64. $options = array();
  65. foreach ($this->view->display_handler->get_handlers('field') as $field => $handler) {
  66. $options[t('Fields')]["[$field]"] = $handler->ui_name();
  67. }
  68. $count = 0;
  69. // This lets us prepare the key as we want it printed.
  70. foreach ($this->view->display_handler->get_handlers('argument') as $arg => $handler) {
  71. $options[t('Arguments')]['%' . ++$count] = t('@argument title', array('@argument' => $handler->ui_name()));
  72. $options[t('Arguments')]['!' . $count] = t('@argument input', array('@argument' => $handler->ui_name()));
  73. }
  74. if (!empty($options)) {
  75. $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>');
  76. foreach (array_keys($options) as $type) {
  77. if (!empty($options[$type])) {
  78. $items = array();
  79. foreach ($options[$type] as $key => $value) {
  80. $items[] = $key . ' == ' . check_plain($value);
  81. }
  82. $output .= theme('item_list',
  83. array(
  84. 'items' => $items,
  85. 'type' => $type,
  86. ));
  87. }
  88. }
  89. $form['token_help'] = array(
  90. '#type' => 'fieldset',
  91. '#title' => t('Replacement patterns'),
  92. '#collapsible' => TRUE,
  93. '#collapsed' => TRUE,
  94. '#value' => $output,
  95. '#id' => 'edit-options-token-help',
  96. '#dependency' => array(
  97. 'edit-options-tokenize' => array(1),
  98. ),
  99. '#prefix' => '<div>',
  100. '#suffix' => '</div>',
  101. '#weight' => 51,
  102. );
  103. }
  104. // Basic tags fieldset should not collapse to display in a larger popup.
  105. // @see https://www.drupal.org/node/1294478
  106. // @see https://www.drupal.org/node/2624020
  107. $form['metatags'][LANGUAGE_NONE]['basic']['#collapsible'] = FALSE;
  108. }
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. function options_submit(&$form, &$form_state) {
  114. // Save the form values.
  115. if ($form_state['section'] == 'metatags') {
  116. $metatags = $form_state['values']['metatags'];
  117. // Leave some possibility for future versions to support translation.
  118. foreach ($metatags as $langcode => $values) {
  119. if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
  120. metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
  121. }
  122. }
  123. $this->display->set_option('metatags', $metatags);
  124. $this->display->set_option('metatags_tokenize', $form_state['values']['tokenize']);
  125. // Update the i18n strings.
  126. if (!empty($metatags[LANGUAGE_NONE]) && $this->definition['enabled'] && module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  127. metatag_translations_update($metatags[LANGUAGE_NONE], 'metatag_views:' . $this->view->name . '_' . $this->display->plugin_name);
  128. }
  129. }
  130. }
  131. /**
  132. * Identify whether or not the current display has custom meta tags defined.
  133. *
  134. * @return bool
  135. * Indicates whether the current display has custom meta tags defined.
  136. */
  137. protected function has_metatags() {
  138. $metatags = $this->get_metatags();
  139. return !empty($metatags[LANGUAGE_NONE]);
  140. }
  141. /**
  142. * Get the Metatag configuration for this display.
  143. *
  144. * @return array
  145. * The meta tag values, keys by language (default LANGUAGE_NONE).
  146. */
  147. private function get_metatags() {
  148. $metatags = $this->display->get_option('metatags');
  149. // Leave some possibility for future versions to support translation.
  150. if (empty($metatags)) {
  151. $metatags = array(LANGUAGE_NONE => array());
  152. }
  153. if (!isset($metatags[LANGUAGE_NONE])) {
  154. $metatags = array(LANGUAGE_NONE => $metatags);
  155. }
  156. return $metatags;
  157. }
  158. }