Commit ef874408 authored by catch's avatar catch

Issue #2050919 by neclimdul, pwolanin, damiankloip, dawehner: Replace local...

Issue #2050919 by neclimdul, pwolanin, damiankloip, dawehner: Replace local task plugin discovery with YamlDiscovery.
parent 304da11c
......@@ -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()
......
<?php
/**
* @file
* Contains \Drupal\Core\Annotation\Menu\LocalTask.
*/
namespace Drupal\Core\Annotation\Menu;
use Drupal\Component\Annotation\Plugin;
/**
* Defines a local task plugin annotation object.
*
* @Annotation
*/
class LocalTask extends Plugin {
/**
* The ID.
*
* @var string
*/
public $id;
/**
* The static title for the local task.
*
* @ingroup plugin_translatable
*
* @var \Drupal\Core\Annotation\Translation
*/
public $title;
/**
* The route name.
*
* @var string
*/
public $route_name;
/**
* The plugin ID of the root tab.
*
* @var array
*/
public $tab_root_id;
/**
* The plugin ID of the parent tab (or NULL for a top-level tab).
*
* @var array|NULL
*/
public $tab_parent_id;
/**
* The weight of the tab.
*
* @var int|NULL
*/
public $weight;
/**
* The default link options.
*
* @var array (optional)
*/
public $options = array();
}
......@@ -2,28 +2,28 @@
/**
* @file
* Contains \Drupal\Core\Menu\LocalTaskBase.
* Contains \Drupal\Core\Menu\LocalTaskDefault.
*/
namespace Drupal\Core\Menu;
use Drupal\Component\Plugin\PluginBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\StringTranslation\Translator\TranslatorInterface;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
/**
* Provides defaults and base methods for menu local tasks plugins.
* Default object used for LocalTaskPlugins.
*/
abstract class LocalTaskBase extends PluginBase implements LocalTaskInterface, ContainerFactoryPluginInterface{
class LocalTaskDefault extends PluginBase implements LocalTaskInterface, ContainerFactoryPluginInterface {
/**
* String translation object.
*
* @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface
* @var \Drupal\Core\StringTranslation\TranslationInterface
*/
protected $t;
protected $stringTranslation;
/**
* URL generator object.
......@@ -40,7 +40,7 @@ abstract class LocalTaskBase extends PluginBase implements LocalTaskInterface, C
protected $active = FALSE;
/**
* Constructs a \Drupal\system\Plugin\LocalTaskBase object.
* Constructs a \Drupal\system\Plugin\LocalTaskDefault object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
......@@ -48,14 +48,13 @@ abstract class LocalTaskBase extends PluginBase implements LocalTaskInterface, C
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $string_translation
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* The string translation object.
* @param \Symfony\Component\Routing\Generator\UrlGeneratorInterface $generator
* The url generator object.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, TranslatorInterface $string_translation, UrlGeneratorInterface $generator) {
// This is available for subclasses that need to translate a dynamic title.
$this->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);
}
......@@ -73,6 +72,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}
*/
......@@ -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();
......
......@@ -7,11 +7,14 @@
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;
......@@ -25,6 +28,28 @@
*/
class LocalTaskManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = array(
// (required) The name of the route this task links to.
'route_name' => '',
// 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',
// The plugin id. Set by the plugin system and should never be provided in YAML.
'id' => '',
);
/**
* A controller resolver object.
*
......@@ -56,9 +81,6 @@ class LocalTaskManager extends DefaultPluginManager {
/**
* Constructs a \Drupal\Core\Menu\LocalTaskManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations,
* @param \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $controller_resolver
* An object to use in introspecting route methods.
* @param \Symfony\Component\HttpFoundation\Request $request
......@@ -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', $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;
......@@ -81,6 +105,17 @@ public function __construct(\Traversable $namespaces, ControllerResolverInterfac
$this->setCacheBackend($cache, $language_manager, 'local_task', array('local_task' => TRUE));
}
/**
* {@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.
*
......
......@@ -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,22 @@ 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.');
// Ensures that the local tasks contains the proper 'provider key'
$definitions = $this->container->get('plugin.manager.menu.local_task')->getDefinitions();
$this->assertEqual($definitions['menu_local_task_test_tasks_view']['provider'], 'menu_test');
$this->assertEqual($definitions['menu_local_task_test_tasks_edit']['provider'], 'menu_test');
$this->assertEqual($definitions['menu_local_task_test_tasks_settings']['provider'], 'menu_test');
$this->assertEqual($definitions['menu_local_task_test_tasks_settings_sub1']['provider'], 'menu_test');
$this->assertEqual($definitions['menu_local_task_test_tasks_settings_sub2']['provider'], 'menu_test');
$this->assertEqual($definitions['menu_local_task_test_tasks_settings_sub3']['provider'], 'menu_test');
}
}
<?php
/**
* @file
* Contains \Drupal\menu_test\Plugin\Menu\LocalTask\TestTasksEdit.
*/
namespace Drupal\menu_test\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Menu\LocalTask;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Translation;
/**
* @LocalTask(
* id = "menu_local_task_test_tasks_edit",
* route_name = "menu_local_task_test_tasks_edit",
* title = @Translation("Edit"),
* tab_root_id = "menu_local_task_test_tasks_view",
* weight = "10"
* )
*/
class TestTasksEdit extends LocalTaskBase {
}
<?php
/**
* @file
* Contains \Drupal\menu_test\Plugin\Menu\LocalTask\TestTasksSettings.
*/
namespace Drupal\menu_test\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Menu\LocalTask;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Translation;
/**
* @LocalTask(
* id = "menu_local_task_test_tasks_settings",
* route_name = "menu_local_task_test_tasks_settings",
* title = @Translation("Settings"),
* tab_root_id = "menu_local_task_test_tasks_view"
* )
*/
class TestTasksSettings extends LocalTaskBase {
}
......@@ -8,19 +8,16 @@
namespace Drupal\menu_test\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Menu\LocalTask;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Annotation\Translation;
/**
* @LocalTask(
* id = "menu_local_task_test_tasks_settings_sub1",
* route_name = "menu_local_task_test_tasks_settings_sub1",
* title = @Translation("sub1"),
* tab_root_id = "menu_local_task_test_tasks_view",
* tab_parent_id = "menu_local_task_test_tasks_settings",
* weight = "-10"
* )
*/
class TestTasksSettingsSub1 extends LocalTaskBase {
class TestTasksSettingsSub1 extends LocalTaskDefault {
/**
* {@inheritdoc}
*/
function getTitle() {
return $this->t('Dynamic title for @class', array('@class' => 'TestTasksSettingsSub1'));
}
}
<?php
/**
* @file
* Contains \Drupal\menu_test\Plugin\Menu\LocalTask\TestTasksSettingsSub2.
*/
namespace Drupal\menu_test\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Menu\LocalTask;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Translation;
/**
* @LocalTask(
* id = "menu_local_task_test_tasks_settings_sub2",
* route_name = "menu_local_task_test_tasks_settings_sub2",
* title = @Translation("sub2"),
* tab_root_id = "menu_local_task_test_tasks_view",
* tab_parent_id = "menu_local_task_test_tasks_settings"
* )
*/
class TestTasksSettingsSub2 extends LocalTaskBase {
}
<?php
/**
* @file
* Contains \Drupal\menu_test\Plugin\Menu\LocalTask\TestTasksView.
*/
namespace Drupal\menu_test\Plugin\Menu\LocalTask;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Menu\LocalTask;
use Drupal\Core\Annotation\Translation;
/**
* @LocalTask(
* id = "menu_local_task_test_tasks_view",
* route_name = "menu_local_task_test_tasks_view",
* title = @Translation("View"),
* tab_root_id = "menu_local_task_test_tasks_view"
* )
*/
class TestTasksView extends LocalTaskBase {
}
menu_local_task_test_tasks_view:
route_name: menu_local_task_test_tasks_view
title: View
tab_root_id: menu_local_task_test_tasks_view
menu_local_task_test_tasks_edit:
route_name: menu_local_task_test_tasks_edit
title: Edit
tab_root_id: menu_local_task_test_tasks_view
weight: 10
menu_local_task_test_tasks_settings:
route_name: menu_local_task_test_tasks_settings
title: Settings
tab_root_id: menu_local_task_test_tasks_view
menu_local_task_test_tasks_settings_sub1:
route_name: menu_local_task_test_tasks_settings_sub1
title: sub1
tab_root_id: menu_local_task_test_tasks_view
tab_parent_id: menu_local_task_test_tasks_settings
class: Drupal\menu_test\Plugin\Menu\LocalTask\TestTasksSettingsSub1
weight: -10
menu_local_task_test_tasks_settings_sub2:
route_name: menu_local_task_test_tasks_settings_sub2
title: sub2
tab_root_id: menu_local_task_test_tasks_view
tab_parent_id: menu_local_task_test_tasks_settings
menu_local_task_test_tasks_settings_sub3:
route_name: menu_local_task_test_tasks_settings_sub3
title: sub3
tab_root_id: menu_local_task_test_tasks_view
tab_parent_id: menu_local_task_test_tasks_settings
weight: 20
......@@ -108,6 +108,13 @@ menu_local_task_test_tasks_settings_sub2:
requirements:
_access: 'TRUE'
menu_local_task_test_tasks_settings_sub3:
pattern: '/menu-local-task-test/tasks/settings/sub3'
defaults:
_content: '\Drupal\menu_test\TestControllers::test1'
requirements:
_access: 'TRUE'
menu_test_optional_placeholder:
pattern: '/menu-test/optional/{placeholder}'
defaults:
......
<?php
/**
* @file
* Contains \Drupal\views_ui\Plugin\Menu\LocalTask\ViewsListTask.
*/
namespace Drupal\views_ui\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Menu\LocalTask;
/**
* @LocalTask(
* id = "views_ui_list_tab",
* route_name = "views_ui.list",
* title = @Translation("List"),
* tab_root_id = "views_ui_list_tab"
* )
*/
class ViewsListTask extends LocalTaskBase {
}
<?php
/**
* @file
* Contains \Drupal\views_ui\Plugin\Menu\LocalTask\ViewsSettingsAdvancedTas.
*/
namespace Drupal\views_ui\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Menu\LocalTask;
/**
* @LocalTask(
* id = "views_ui_settings_advanced_tab",
* route_name = "views_ui.settings.advanced",
* title = @Translation("Advanced"),
* tab_root_id = "views_ui_list_tab",
* tab_parent_id = "views_ui_settings_tab",
* weight = "10"
* )
*/
class ViewsSettingsAdvancedTask extends LocalTaskBase {
}
<?php
/**
* @file
* Contains \Drupal\views_ui\Plugin\Menu\LocalTask\ViewsSettingsBasicTask.
*/
namespace Drupal\views_ui\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Menu\LocalTask;
/**
* @LocalTask(
* id = "views_ui_settings_basic_tab",
* route_name = "views_ui.settings.basic",
* title = @Translation("Basic"),
* tab_root_id = "views_ui_list_tab",
* tab_parent_id = "views_ui_settings_tab"
* )
*/
class ViewsSettingsBasicTask extends LocalTaskBase {
}
<?php
/**
* @file
* Contains \Drupal\views_ui\Plugin\Menu\LocalTask\ViewsSettingsTask.
*/
namespace Drupal\views_ui\Plugin\Menu\LocalTask;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Menu\LocalTaskBase;
use Drupal\Core\Annotation\Menu\LocalTask;
/**
* @LocalTask(
* id = "views_ui_settings_tab",
* route_name = "views_ui.settings.basic",
* title = @Translation("Settings"),
* tab_root_id = "views_ui_list_tab"
* )
*/
class ViewsSettingsTask extends LocalTaskBase {
}
views_ui_settings_tab:
route_name: views_ui.settings.basic
title: Settings
tab_root_id: views_ui_list_tab
views_ui_settings_basic_tab:
route_name: views_ui.settings.basic
title: Basic
tab_root_id: views_ui_list_tab
tab_parent_id: views_ui_settings_tab
views_ui_settings_advanced_tab:
route_name: views_ui.settings.advanced
title: Advanced
tab_root_id: views_ui_list_tab
tab_parent_id: views_ui_settings_tab
weight: 10
views_ui_list_tab:
route_name: views_ui.list
title: List
tab_root_id: views_ui_list_tab
......@@ -7,10 +7,8 @@
namespace Drupal\Tests\Core\Menu;
use Drupal\Component\Plugin\Factory\DefaultFactory;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Language\Language;
use Drupal\system\Plugin\Type\MenuLocalTaskManager;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\Request;
use Zend\Stdlib\ArrayObject;
......@@ -18,7 +16,7 @@
/**
* Tests local tasks manager.
*
* @see \Drupal\system\Plugin\Type\MenuLocalTaskManager
* @see \Drupal\Core\Menu\LocalTaskManager
*/
class LocalTaskManagerTest extends UnitTestCase {
......@@ -293,6 +291,19 @@ protected function getLocalTaskFixtures() {
'tab_root_id' => '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(
'id' => '',
'route_name' => '',
'title' => '',
'tab_root_id' => '',
'tab_parent_id' => NULL,
'weight' => 0,
'options' => array(),
'class' => 'Drupal\Core\Menu\LocalTaskDefault',
);
}
return $definitions;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment