You are here

public function ContextualLinkManager::getContextualLinksArrayByGroup in Drupal 10

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Menu/ContextualLinkManager.php \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()
  2. 9 core/lib/Drupal/Core/Menu/ContextualLinkManager.php \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup()

Gets the contextual links prepared as expected by links.html.twig.

Parameters

string $group_name: The group name.

array $route_parameters: The incoming route parameters. The route parameters need to have the same name on all contextual link routes, e.g. you cannot use 'node' and 'entity' in parallel.

array $metadata: Additional metadata of contextual links, like the position (optional).

Return value

array An array of link information, keyed by the plugin ID. Each entry is an associative array with the following keys:

  • route_name: The route name to link to.
  • route_parameters: The route parameters for the contextual link.
  • title: The title of the contextual link.
  • weight: The weight of the contextual link.
  • localized_options: The options of the link, which will be passed to the link generator.
  • metadata: The array of additional metadata that was passed in.

Overrides ContextualLinkManagerInterface::getContextualLinksArrayByGroup

File

core/lib/Drupal/Core/Menu/ContextualLinkManager.php, line 166

Class

ContextualLinkManager
Defines a contextual link plugin manager to deal with contextual links.

Namespace

Drupal\Core\Menu

Code

public function getContextualLinksArrayByGroup($group_name, array $route_parameters, array $metadata = []) {
  $links = [];
  $request = $this->requestStack
    ->getCurrentRequest();
  foreach ($this
    ->getContextualLinkPluginsByGroup($group_name) as $plugin_id => $plugin_definition) {

    /** @var \Drupal\Core\Menu\ContextualLinkInterface $plugin */
    $plugin = $this
      ->createInstance($plugin_id);
    $route_name = $plugin
      ->getRouteName();

    // Check access.
    if (!$this->accessManager
      ->checkNamedRoute($route_name, $route_parameters, $this->account)) {
      continue;
    }
    $links[$plugin_id] = [
      'route_name' => $route_name,
      'route_parameters' => $route_parameters,
      'title' => $plugin
        ->getTitle($request),
      'weight' => $plugin
        ->getWeight(),
      'localized_options' => $plugin
        ->getOptions(),
      'metadata' => $metadata,
    ];
  }
  $this->moduleHandler
    ->alter('contextual_links', $links, $group_name, $route_parameters);
  return $links;
}