comparison core/modules/contextual/src/Element/ContextualLinks.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\contextual\Element;
4
5 use Drupal\Component\Utility\Html;
6 use Drupal\Core\Render\Element\RenderElement;
7 use Drupal\Core\Url;
8
9 /**
10 * Provides a contextual_links element.
11 *
12 * @RenderElement("contextual_links")
13 */
14 class ContextualLinks extends RenderElement {
15
16 /**
17 * {@inheritdoc}
18 */
19 public function getInfo() {
20 $class = get_class($this);
21 return [
22 '#pre_render' => [
23 [$class, 'preRenderLinks'],
24 ],
25 '#theme' => 'links__contextual',
26 '#links' => [],
27 '#attributes' => ['class' => ['contextual-links']],
28 '#attached' => [
29 'library' => [
30 'contextual/drupal.contextual-links',
31 ],
32 ],
33 ];
34 }
35
36 /**
37 * Pre-render callback: Builds a renderable array for contextual links.
38 *
39 * @param array $element
40 * A renderable array containing a #contextual_links property, which is a
41 * keyed array. Each key is the name of the group of contextual links to
42 * render (based on the 'group' key in the *.links.contextual.yml files for
43 * all enabled modules). The value contains an associative array containing
44 * the following keys:
45 * - route_parameters: The route parameters passed to the url generator.
46 * - metadata: Any additional data needed in order to alter the link.
47 * @code
48 * array('#contextual_links' => array(
49 * 'block' => array(
50 * 'route_parameters' => array('block' => 'system.menu-tools'),
51 * ),
52 * 'menu' => array(
53 * 'route_parameters' => array('menu' => 'tools'),
54 * ),
55 * ))
56 * @endcode
57 *
58 * @return array
59 * A renderable array representing contextual links.
60 */
61 public static function preRenderLinks(array $element) {
62 // Retrieve contextual menu links.
63 $items = [];
64
65 $contextual_links_manager = static::contextualLinkManager();
66
67 foreach ($element['#contextual_links'] as $group => $args) {
68 $args += [
69 'route_parameters' => [],
70 'metadata' => [],
71 ];
72 $items += $contextual_links_manager->getContextualLinksArrayByGroup($group, $args['route_parameters'], $args['metadata']);
73 }
74
75 // Transform contextual links into parameters suitable for links.html.twig.
76 $links = [];
77 foreach ($items as $class => $item) {
78 $class = Html::getClass($class);
79 $links[$class] = [
80 'title' => $item['title'],
81 'url' => Url::fromRoute(isset($item['route_name']) ? $item['route_name'] : '', isset($item['route_parameters']) ? $item['route_parameters'] : [], $item['localized_options']),
82 ];
83 }
84 $element['#links'] = $links;
85
86 // Allow modules to alter the renderable contextual links element.
87 static::moduleHandler()->alter('contextual_links_view', $element, $items);
88
89 // If there are no links, tell drupal_render() to abort rendering.
90 if (empty($element['#links'])) {
91 $element['#printed'] = TRUE;
92 }
93
94 return $element;
95 }
96
97 /**
98 * Wraps the contextual link manager.
99 *
100 * @return \Drupal\Core\Menu\ContextualLinkManager
101 */
102 protected static function contextualLinkManager() {
103 return \Drupal::service('plugin.manager.menu.contextual_link');
104 }
105
106 /**
107 * Wraps the module handler.
108 *
109 * @return \Drupal\Core\Extension\ModuleHandlerInterface
110 */
111 protected static function moduleHandler() {
112 return \Drupal::moduleHandler();
113 }
114
115 }