Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / metatag_views / src / Controller / MetatagViewsController.php
1 <?php
2
3 namespace Drupal\metatag_views\Controller;
4
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
7 use Drupal\Core\Url;
8 use Drupal\metatag\MetatagManagerInterface;
9 use Drupal\views\Views;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11
12 /**
13  * Class MetatagViewsController.
14  *
15  * @package Drupal\metatag_views\Controller
16  */
17 class MetatagViewsController extends ControllerBase {
18
19   /**
20    * The Views storage interface.
21    *
22    * @var \Drupal\Core\Entity\EntityStorageInterface
23    */
24   protected $viewStorage;
25
26   /**
27    * The Metatag manager interface.
28    *
29    * @var \Drupal\metatag\MetatagManagerInterface
30    */
31   protected $metatagManager;
32
33   /**
34    * Associative array of labels.
35    *
36    * @var array
37    */
38   protected $viewLabels;
39
40   /**
41    * {@inheritdoc}
42    */
43   public function __construct(EntityStorageInterface $viewStorage, MetatagManagerInterface $metatagManager) {
44     $this->viewStorage = $viewStorage;
45     $this->metatagManager = $metatagManager;
46
47     // Generate the labels for views and displays.
48     $this->labels = $this->getViewsAndDisplaysLabels();
49   }
50
51   /**
52    * {@inheritdoc}
53    */
54   public static function create(ContainerInterface $container) {
55     return new static(
56       $container->get('entity_type.manager')->getStorage('view'),
57       $container->get('metatag.manager')
58     );
59   }
60
61   /**
62    * Get meta tags for all of the views / displays that have them set.
63    *
64    * @return array
65    *   List of tags grouped by view and display.
66    */
67   public static function getTaggedViews() {
68     $tagged_views = [];
69     foreach (Views::getEnabledViews() as $view_id => $view) {
70       $displays = $view->get('display');
71       foreach (array_keys($displays) as $display_id) {
72         if ($tags = metatag_get_view_tags($view_id, $display_id)) {
73           $tagged_views[$view_id][$display_id] = $tags;
74         }
75       }
76     }
77     return $tagged_views;
78   }
79
80   /**
81    * Generates the renderable array for views meta tags UI.
82    *
83    * @return array
84    *   The list of details.
85    */
86   public function listViews() {
87     $elements = [];
88
89     $elements['header'] = [
90       '#markup' => '<p>' . $this->t("To view a list of displays with meta tags set up, click on a view name. To view a summary of meta tags configuration for a particular display, click on the display name. If you need to set meta tags for a specific view, choose Add views meta tags. Reverting the meta tags removes the specific configuration and falls back to defaults.") . '</p>',
91     ];
92
93     // Iterate over the values and build the whole UI.
94     // 1. Top level is a collapsible fieldset with a view name (details)
95     // 2. Inside each fieldset we have 2 columns -> Display and Operations.
96     //    Display contains point 3.
97     //    Operations contain edit and revert.
98     // 3. In each display there is a table that has 2 columns: tag name and tag
99     //    value.
100     $tagged_views = $this->getTaggedViews();
101     foreach ($tagged_views as $view_id => $displays) {
102       $elements[$view_id] = [
103         '#type' => 'details',
104         '#title' => $this->t($this->viewLabels[$view_id]['#label']),
105         'details' => $this->buildViewDetails($view_id, $displays),
106       ];
107     }
108
109     return $elements;
110   }
111
112   /**
113    * Builds the second "level" of the UI table with display fieldset and ops.
114    *
115    * @param string $view_id
116    *   The view display to use.
117    * @param array $displays
118    *   The displays to process.
119    *
120    * @return array
121    *   Render array.
122    */
123   protected function buildViewDetails($view_id, array $displays) {
124     $element = [
125       '#type' => 'table',
126       '#collapsible' => TRUE,
127       '#header' => [
128         $this->t('Display'),
129         $this->t('Operations'),
130       ],
131     ];
132
133     foreach ($displays as $display_id => $metatags) {
134       $metatags = array_filter($metatags);
135
136       $element[$display_id]['details'] = [
137         '#type' => 'details',
138         '#title' => $this->viewLabels[$view_id][$display_id],
139       ];
140
141       $params = [
142         'view_id' => $view_id,
143         'display_id' => $display_id,
144       ];
145
146       // Generate the operations.
147       $element[$display_id]['ops'] = [
148         '#type' => 'operations',
149         '#links' => [
150           'edit' => [
151             'title' => $this->t('Edit'),
152             'url' => Url::fromRoute('metatag_views.metatags.edit', $params),
153           ],
154           'translate' => [
155             'title' => $this->t('Translate'),
156             'url' => Url::fromRoute('metatag_views.metatags.translate_overview', $params),
157           ],
158           'revert' => [
159             'title' => $this->t('Revert'),
160             'url' => Url::fromRoute('metatag_views.metatags.revert', $params),
161           ],
162         ],
163       ];
164
165       // Build the rows for each of the metatag types.
166       $element[$display_id]['details']['table'] = $this->buildDisplayDetailsTable($metatags);
167     }
168
169     return $element;
170   }
171
172   /**
173    * Build the table with metatag values summary.
174    *
175    * @param array $tags
176    *   The tags to process.
177    *
178    * @return array
179    *   The tag structure in a display element.
180    */
181   protected function buildDisplayDetailsTable(array $tags) {
182     $element = [
183       '#type' => 'table',
184     ];
185
186     $i = 0;
187     foreach ($tags as $tag_name => $tag_value) {
188       // This is for the case where we have a subarray.
189       $tag_value = $this->prepareTagValue($tag_value);
190       if (!$tag_value) {
191         continue;
192       }
193
194       $element[$i]['tag_name'] = [
195         '#type' => 'markup',
196         '#markup' => $tag_name,
197       ];
198
199       $element[$i]['tag_value'] = [
200         '#type' => 'markup',
201         '#markup' => $tag_value,
202       ];
203       $i++;
204     }
205
206     return $element;
207   }
208
209   /**
210    * Massage the tag value.
211    *
212    * @param string $value
213    *   The meta tag to output.
214    *
215    * @return string
216    *   An imploded string for meta tags that are nested, ex. robots.
217    */
218   protected function prepareTagValue($value) {
219     if (is_array($value)) {
220       $value = implode(', ', array_filter($value));
221     }
222
223     return $value;
224   }
225
226   /**
227    * Gets label values for the views and their displays.
228    */
229   protected function getViewsAndDisplaysLabels() {
230     /** @var \Drupal\views\ViewEntityInterface[] $views */
231     $views = $this->viewStorage->loadByProperties(['status' => 1]);
232
233     $labels = [];
234
235     foreach ($views as $view_id => $view) {
236       $displays = $view->get('display');
237       $labels[$view_id]['#label'] = $view->label();
238       foreach (array_keys($displays) as $display_id) {
239         $labels[$view_id][$display_id] = $displays[$display_id]['display_title'];
240       }
241     }
242
243     $this->viewLabels = $labels;
244   }
245
246 }