metatag_views_plugin_display_extender_metatags.inc 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. }
  46. }
  47. /**
  48. * Saves the form values.
  49. */
  50. function options_submit(&$form, &$form_state) {
  51. if ($form_state['section'] == 'metatags') {
  52. $metatags = $form_state['values']['metatags'];
  53. // Leave some possibility for future versions to support translation.
  54. foreach ($metatags as $langcode => $values) {
  55. if (!empty($form['metatags'][$langcode]['#metatag_defaults'])) {
  56. metatag_filter_values_from_defaults($form_state['values']['metatags'][$langcode], $form['metatags'][$langcode]['#metatag_defaults']);
  57. }
  58. }
  59. $this->display->set_option('metatags', $metatags);
  60. }
  61. }
  62. /**
  63. * Identify whether or not the current display has custom meta tags defined.
  64. */
  65. protected function has_metatags() {
  66. $metatags = $this->get_metatags();
  67. return !empty($metatags[LANGUAGE_NONE]);
  68. }
  69. /**
  70. * Get the Metatag configuration for this display.
  71. *
  72. * @return array
  73. * The meta tag values, keys by language (default LANGUAGE_NONE).
  74. */
  75. private function get_metatags() {
  76. $metatags = $this->display->get_option('metatags');
  77. // Leave some possibility for future versions to support translation.
  78. if (empty($metatags)) {
  79. $metatags = array(LANGUAGE_NONE => array());
  80. }
  81. if (!isset($metatags[LANGUAGE_NONE])) {
  82. $metatags = array(LANGUAGE_NONE => $metatags);
  83. }
  84. return $metatags;
  85. }
  86. }