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