3 namespace Drupal\metatag_views\Controller;
5 use Drupal\Core\Controller\ControllerBase;
6 use Drupal\Core\Entity\EntityStorageInterface;
8 use Drupal\metatag\MetatagManagerInterface;
9 use Drupal\views\Views;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
13 * Class MetatagViewsController.
15 * @package Drupal\metatag_views\Controller
17 class MetatagViewsController extends ControllerBase {
20 * @var \Drupal\Core\Entity\EntityStorageInterface
22 protected $viewStorage;
25 * @var \Drupal\metatag\MetatagManagerInterface
27 protected $metatagManager;
30 * Associative array of labels.
34 protected $viewLabels;
39 public function __construct(EntityStorageInterface $viewStorage, MetatagManagerInterface $metatagManager) {
40 $this->viewStorage = $viewStorage;
41 $this->metatagManager = $metatagManager;
43 // Generate the labels for views and displays.
44 $this->labels = $this->getViewsAndDisplaysLabels();
50 public static function create(ContainerInterface $container) {
52 $container->get('entity_type.manager')->getStorage('view'),
53 $container->get('metatag.manager')
58 * Get meta tags for all of the views / displays that have them set.
61 * List of tags grouped by view and display.
63 public static function getTaggedViews() {
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;
77 * Generates the renderable array for views meta tags UI.
82 public function listViews() {
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>',
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
96 $tagged_views = $this->getTaggedViews();
97 foreach ($tagged_views as $view_id => $displays) {
98 $elements[$view_id] = [
100 '#title' => $this->t($this->viewLabels[$view_id]['#label']),
101 'details' => $this->buildViewDetails($view_id, $displays),
109 * Builds the second "level" of the UI table with display fieldset and ops.
111 * @param string $view_id
112 * The view display to use.
113 * @param array $displays
114 * The displays to process.
119 protected function buildViewDetails($view_id, array $displays) {
122 '#collapsible' => TRUE,
125 $this->t('Operations'),
129 foreach ($displays as $display_id => $metatags) {
130 $metatags = array_filter($metatags);
132 $element[$display_id]['details'] = [
133 '#type' => 'details',
134 '#title' => $this->viewLabels[$view_id][$display_id],
138 'view_id' => $view_id,
139 'display_id' => $display_id,
142 // Generate the operations.
143 $element[$display_id]['ops'] = [
144 '#type' => 'operations',
147 'title' => t('Edit'),
148 'url' => Url::fromRoute('metatag_views.metatags.edit', $params),
151 'title' => t('Translate'),
152 'url' => Url::fromRoute('metatag_views.metatags.translate_overview', $params),
155 'title' => t('Revert'),
156 'url' => Url::fromRoute('metatag_views.metatags.revert', $params),
161 // Build the rows for each of the metatag types.
162 $element[$display_id]['details']['table'] = $this->buildDisplayDetailsTable($metatags);
169 * Build the table with metatag values summary.
172 * The tags to process.
175 * The tag structure in a display element.
177 protected function buildDisplayDetailsTable(array $tags) {
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);
190 $element[$i]['tag_name'] = [
192 '#markup' => $tag_name,
195 $element[$i]['tag_value'] = [
197 '#markup' => $tag_value,
206 * Massage the tag value.
208 * @param string $value
209 * The meta tag to output.
212 * An imploded string for meta tags that are nested, ex. robots.
214 protected function prepareTagValue($value) {
215 if (is_array($value)) {
216 $value = implode(', ', array_filter($value));
223 * Gets label values for the views and their displays.
225 protected function getViewsAndDisplaysLabels() {
226 /** @var \Drupal\views\ViewEntityInterface[] $views */
227 $views = $this->viewStorage->loadByProperties(['status' => 1]);
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'];
239 $this->viewLabels = $labels;