metatag_views_plugin_display_extender_metatags.inc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @file
  4. * Custom display extender plugin for Views.
  5. */
  6. class metatag_views_plugin_display_extender_metatags extends views_plugin_display_extender {
  7. /**
  8. * Default values.
  9. */
  10. function options_definition() {
  11. $options = parent::option_definition();
  12. $options['metatags'] = array('default' => '');
  13. return $options;
  14. }
  15. function options_definition_alter(&$options) {
  16. $options['metatags'] = array('default' => array());
  17. }
  18. /**
  19. * Defines where within the Views admin UI the new settings will be visible.
  20. */
  21. function options_summary(&$categories, &$options) {
  22. $categories['metatags'] = array(
  23. 'title' => t('Meta tags'),
  24. 'column' => 'second',
  25. );
  26. $options['metatags'] = array(
  27. 'category' => 'metatags',
  28. 'title' => t('Meta tags'),
  29. 'value' => $this->has_metatags() ? t('Overridden') : t('Using defaults'),
  30. );
  31. }
  32. /**
  33. * Defines the form.
  34. */
  35. function options_form(&$form, &$form_state) {
  36. if ($form_state['section'] == 'metatags') {
  37. $form['#title'] .= t('The meta tags for this display');
  38. $metatags = $this->get_metatags();
  39. // Build/inject the Metatag form.
  40. $instance = 'view:' . $form_state['view']->name;
  41. $options['token types'] = array('view');
  42. $options['context'] = 'view';
  43. metatag_metatags_form($form, $instance, $metatags[LANGUAGE_NONE], $options);
  44. $form['metatags']['#type'] = 'container';
  45. // Basic tags fieldset should not collapse to display in a larger popup.
  46. // @see https://www.drupal.org/node/1294478
  47. // @see https://www.drupal.org/node/2624020
  48. $form['metatags'][LANGUAGE_NONE]['basic']['#collapsible'] = FALSE;
  49. }
  50. }
  51. /**
  52. * Saves the form values.
  53. */
  54. function options_submit(&$form, &$form_state) {
  55. if ($form_state['section'] == 'metatags') {
  56. $metatags = $form_state['values']['metatags'];
  57. // Leave some possibility for future versions to support translation.
  58. foreach ($metatags as $langcode => $values) {
  59. if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
  60. metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
  61. }
  62. }
  63. $this->display->set_option('metatags', $metatags);
  64. // Update the i18n strings.
  65. if (!empty($metatags[LANGUAGE_NONE]) && $this->definition['enabled'] && module_exists('i18n_string') && !variable_get('metatag_i18n_disabled', FALSE)) {
  66. metatag_translations_update($metatags[LANGUAGE_NONE], 'metatag_views:' . $this->view->name . '_' . $this->display->plugin_name);
  67. }
  68. }
  69. }
  70. /**
  71. * Identify whether or not the current display has custom meta tags defined.
  72. */
  73. protected function has_metatags() {
  74. $metatags = $this->get_metatags();
  75. return !empty($metatags[LANGUAGE_NONE]);
  76. }
  77. /**
  78. * Get the Metatag configuration for this display.
  79. *
  80. * @return array
  81. * The meta tag values, keys by language (default LANGUAGE_NONE).
  82. */
  83. private function get_metatags() {
  84. $metatags = $this->display->get_option('metatags');
  85. // Leave some possibility for future versions to support translation.
  86. if (empty($metatags)) {
  87. $metatags = array(LANGUAGE_NONE => array());
  88. }
  89. if (!isset($metatags[LANGUAGE_NONE])) {
  90. $metatags = array(LANGUAGE_NONE => $metatags);
  91. }
  92. return $metatags;
  93. }
  94. }