Chris@0: [ Chris@0: [$class, 'preRenderLinks'], Chris@0: ], Chris@0: '#theme' => 'links__contextual', Chris@0: '#links' => [], Chris@0: '#attributes' => ['class' => ['contextual-links']], Chris@0: '#attached' => [ Chris@0: 'library' => [ Chris@0: 'contextual/drupal.contextual-links', Chris@0: ], Chris@0: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Pre-render callback: Builds a renderable array for contextual links. Chris@0: * Chris@0: * @param array $element Chris@0: * A renderable array containing a #contextual_links property, which is a Chris@0: * keyed array. Each key is the name of the group of contextual links to Chris@0: * render (based on the 'group' key in the *.links.contextual.yml files for Chris@0: * all enabled modules). The value contains an associative array containing Chris@0: * the following keys: Chris@0: * - route_parameters: The route parameters passed to the url generator. Chris@0: * - metadata: Any additional data needed in order to alter the link. Chris@0: * @code Chris@0: * array('#contextual_links' => array( Chris@0: * 'block' => array( Chris@0: * 'route_parameters' => array('block' => 'system.menu-tools'), Chris@0: * ), Chris@0: * 'menu' => array( Chris@0: * 'route_parameters' => array('menu' => 'tools'), Chris@0: * ), Chris@0: * )) Chris@0: * @endcode Chris@0: * Chris@0: * @return array Chris@0: * A renderable array representing contextual links. Chris@0: */ Chris@0: public static function preRenderLinks(array $element) { Chris@0: // Retrieve contextual menu links. Chris@0: $items = []; Chris@0: Chris@0: $contextual_links_manager = static::contextualLinkManager(); Chris@0: Chris@0: foreach ($element['#contextual_links'] as $group => $args) { Chris@0: $args += [ Chris@0: 'route_parameters' => [], Chris@0: 'metadata' => [], Chris@0: ]; Chris@0: $items += $contextual_links_manager->getContextualLinksArrayByGroup($group, $args['route_parameters'], $args['metadata']); Chris@0: } Chris@0: Chris@0: // Transform contextual links into parameters suitable for links.html.twig. Chris@0: $links = []; Chris@0: foreach ($items as $class => $item) { Chris@0: $class = Html::getClass($class); Chris@0: $links[$class] = [ Chris@0: 'title' => $item['title'], Chris@0: 'url' => Url::fromRoute(isset($item['route_name']) ? $item['route_name'] : '', isset($item['route_parameters']) ? $item['route_parameters'] : [], $item['localized_options']), Chris@0: ]; Chris@0: } Chris@0: $element['#links'] = $links; Chris@0: Chris@0: // Allow modules to alter the renderable contextual links element. Chris@0: static::moduleHandler()->alter('contextual_links_view', $element, $items); Chris@0: Chris@0: // If there are no links, tell drupal_render() to abort rendering. Chris@0: if (empty($element['#links'])) { Chris@0: $element['#printed'] = TRUE; Chris@0: } Chris@0: Chris@0: return $element; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps the contextual link manager. Chris@0: * Chris@0: * @return \Drupal\Core\Menu\ContextualLinkManager Chris@0: */ Chris@0: protected static function contextualLinkManager() { Chris@0: return \Drupal::service('plugin.manager.menu.contextual_link'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Wraps the module handler. Chris@0: * Chris@0: * @return \Drupal\Core\Extension\ModuleHandlerInterface Chris@0: */ Chris@0: protected static function moduleHandler() { Chris@0: return \Drupal::moduleHandler(); Chris@0: } Chris@0: Chris@0: }