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