Updated all the contrib modules to their latest versions.
[yaffs-website] / web / modules / contrib / metatag / src / MetatagDefaultsListBuilder.php
1 <?php
2
3 namespace Drupal\metatag;
4
5 use Drupal\Core\Config\Entity\ConfigEntityListBuilder;
6 use Drupal\Core\Entity\EntityInterface;
7
8 /**
9  * Provides a listing of Metatag defaults entities.
10  */
11 class MetatagDefaultsListBuilder extends ConfigEntityListBuilder {
12
13   /**
14    * {@inheritdoc}
15    */
16   protected function getEntityIds() {
17     $query = $this->getStorage()->getQuery()
18       ->condition('id', 'global', '<>');
19
20     // Only add the pager if a limit is specified.
21     if ($this->limit) {
22       $query->pager($this->limit);
23     }
24
25     $entity_ids = $query->execute();
26
27     // Load global entity always.
28     return $entity_ids + $this->getParentIds($entity_ids);
29   }
30
31   /**
32    * Gets the parent entity ids for the list of entities to load.
33    *
34    * @param array $entity_ids
35    *   The metatag entity ids.
36    *
37    * @return array
38    *   The list of parents to load
39    */
40   protected function getParentIds(array $entity_ids) {
41     $parents = ['global' => 'global'];
42     foreach ($entity_ids as $entity_id) {
43       if (strpos($entity_id, '__') !== FALSE) {
44         $entity_id_array = explode('__', $entity_id);
45         $parent = reset($entity_id_array);
46         $parents[$parent] = $parent;
47       }
48     }
49     $parents_query = $this->getStorage()->getQuery()
50       ->condition('id', $parents, 'IN');
51     return $parents_query->execute();
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public function buildHeader() {
58     $header['label'] = $this->t('Type');
59     return $header + parent::buildHeader();
60   }
61
62   /**
63    * {@inheritdoc}
64    */
65   public function buildRow(EntityInterface $entity) {
66     $row['label'] = $this->getLabelAndConfig($entity);
67     return $row + parent::buildRow($entity);
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function getOperations(EntityInterface $entity) {
74     $operations = parent::getOperations($entity);
75
76     // Global and entity defaults can be reverted but not deleted.
77     if (in_array($entity->id(), MetatagManager::protectedDefaults())) {
78       unset($operations['delete']);
79       $operations['revert'] = [
80         'title' => t('Revert'),
81         'weight' => $operations['edit']['weight'] + 1,
82         'url' => $entity->toUrl('revert-form'),
83       ];
84     }
85
86     return $operations;
87   }
88
89   /**
90    * Renders the Metatag defaults label plus its configuration.
91    *
92    * @param \Drupal\Core\Entity\EntityInterface $entity
93    *   The Metatag defaults entity.
94    *
95    * @return array
96    *   Render array for a table cell.
97    */
98   public function getLabelAndConfig(EntityInterface $entity) {
99     $output = '<div>';
100     $prefix = '';
101     $inherits = '';
102     if ($entity->id() != 'global') {
103       $prefix = '<div class="indentation"></div>';
104       $inherits .= 'Global';
105     }
106     if (strpos($entity->id(), '__') !== FALSE) {
107       $prefix .= '<div class="indentation"></div>';
108       list($entity_label, $bundle_label) = explode(': ', $entity->get('label'));
109       $inherits .= ', ' . $entity_label;
110     }
111
112     if (!empty($inherits)) {
113       $output .= '<div><p>' . t('Inherits meta tags from: @inherits', [
114         '@inherits' => $inherits,
115       ]) . '</p></div>';
116     }
117     $tags = $entity->get('tags');
118     if (count($tags)) {
119       $output .= '<table>
120 <tbody>';
121       foreach ($tags as $tag_id => $tag_value) {
122         $output .= '<tr><td>' . $tag_id . ':</td><td>' . $tag_value . '</td></tr>';
123       }
124       $output .= '</tbody></table>';
125     }
126
127     $output .= '</div></div>';
128
129     return [
130       'data' => [
131         '#type' => 'details',
132         '#prefix' => $prefix,
133         '#title' => $entity->label(),
134         'config' => [
135           '#markup' => $output,
136         ],
137       ],
138     ];
139   }
140
141 }