Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\toolbar\Element;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Render\Element\RenderElement;
|
Chris@0
|
6 use Drupal\Core\Url;
|
Chris@0
|
7
|
Chris@0
|
8 /**
|
Chris@0
|
9 * Provides a toolbar item that is wrapped in markup for common styling.
|
Chris@0
|
10 *
|
Chris@0
|
11 * The 'tray' property contains a renderable array.
|
Chris@0
|
12 *
|
Chris@0
|
13 * @RenderElement("toolbar_item")
|
Chris@0
|
14 */
|
Chris@0
|
15 class ToolbarItem extends RenderElement {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * {@inheritdoc}
|
Chris@0
|
19 */
|
Chris@0
|
20 public function getInfo() {
|
Chris@0
|
21 $class = get_class($this);
|
Chris@0
|
22 return [
|
Chris@0
|
23 '#pre_render' => [
|
Chris@0
|
24 [$class, 'preRenderToolbarItem'],
|
Chris@0
|
25 ],
|
Chris@0
|
26 'tab' => [
|
Chris@0
|
27 '#type' => 'link',
|
Chris@0
|
28 '#title' => NULL,
|
Chris@0
|
29 '#url' => Url::fromRoute('<front>'),
|
Chris@0
|
30 ],
|
Chris@0
|
31 ];
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Provides markup for associating a tray trigger with a tray element.
|
Chris@0
|
36 *
|
Chris@0
|
37 * A tray is a responsive container that wraps renderable content. Trays
|
Chris@0
|
38 * present content well on small and large screens alike.
|
Chris@0
|
39 *
|
Chris@0
|
40 * @param array $element
|
Chris@0
|
41 * A renderable array.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @return array
|
Chris@0
|
44 * A renderable array.
|
Chris@0
|
45 */
|
Chris@0
|
46 public static function preRenderToolbarItem($element) {
|
Chris@0
|
47 $id = $element['#id'];
|
Chris@0
|
48
|
Chris@0
|
49 // Provide attributes for a toolbar item.
|
Chris@0
|
50 $attributes = [
|
Chris@0
|
51 'id' => $id,
|
Chris@0
|
52 ];
|
Chris@0
|
53
|
Chris@0
|
54 // If tray content is present, markup the tray and its associated trigger.
|
Chris@0
|
55 if (!empty($element['tray'])) {
|
Chris@0
|
56 // Provide attributes necessary for trays.
|
Chris@0
|
57 $attributes += [
|
Chris@0
|
58 'data-toolbar-tray' => $id . '-tray',
|
Chris@0
|
59 'aria-owns' => $id . '-tray',
|
Chris@0
|
60 'role' => 'button',
|
Chris@0
|
61 'aria-pressed' => 'false',
|
Chris@0
|
62 ];
|
Chris@0
|
63
|
Chris@0
|
64 // Merge in module-provided attributes.
|
Chris@0
|
65 $element['tab'] += ['#attributes' => []];
|
Chris@0
|
66 $element['tab']['#attributes'] += $attributes;
|
Chris@0
|
67 $element['tab']['#attributes']['class'][] = 'trigger';
|
Chris@0
|
68
|
Chris@0
|
69 // Provide attributes for the tray theme wrapper.
|
Chris@0
|
70 $attributes = [
|
Chris@0
|
71 'id' => $id . '-tray',
|
Chris@0
|
72 'data-toolbar-tray' => $id . '-tray',
|
Chris@0
|
73 ];
|
Chris@0
|
74 // Merge in module-provided attributes.
|
Chris@0
|
75 if (!isset($element['tray']['#wrapper_attributes'])) {
|
Chris@0
|
76 $element['tray']['#wrapper_attributes'] = [];
|
Chris@0
|
77 }
|
Chris@0
|
78 $element['tray']['#wrapper_attributes'] += $attributes;
|
Chris@0
|
79 $element['tray']['#wrapper_attributes']['class'][] = 'toolbar-tray';
|
Chris@0
|
80 }
|
Chris@0
|
81
|
Chris@0
|
82 $element['tab']['#attributes']['class'][] = 'toolbar-item';
|
Chris@0
|
83
|
Chris@0
|
84 return $element;
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 }
|