Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_views / src / Plugin / views / display_extender / MetatagDisplayExtender.php
1 <?php
2
3 namespace Drupal\metatag_views\Plugin\views\display_extender;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\metatag\MetatagManagerInterface;
7 use Drupal\metatag\MetatagTagPluginManager;
8 use Drupal\views\Plugin\views\display_extender\DisplayExtenderPluginBase;
9 use Symfony\Component\DependencyInjection\ContainerInterface;
10
11 /**
12  * Metatag display extender plugin.
13  *
14  * @ingroup views_display_extender_plugins
15  *
16  * @ViewsDisplayExtender(
17  *   id = "metatag_display_extender",
18  *   title = @Translation("Metatag display extender"),
19  *   help = @Translation("Metatag settings for this view."),
20  *   no_ui = FALSE
21  * )
22  */
23 class MetatagDisplayExtender extends DisplayExtenderPluginBase {
24
25   /**
26    * The metatag manager.
27    *
28    * @var \Drupal\metatag\MetatagManagerInterface
29    */
30   protected $metatagManager;
31
32   /**
33    * The plugin manager for metatag tags.
34    *
35    * @var \Drupal\metatag\MetatagTagPluginManager
36    */
37   protected $metatagTagManager;
38
39   /**
40    * Constructs the plugin.
41    *
42    * @param array $configuration
43    *   A configuration array containing information about the plugin instance.
44    * @param string $plugin_id
45    *   The plugin_id for the plugin instance.
46    * @param mixed $plugin_definition
47    *   The plugin implementation definition.
48    * @param \Drupal\metatag\MetatagTagPluginManager $metatag_plugin_manager
49    *   The plugin manager for metatag tags.
50    * @param \Drupal\metatag\MetatagManagerInterface $metatag_manager
51    *   The metatag manager.
52    */
53   public function __construct(array $configuration, $plugin_id, $plugin_definition, MetatagTagPluginManager $metatag_plugin_manager, MetatagManagerInterface $metatag_manager) {
54     parent::__construct($configuration, $plugin_id, $plugin_definition);
55
56     $this->metatagTagManager = $metatag_plugin_manager;
57     $this->metatagManager = $metatag_manager;
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('plugin.manager.metatag.tag'),
69       $container->get('metatag.manager')
70     );
71   }
72
73   /**
74    * Provide a form to edit options for this plugin.
75    */
76   public function buildOptionsForm(&$form, FormStateInterface $form_state) {
77
78     if ($form_state->get('section') == 'metatags') {
79       $form['#title'] .= t('The meta tags for this display');
80       $metatags = $this->getMetatags();
81
82       // Build/inject the Metatag form.
83       $form['metatags'] = $this->metatagManager->form($metatags, $form, ['view']);
84     }
85   }
86
87   /**
88    * Validate the options form.
89    */
90   public function validateOptionsForm(&$form, FormStateInterface $form_state) {
91   }
92
93   /**
94    * Handle any special handling on the validate form.
95    */
96   public function submitOptionsForm(&$form, FormStateInterface $form_state) {
97     if ($form_state->get('section') == 'metatags') {
98       // Process submitted metatag values and remove empty tags.
99       $tag_values = [];
100       $metatags = $form_state->cleanValues()->getValues();
101       foreach ($metatags as $tag_id => $tag_value) {
102         // Some plugins need to process form input before storing it.
103         // Hence, we set it and then get it.
104         $tag = $this->metatagTagManager->createInstance($tag_id);
105         $tag->setValue($tag_value);
106         if (!empty($tag->value())) {
107           $tag_values[$tag_id] = $tag->value();
108         }
109       }
110       $this->options['metatags'] = $tag_values;
111     }
112   }
113
114   /**
115    * Set up any variables on the view prior to execution.
116    */
117   public function preExecute() {
118   }
119
120   /**
121    * Inject anything into the query that the display_extender handler needs.
122    */
123   public function query() {
124   }
125
126   /**
127    * Provide the default summary for options in the views UI.
128    *
129    * This output is returned as an array.
130    */
131   public function optionsSummary(&$categories, &$options) {
132     $categories['metatags'] = [
133       'title' => t('Meta tags'),
134       'column' => 'second',
135     ];
136     $options['metatags'] = [
137       'category' => 'metatags',
138       'title' => t('Meta tags'),
139       'value' => $this->hasMetatags() ? t('Overridden') : t('Using defaults'),
140     ];
141   }
142
143   /**
144    * Lists defaultable sections and items contained in each section.
145    */
146   public function defaultableSections(&$sections, $section = NULL) {
147   }
148
149   /**
150    * Identify whether or not the current display has custom meta tags defined.
151    *
152    * @return bool
153    *   Whether or not the view has overridden metatags.
154    */
155   protected function hasMetatags() {
156     $metatags = $this->getMetatags();
157     return !empty($metatags);
158
159   }
160
161   /**
162    * Get the Metatag configuration for this display.
163    *
164    * @return array
165    *   The meta tag values.
166    */
167   public function getMetatags() {
168     $metatags = [];
169
170     if (!empty($this->options['metatags'])) {
171       $metatags = $this->options['metatags'];
172     }
173
174     return $metatags;
175   }
176
177   /**
178    * Sets the meta tags for the given view.
179    *
180    * @param array $metatags
181    *   Metatag arrays as suitable for storage.
182    */
183   public function setMetatags(array $metatags) {
184     $this->options['metatags'] = $metatags;
185   }
186
187 }