getContextualLinksArrayByGroup example

public static function preRenderLinks(array $element) {
    // Retrieve contextual menu links.     $items = [];

    $contextual_links_manager = static::contextualLinkManager();

    foreach ($element['#contextual_links'] as $group => $args) {
      $args += [
        'route_parameters' => [],
        'metadata' => [],
      ];
      $items += $contextual_links_manager->getContextualLinksArrayByGroup($group$args['route_parameters']$args['metadata']);
    }

    uasort($items[SortArray::class, 'sortByWeightElement']);

    // Transform contextual links into parameters suitable for links.html.twig.     $links = [];
    foreach ($items as $class => $item) {
      $class = Html::getClass($class);
      $links[$class] = [
        'title' => $item['title'],
        'url' => Url::fromRoute($item['route_name'] ?? '', $item['route_parameters'] ?? []$item['localized_options']),
      ];
$this->accessManager->expects($this->any())
      ->method('checkNamedRoute')
      ->willReturn(AccessResult::allowed());

    $this->moduleHandler->expects($this->exactly(2))
      ->method('alter')
      ->withConsecutive(
        ['contextual_links_plugins'],
        ['contextual_links', new Count(2), 'group1', ['key' => 'value']],
      );

    $result = $this->contextualLinkManager->getContextualLinksArrayByGroup('group1', ['key' => 'value']);
    $this->assertCount(2, $result);
    foreach (['test_plugin1', 'test_plugin2'] as $plugin_id) {
      $definition = $definitions[$plugin_id];
      $this->assertEquals($definition['weight']$result[$plugin_id]['weight']);
      $this->assertEquals($definition['title']$result[$plugin_id]['title']);
      $this->assertEquals($definition['route_name']$result[$plugin_id]['route_name']);
      $this->assertEquals($definition['options']$result[$plugin_id]['localized_options']);
    }
  }

  /** * Tests the access checking of the getContextualLinksArrayByGroup method. * * @see \Drupal\Core\Menu\ContextualLinkManager::getContextualLinksArrayByGroup() */
Home | Imprint | This part of the site doesn't use cookies.