diff --git a/core/core.services.yml b/core/core.services.yml index 65882af..e4d2ece 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -176,7 +176,7 @@ services: arguments: ['@container.namespaces', '@controller_resolver', '@request', '@module_handler', '@cache.cache', '@language_manager'] plugin.manager.menu.local_task: class: Drupal\Core\Menu\LocalTaskManager - arguments: ['@container.namespaces', '@controller_resolver', '@request', '@router.route_provider', '@module_handler', '@cache.cache', '@language_manager'] + arguments: ['@controller_resolver', '@request', '@router.route_provider', '@module_handler', '@cache.cache', '@language_manager'] request: class: Symfony\Component\HttpFoundation\Request # @TODO the synthetic setting must be uncommented whenever drupal_session_initialize() diff --git a/core/lib/Drupal/Core/Annotation/Menu/LocalTask.php b/core/lib/Drupal/Core/Annotation/Menu/LocalTask.php deleted file mode 100644 index 52ef6a1..0000000 --- a/core/lib/Drupal/Core/Annotation/Menu/LocalTask.php +++ /dev/null @@ -1,70 +0,0 @@ -t = $string_translation; + public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslationInterface $string_translation, UrlGeneratorInterface $generator) { + $this->stringTranslation = $string_translation; $this->generator = $generator; parent::__construct($configuration, $plugin_id, $plugin_definition); } @@ -74,6 +73,15 @@ public static function create(ContainerInterface $container, array $configuratio } /** + * Translates a string to the current language or to a given language. + * + * See the t() documentation for details. + */ + protected function t($string, array $args = array(), array $options = array()) { + return $this->stringTranslation->translate($string, $args, $options); + } + + /** * {@inheritdoc} */ public function getRouteName() { @@ -85,16 +93,17 @@ public function getRouteName() { */ public function getTitle() { // Subclasses may pull in the request or specific attributes as parameters. - return $this->pluginDefinition['title']; + return $this->t($this->pluginDefinition['title']); } /** * {@inheritdoc} + * + * @todo update based on https://drupal.org/node/2045267 */ public function getPath() { // Subclasses may set a request into the generator or use any desired method // to generate the path. - // @todo - use the new method from https://drupal.org/node/2031353 $path = $this->generator->generate($this->getRouteName()); // In order to get the Drupal path the base URL has to be stripped off. $base_url = $this->generator->getContext()->getBaseUrl(); diff --git a/core/lib/Drupal/Core/Menu/LocalTaskManager.php b/core/lib/Drupal/Core/Menu/LocalTaskManager.php index 17b5128..0dad7f3 100644 --- a/core/lib/Drupal/Core/Menu/LocalTaskManager.php +++ b/core/lib/Drupal/Core/Menu/LocalTaskManager.php @@ -7,11 +7,15 @@ namespace Drupal\Core\Menu; +use Drupal\Component\Plugin\Exception\PluginException; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageManager; use Drupal\Core\Plugin\DefaultPluginManager; +use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator; +use Drupal\Core\Plugin\Discovery\YamlDiscovery; +use Drupal\Core\Plugin\Factory\ContainerFactory; use Drupal\Core\Routing\RouteProviderInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface; @@ -26,6 +30,24 @@ class LocalTaskManager extends DefaultPluginManager { /** + * {@inheritdoc} + */ + protected $defaults = array( + // The static title for the local task. + 'title' => '', + // The plugin ID of the root tab. + 'tab_root_id' => '', + // The plugin ID of the parent tab (or NULL for the top-level tab). + 'tab_parent_id' => NULL, + // The weight of the tab. + 'weight' => 0, + // The default link options. + 'options' => array(), + // Default class for local task implementations. + 'class' => 'Drupal\Core\Menu\LocalTaskDefault', + ); + + /** * A controller resolver object. * * @var \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface @@ -72,8 +94,10 @@ class LocalTaskManager extends DefaultPluginManager { * @param \Drupal\Core\Language\LanguageManager $language_manager * The language manager. */ - public function __construct(\Traversable $namespaces, ControllerResolverInterface $controller_resolver, Request $request, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager) { - parent::__construct('Plugin/Menu/LocalTask', $namespaces, array(), 'Drupal\Core\Annotation\Menu\LocalTask'); + public function __construct(ControllerResolverInterface $controller_resolver, Request $request, RouteProviderInterface $route_provider, ModuleHandlerInterface $module_handler, CacheBackendInterface $cache, LanguageManager $language_manager) { + $this->discovery = new YamlDiscovery('local_tasks', array_values($module_handler->getModuleDirectories())); + $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); + $this->factory = new ContainerFactory($this); $this->controllerResolver = $controller_resolver; $this->request = $request; $this->routeProvider = $route_provider; @@ -82,6 +106,17 @@ public function __construct(\Traversable $namespaces, ControllerResolverInterfac } /** + * {@inheritdoc} + */ + public function processDefinition(&$definition, $plugin_id) { + parent::processDefinition($definition, $plugin_id); + // If there is no route name, this is a broken definition. + if (empty($definition['route_name'])) { + throw new PluginException(sprintf('Plugin (%s) definition must include "route_name"', $plugin_id)); + } + } + + /** * Gets the title for a local task. * * @param \Drupal\Core\Menu\LocalTaskInterface $local_task diff --git a/core/modules/system/lib/Drupal/system/Tests/Menu/LocalTasksTest.php b/core/modules/system/lib/Drupal/system/Tests/Menu/LocalTasksTest.php index 1a32725..91d68f9 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Menu/LocalTasksTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Menu/LocalTasksTest.php @@ -165,6 +165,7 @@ public function testPluginLocalTask() { $this->assertLocalTasks(array( 'menu-local-task-test/tasks/settings/sub1', 'menu-local-task-test/tasks/settings/sub2', + 'menu-local-task-test/tasks/settings/sub3', ), 1); $result = $this->xpath('//ul[contains(@class, "tabs")]//a[contains(@class, "active")]'); @@ -176,12 +177,13 @@ public function testPluginLocalTask() { $this->assertLocalTasks(array( 'menu-local-task-test/tasks/settings/sub1', 'menu-local-task-test/tasks/settings/sub2', + 'menu-local-task-test/tasks/settings/sub3', ), 1); $result = $this->xpath('//ul[contains(@class, "tabs")]//a[contains(@class, "active")]'); $this->assertEqual(2, count($result), 'There are tabs active on both levels.'); $this->assertEqual('Settings', (string) $result[0], 'The settings tab is active.'); - $this->assertEqual('sub1', (string) $result[1], 'The sub1 tab is active.'); + $this->assertEqual('Dynamic title for TestTasksSettingsSub1', (string) $result[1], 'The sub1 tab is active.'); } } diff --git a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksEdit.php b/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksEdit.php deleted file mode 100644 index 40996c8..0000000 --- a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksEdit.php +++ /dev/null @@ -1,25 +0,0 @@ -t('Dynamic title for @class', array('@class' => 'TestTasksSettingsSub1')); + } } diff --git a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub2.php b/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub2.php deleted file mode 100644 index f3cedfe..0000000 --- a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub2.php +++ /dev/null @@ -1,25 +0,0 @@ - 'menu_local_task_test_tasks_view', 'class' => 'Drupal\menu_test\Plugin\Menu\MenuLocalTasksTestTasksView', ); + // Add the defaults from the LocalTaskManager. + foreach ($definitions as $id => &$info) { + $info += array( + 'title' => '', + 'tab_root_id' => '', + 'tab_parent_id' => NULL, + 'weight' => 0, + 'options' => array(), + 'class' => 'Drupal\Core\Menu\LocalTaskDefault', + ); + } return $definitions; }