Browse Source

Added drupal_menu twig function.

8.x-1.x
Chi 8 years ago
parent
commit
15626732ea
  1. 60
      src/TwigExtension.php
  2. 12
      tests/src/Functional/TwigTweakTest.php
  3. 7
      tests/twig_tweak_test/config/install/system.menu.twig-tweak-test.yml
  4. 3
      tests/twig_tweak_test/templates/twig-tweak-test.html.twig
  5. 16
      twig_tweak.links.menu.yml
  6. 2
      twig_tweak.services.yml

60
src/TwigExtension.php

@ -4,6 +4,8 @@ namespace Drupal\twig_tweak;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuActiveTrailInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Site\Settings;
use Drupal\Core\Utility\Token;
@ -42,6 +44,20 @@ class TwigExtension extends \Twig_Extension {
*/
protected $routeMatch;
/**
* The menu link tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected $menuTree;
/**
* The active menu trail service.
*
* @var \Drupal\Core\Menu\MenuActiveTrailInterface
*/
protected $menuActiveTrail;
/**
* TwigExtension constructor.
*
@ -53,12 +69,18 @@ class TwigExtension extends \Twig_Extension {
* The configuration factory.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menu_tree
* The menu tree service.
* @param \Drupal\Core\Menu\MenuActiveTrailInterface $menu_active_trail
* The active menu trail service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, Token $token, ConfigFactoryInterface $config_factory, RouteMatchInterface $route_match, MenuLinkTreeInterface $menu_tree, MenuActiveTrailInterface $menu_active_trail) {
$this->entityTypeManager = $entity_type_manager;
$this->token = $token;
$this->configFactory = $config_factory;
$this->routeMatch = $route_match;
$this->menuTree = $menu_tree;
$this->menuActiveTrail = $menu_active_trail;
}
/**
@ -71,6 +93,7 @@ class TwigExtension extends \Twig_Extension {
new \Twig_SimpleFunction('drupal_token', [$this, 'drupalToken']),
new \Twig_SimpleFunction('drupal_entity', [$this, 'drupalEntity']),
new \Twig_SimpleFunction('drupal_field', [$this, 'drupalField']),
new \Twig_SimpleFunction('drupal_menu', [$this, 'drupalMenu']),
new \Twig_SimpleFunction('drupal_config', [$this, 'drupalConfig']),
];
}
@ -178,6 +201,41 @@ class TwigExtension extends \Twig_Extension {
return NULL;
}
/**
* Returns the render array for Drupal menu.
*
* @param string $menu_name
* The name of the menu.
* @param int $level
* (optional) Initial menu level.
* @param int $depth
* (optional) Maximum number of menu levels to display.
*
* @return array
* A render array for the menu.
*/
public function drupalMenu($menu_name, $level = 1, $depth = 0) {
$parameters = $this->menuTree->getCurrentRouteMenuTreeParameters($menu_name);
// Adjust the menu tree parameters based on the block's configuration.
$parameters->setMinDepth($level);
// When the depth is configured to zero, there is no depth limit. When depth
// is non-zero, it indicates the number of levels that must be displayed.
// Hence this is a relative depth that we must convert to an actual
// (absolute) depth, that may never exceed the maximum depth.
if ($depth > 0) {
$parameters->setMaxDepth(min($level + $depth - 1, $this->menuTree->maxDepth()));
}
$tree = $this->menuTree->load($menu_name, $parameters);
$manipulators = [
['callable' => 'menu.default_tree_manipulators:checkAccess'],
['callable' => 'menu.default_tree_manipulators:generateIndexAndSort'],
];
$tree = $this->menuTree->transform($tree, $manipulators);
return $this->menuTree->build($tree);
}
/**
* Gets data from this configuration.
*

12
tests/src/Functional/TwigTweakTest.php

@ -88,6 +88,18 @@ class TwigTweakTest extends BrowserTestBase {
$xpath = '//div[@class = "tt-field"]/div[contains(@class, "field--name-body")]/p[. != ""]';
$this->assertByXpath($xpath);
// Test menu (default).
$xpath = '//div[@class = "tt-menu-default"]/ul[@class = "menu"]/li/a[. = "Link 1"]/../ul[@class = "menu"]/li/ul[@class = "menu"]/li/a[. = "Link 3"]';
$this->assertByXpath($xpath);
// Test menu (level).
$xpath = '//div[@class = "tt-menu-level"]/ul[@class = "menu"]/li/a[. = "Link 2"]/../ul[@class = "menu"]/li/a[. = "Link 3"]';
$this->assertByXpath($xpath);
// Test menu (depth).
$xpath = '//div[@class = "tt-menu-depth"]/ul[@class = "menu"]/li[not(ul)]/a[. = "Link 1"]';
$this->assertByXpath($xpath);
// Test block.
$xpath = '//div[@class = "tt-block"]';
$xpath .= '/div[@id="block-powered-by-drupal"]/span[contains(., "Powered by Drupal")]';

7
tests/twig_tweak_test/config/install/system.menu.twig-tweak-test.yml

@ -0,0 +1,7 @@
langcode: en
status: true
dependencies: {}
id: twig-tweak-test
label: Twig tweak test
description: ''
locked: false

3
tests/twig_tweak_test/templates/twig-tweak-test.html.twig

@ -6,6 +6,9 @@
<div class="tt-entity-teaser">{{ drupal_entity('node', 1, 'teaser') }}</div>
<div class="tt-entity-from-url">{{ drupal_entity('node') }}</div>
<div class="tt-field">{{ drupal_field('body', 'node', 1) }}</div>
<div class="tt-menu-default">{{ drupal_menu('twig-tweak-test') }}</div>
<div class="tt-menu-level">{{ drupal_menu('twig-tweak-test', 2) }}</div>
<div class="tt-menu-depth">{{ drupal_menu('twig-tweak-test', 1, 1) }}</div>
<div class="tt-block">{{ drupal_block('powered_by_drupal') }}</div>
<div class="tt-token">{{ drupal_token('site:name') }}</div>
<div class="tt-config">{{ drupal_config('user.settings', 'anonymous') }}</div>

16
twig_tweak.links.menu.yml

@ -0,0 +1,16 @@
twig_tweak.link_1:
title: Link 1
menu_name: twig-tweak-test
route_name: <front>
expanded: true
twig_tweak.link_2:
title: Link 2
menu_name: twig-tweak-test
route_name: <front>
parent: twig_tweak.link_1
expanded: true
twig_tweak.link_3:
title: Link 3
menu_name: twig-tweak-test
route_name: <front>
parent: twig_tweak.link_2

2
twig_tweak.services.yml

@ -1,6 +1,6 @@
services:
twig_tweak.twig_extension:
class: Drupal\twig_tweak\TwigExtension
arguments: ['@entity_type.manager', '@token', '@config.factory', '@current_route_match']
arguments: ['@entity_type.manager', '@token', '@config.factory', '@current_route_match', '@menu.link_tree', '@menu.active_trail']
tags:
- { name: twig.extension }

Loading…
Cancel
Save