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