Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / devel / src / Controller / ElementInfoController.php
1 <?php
2
3 namespace Drupal\devel\Controller;
4
5 use Drupal\Component\Serialization\Json;
6 use Drupal\Core\Controller\ControllerBase;
7 use Drupal\Core\Render\ElementInfoManagerInterface;
8 use Drupal\Core\Url;
9 use Drupal\devel\DevelDumperManagerInterface;
10 use Symfony\Component\DependencyInjection\ContainerInterface;
11 use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
13 /**
14  * Provides route responses for the element info page.
15  */
16 class ElementInfoController extends ControllerBase {
17
18   /**
19    * Element info manager service.
20    *
21    * @var \Drupal\Core\Render\ElementInfoManagerInterface
22    */
23   protected $elementInfo;
24
25   /**
26    * The dumper service.
27    *
28    * @var \Drupal\devel\DevelDumperManagerInterface
29    */
30   protected $dumper;
31
32   /**
33    * EventInfoController constructor.
34    *
35    * @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
36    *   Element info manager service.
37    * @param \Drupal\devel\DevelDumperManagerInterface $dumper
38    *   The dumper service.
39    */
40   public function __construct(ElementInfoManagerInterface $element_info, DevelDumperManagerInterface $dumper) {
41     $this->elementInfo = $element_info;
42     $this->dumper = $dumper;
43   }
44
45   /**
46    * {@inheritdoc}
47    */
48   public static function create(ContainerInterface $container) {
49     return new static(
50       $container->get('element_info'),
51       $container->get('devel.dumper')
52     );
53   }
54
55   /**
56    * Builds the element overview page.
57    *
58    * @return array
59    *   A render array as expected by the renderer.
60    */
61   public function elementList() {
62     $headers = [
63       $this->t('Name'),
64       $this->t('Provider'),
65       $this->t('Class'),
66       $this->t('Operations'),
67     ];
68
69     $rows = [];
70
71     foreach ($this->elementInfo->getDefinitions() as $element_type => $definition) {
72       $row['name'] = [
73         'data' => $element_type,
74         'class' => 'table-filter-text-source',
75       ];
76       $row['provider'] = [
77         'data' => $definition['provider'],
78         'class' => 'table-filter-text-source',
79       ];
80       $row['class'] = [
81         'data' => $definition['class'],
82         'class' => 'table-filter-text-source',
83       ];
84       $row['operations']['data'] = [
85         '#type' => 'operations',
86         '#links' => [
87           'devel' => [
88             'title' => $this->t('Devel'),
89             'url' => Url::fromRoute('devel.elements_page.detail', ['element_name' => $element_type]),
90             'attributes' => [
91               'class' => ['use-ajax'],
92               'data-dialog-type' => 'modal',
93               'data-dialog-options' => Json::encode([
94                 'width' => 700,
95                 'minHeight' => 500,
96               ]),
97             ],
98           ],
99         ],
100       ];
101
102       $rows[$element_type] = $row;
103     }
104
105     ksort($rows);
106
107     $output['#attached']['library'][] = 'system/drupal.system.modules';
108
109     $output['filters'] = [
110       '#type' => 'container',
111       '#attributes' => [
112         'class' => ['table-filter', 'js-show'],
113       ],
114     ];
115     $output['filters']['text'] = [
116       '#type' => 'search',
117       '#title' => $this->t('Search'),
118       '#size' => 30,
119       '#placeholder' => $this->t('Enter element id, provider or class'),
120       '#attributes' => [
121         'class' => ['table-filter-text'],
122         'data-table' => '.devel-filter-text',
123         'autocomplete' => 'off',
124         'title' => $this->t('Enter a part of the element id, provider or class to filter by.'),
125       ],
126     ];
127     $output['elements'] = [
128       '#type' => 'table',
129       '#header' => $headers,
130       '#rows' => $rows,
131       '#empty' => $this->t('No elements found.'),
132       '#sticky' => TRUE,
133       '#attributes' => [
134         'class' => ['devel-element-list', 'devel-filter-text'],
135       ],
136     ];
137
138     return $output;
139   }
140
141   /**
142    * Returns a render array representation of the element.
143    *
144    * @param string $element_name
145    *   The name of the element to retrieve.
146    *
147    * @return array
148    *   A render array containing the element.
149    *
150    * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
151    *   If the requested element is not defined.
152    */
153   public function elementDetail($element_name) {
154     if (!$element = $this->elementInfo->getDefinition($element_name, FALSE)) {
155       throw new NotFoundHttpException();
156     }
157
158     $element += $this->elementInfo->getInfo($element_name);
159     return $this->dumper->exportAsRenderable($element, $element_name);
160   }
161
162 }