3811665f091b5a05a512b4de1af8bf74468844d0
[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   public function load() {
17     $entities = parent::load();
18
19     // Move the Global defaults to the top. Don't assume that the global config
20     // exists, it might have been removed.
21     if (isset($entities['global'])) {
22       return ['global' => $entities['global']] + $entities;
23     }
24     else {
25       return $entities;
26     }
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function buildHeader() {
33     $header['label'] = $this->t('Type');
34     return $header + parent::buildHeader();
35   }
36
37   /**
38    * {@inheritdoc}
39    */
40   public function buildRow(EntityInterface $entity) {
41     $row['label'] = $this->getLabelAndConfig($entity);
42     return $row + parent::buildRow($entity);
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public function getOperations(EntityInterface $entity) {
49     $operations = parent::getOperations($entity);
50
51     // Global and entity defaults can be reverted but not deleted.
52     if (in_array($entity->id(), MetatagManager::protectedDefaults())) {
53       unset($operations['delete']);
54       $operations['revert'] = [
55         'title' => t('Revert'),
56         'weight' => $operations['edit']['weight'] + 1,
57         'url' => $entity->toUrl('revert-form'),
58       ];
59     }
60
61     return $operations;
62   }
63
64   /**
65    * Renders the Metatag defaults label plus its configuration.
66    *
67    * @param Drupal\Core\Entity\EntityInterface $entity
68    *   The Metatag defaults entity.
69    *
70    * @return array
71    *   Render array for a table cell.
72    */
73   public function getLabelAndConfig(EntityInterface $entity) {
74     $output = '<div>';
75     $prefix = '';
76     $inherits = '';
77     if ($entity->id() != 'global') {
78       $prefix = '<div class="indentation"></div>';
79       $inherits .= 'Global';
80     }
81     if (strpos($entity->id(), '__') !== FALSE) {
82       $prefix .= '<div class="indentation"></div>';
83       list($entity_label, $bundle_label) = explode(': ', $entity->get('label'));
84       $inherits .= ', ' . $entity_label;
85     }
86
87     if (!empty($inherits)) {
88       $output .= '<div><p>' . t('Inherits meta tags from: @inherits', [
89         '@inherits' => $inherits,
90       ]) . '</p></div>';
91     }
92     $tags = $entity->get('tags');
93     if (count($tags)) {
94       $output .= '<table>
95 <tbody>';
96       foreach ($tags as $tag_id => $tag_value) {
97         $output .= '<tr><td>' . $tag_id . ':</td><td>' . $tag_value . '</td></tr>';
98       }
99       $output .= '</tbody></table>';
100     }
101
102     $output .= '</div></div>';
103
104     return [
105       'data' => [
106         '#type' => 'details',
107         '#prefix' => $prefix,
108         '#title' => $entity->label(),
109         'config' => [
110           '#markup' => $output,
111         ],
112       ],
113     ];
114   }
115
116 }