annotate core/modules/contextual/src/Element/ContextualLinks.php @ 19:fa3358dc1485 tip

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