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