diff --git a/core/core.services.yml b/core/core.services.yml index d2de794..ac59312 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -215,7 +215,7 @@ services: arguments: ['@controller_resolver', '@request', '@router.route_provider', '@router.builder', '@module_handler', '@cache.cache', '@language_manager', '@access_manager', '@current_user'] plugin.manager.menu.contextual_link: class: Drupal\Core\Menu\ContextualLinkManager - arguments: ['@controller_resolver', '@module_handler', '@cache.cache', '@language_manager', '@access_manager', '@current_user'] + arguments: ['@controller_resolver', '@module_handler', '@cache.cache', '@language_manager', '@access_manager', '@current_user', '@request_stack'] request: class: Symfony\Component\HttpFoundation\Request synthetic: true diff --git a/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php index 79c451f..96f2d32 100644 --- a/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php +++ b/core/lib/Drupal/Core/Menu/ContextualLinkDefault.php @@ -8,6 +8,7 @@ namespace Drupal\Core\Menu; use Drupal\Core\Plugin\PluginBase; +use Symfony\Component\HttpFoundation\Request; /** * Provides a common base implementation of a contextual link. @@ -17,12 +18,20 @@ class ContextualLinkDefault extends PluginBase implements ContextualLinkInterfac /** * {@inheritdoc} */ - public function getTitle() { + public function getTitle(Request $request = NULL) { $options = array(); if (!empty($this->pluginDefinition['title_context'])) { $options['context'] = $this->pluginDefinition['title_context']; } - return $this->t($this->pluginDefinition['title'], array(), $options); + $args = array(); + if ($request && ($raw_parameters = $request->attributes->get('_raw_variables'))) { + foreach ($raw_parameters->all() as $key => $value) { + $args['@' . $key] = $value; + $args['%' . $key] = $value; + $args['!' . $key] = $value; + } + } + return $this->t($this->pluginDefinition['title'], $args, $options); } /** diff --git a/core/lib/Drupal/Core/Menu/ContextualLinkManager.php b/core/lib/Drupal/Core/Menu/ContextualLinkManager.php index 5141f5c..9aa8b01 100644 --- a/core/lib/Drupal/Core/Menu/ContextualLinkManager.php +++ b/core/lib/Drupal/Core/Menu/ContextualLinkManager.php @@ -18,6 +18,7 @@ use Drupal\Core\Plugin\Discovery\YamlDiscovery; use Drupal\Core\Plugin\Factory\ContainerFactory; use Drupal\Core\Session\AccountInterface; +use Symfony\Component\HttpFoundation\RequestStack; /** * Defines a contextual link plugin manager to deal with contextual links. @@ -70,6 +71,13 @@ class ContextualLinkManager extends DefaultPluginManager implements ContextualLi protected $account; /** + * The request stack. + * + * @var \Symfony\Component\HttpFoundation\RequestStack + */ + protected $requestStack; + + /** * A static cache of all the contextual link plugins by group name. * * @var array @@ -91,8 +99,10 @@ class ContextualLinkManager extends DefaultPluginManager implements ContextualLi * The access manager. * @param \Drupal\Core\Session\AccountInterface $account * The current user. + * @param \Symfony\Component\HttpFoundation\RequestStack $request_stack + * The request stack. */ - public function __construct(ControllerResolverInterface $controller_resolver, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManager $language_manager, AccessManager $access_manager, AccountInterface $account) { + public function __construct(ControllerResolverInterface $controller_resolver, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, LanguageManager $language_manager, AccessManager $access_manager, AccountInterface $account, RequestStack $request_stack) { $this->discovery = new YamlDiscovery('contextual_links', $module_handler->getModuleDirectories()); $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); $this->factory = new ContainerFactory($this); @@ -100,6 +110,7 @@ public function __construct(ControllerResolverInterface $controller_resolver, Mo $this->controllerResolver = $controller_resolver; $this->accessManager = $access_manager; $this->account = $account; + $this->requestStack = $request_stack; $this->alterInfo($module_handler, 'contextual_links_plugins'); $this->setCacheBackend($cache_backend, $language_manager, 'contextual_links_plugins'); } @@ -149,6 +160,7 @@ public function getContextualLinkPluginsByGroup($group_name) { */ public function getContextualLinksArrayByGroup($group_name, array $route_parameters, array $metadata = array()) { $links = array(); + $request = $this->requestStack->getCurrentRequest(); foreach ($this->getContextualLinkPluginsByGroup($group_name) as $plugin_id => $plugin_definition) { /** @var $plugin \Drupal\Core\Menu\ContextualLinkInterface */ $plugin = $this->createInstance($plugin_id); @@ -162,7 +174,7 @@ public function getContextualLinksArrayByGroup($group_name, array $route_paramet $links[$plugin_id] = array( 'route_name' => $route_name, 'route_parameters' => $route_parameters, - 'title' => $plugin->getTitle(), + 'title' => $plugin->getTitle($request), 'weight' => $plugin->getWeight(), 'localized_options' => $plugin->getOptions(), 'metadata' => $metadata, diff --git a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php index e93b127..a165509 100644 --- a/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php +++ b/core/tests/Drupal/Tests/Core/Menu/ContextualLinkDefaultTest.php @@ -9,6 +9,8 @@ use Drupal\Core\Menu\ContextualLinkDefault; use Drupal\Tests\UnitTestCase; +use Symfony\Component\HttpFoundation\ParameterBag; +use Symfony\Component\HttpFoundation\Request; /** * Tests the contextual link default class. @@ -108,6 +110,23 @@ public function testGetTitleWithContext() { } /** + * Tests the getTitle method with a parameter. + */ + public function testGetTitleWithParameter() { + $this->pluginDefinition['title'] = 'Example @test'; + $this->stringTranslation->expects($this->once()) + ->method('translate') + ->with($this->pluginDefinition['title'], $this->arrayHasKey('@test'), array()) + ->will($this->returnValue('Example value')); + + $this->setupContextualLinkDefault(); + $raw_variables = new ParameterBag(array('test' => 'value', 'test2' => 'value2')); + $request = new Request(); + $request->attributes->set('_raw_variables', $raw_variables); + $this->assertEquals('Example value', $this->contextualLinkDefault->getTitle($request)); + } + + /** * Tests the getRouteName() method. * * @covers \Drupal\Core\Menu\ContextualLinkDefault::getRouteName()