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; - $this->generator = $generator; - parent::__construct($configuration, $plugin_id, $plugin_definition); - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('string_translation'), - $container->get('url_generator') - ); - } - - /** - * {@inheritdoc} - */ - public function getRouteName() { - return $this->pluginDefinition['route_name']; - } - - /** - * {@inheritdoc} - */ - public function getTitle() { - // Subclasses may pull in the request or specific attributes as parameters. - return $this->pluginDefinition['title']; - } - - /** - * {@inheritdoc} - */ - 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(); - if (!empty($base_url) && strpos($path, $base_url) === 0) { - $path = substr($path, strlen($base_url)); - } - return trim($path, '/'); - } - - /** - * Returns the weight of the local task. - * - * @return int - * The weight of the task. If not defined in the annotation returns 0 by - * default or -10 for the root tab. - */ - public function getWeight() { - // By default the weight is 0, or -10 for the root tab. - if (!isset($this->pluginDefinition['weight'])) { - if ($this->pluginDefinition['tab_root_id'] == $this->pluginDefinition['id']) { - $this->pluginDefinition['weight'] = -10; - } - else { - $this->pluginDefinition['weight'] = 0; - } - } - return (int) $this->pluginDefinition['weight']; - } - - /** - * {@inheritdoc} - */ - public function getOptions() { - $options = $this->pluginDefinition['options']; - if ($this->active) { - if (empty($options['attributes']['class']) || !in_array('active', $options['attributes']['class'])) { - $options['attributes']['class'][] = 'active'; - } - } - return (array) $options; - } - - /** - * {@inheritdoc} - */ - public function setActive($active = TRUE) { - $this->active = $active; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getActive() { - return $this->active; - } - -} diff --git a/core/lib/Drupal/Core/Menu/LocalTaskDefault.php b/core/lib/Drupal/Core/Menu/LocalTaskDefault.php index 57e32df..40f53ba 100644 --- a/core/lib/Drupal/Core/Menu/LocalTaskDefault.php +++ b/core/lib/Drupal/Core/Menu/LocalTaskDefault.php @@ -7,10 +7,144 @@ namespace Drupal\Core\Menu; - /** * Default object used for LocalTaskPlugins. */ -class LocalTaskDefault extends LocalTaskBase { +class LocalTaskDefault extends PluginBase implements LocalTaskInterface, ContainerFactoryPluginInterface{ + + /** + * String translation object. + * + * @var \Drupal\Core\StringTranslation\Translator\TranslatorInterface + */ + protected $t; + + /** + * URL generator object. + * + * @var \Symfony\Component\Routing\Generator\UrlGeneratorInterface + */ + protected $generator; + + /** + * TRUE if this plugin is forced active for options attributes. + * + * @var bool + */ + protected $active = FALSE; + + /** + * Constructs a \Drupal\system\Plugin\LocalTaskDefault object. + * + * @param array $configuration + * A configuration array containing information about the plugin instance. + * @param string $plugin_id + * The plugin_id for the plugin instance. + * @param array $plugin_definition + * The plugin implementation definition. + * @param \Drupal\Core\StringTranslation\Translator\TranslatorInterface $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; + $this->generator = $generator; + parent::__construct($configuration, $plugin_id, $plugin_definition); + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('string_translation'), + $container->get('url_generator') + ); + } + + /** + * {@inheritdoc} + */ + public function getRouteName() { + return $this->pluginDefinition['route_name']; + } + + /** + * {@inheritdoc} + */ + public function getTitle() { + // Subclasses may pull in the request or specific attributes as parameters. + return $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. + $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(); + if (!empty($base_url) && strpos($path, $base_url) === 0) { + $path = substr($path, strlen($base_url)); + } + return trim($path, '/'); + } + + /** + * Returns the weight of the local task. + * + * @return int + * The weight of the task. If not defined in the annotation returns 0 by + * default or -10 for the root tab. + */ + public function getWeight() { + // By default the weight is 0, or -10 for the root tab. + if (!isset($this->pluginDefinition['weight'])) { + if ($this->pluginDefinition['tab_root_id'] == $this->pluginDefinition['id']) { + $this->pluginDefinition['weight'] = -10; + } + else { + $this->pluginDefinition['weight'] = 0; + } + } + return (int) $this->pluginDefinition['weight']; + } + + /** + * {@inheritdoc} + */ + public function getOptions() { + $options = $this->pluginDefinition['options']; + if ($this->active) { + if (empty($options['attributes']['class']) || !in_array('active', $options['attributes']['class'])) { + $options['attributes']['class'][] = 'active'; + } + } + return (array) $options; + } + + /** + * {@inheritdoc} + */ + public function setActive($active = TRUE) { + $this->active = $active; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getActive() { + return $this->active; + } -} \ No newline at end of file +} diff --git a/core/lib/Drupal/Core/Menu/LocalTaskManager.php b/core/lib/Drupal/Core/Menu/LocalTaskManager.php index 791c639..da4add8 100644 --- a/core/lib/Drupal/Core/Menu/LocalTaskManager.php +++ b/core/lib/Drupal/Core/Menu/LocalTaskManager.php @@ -3,6 +3,8 @@ /** * @file * Contains \Drupal\Core\Menu\MenuLocalTaskManager. + * + * @todo caching/compiling, translations and altering. */ namespace Drupal\Core\Menu; @@ -28,9 +30,10 @@ class LocalTaskManager extends PluginManagerBase { protected $defaults = array( // The ID. - 'title' => '', - // The static title for the local task. 'id' => '', + // The static title for the local task. + 'title' => '', + // The route name. 'route_name' => '', // The plugin ID of the root tab. 'tab_root_id' => '', @@ -73,6 +76,13 @@ class LocalTaskManager extends PluginManagerBase { protected $routeProvider; /** + * The module handler to invoke the alter hook. + * + * @var \Drupal\Core\Extension\ModuleHandlerInterface + */ + protected $moduleHandler; + + /** * Constructs a \Drupal\Core\Menu\LocalTaskManager object. * * @param \Symfony\Component\HttpKernel\Controller\ControllerResolverInterface $controller_resolver @@ -89,9 +99,8 @@ public function __construct(ControllerResolverInterface $controller_resolver, Re $this->controllerResolver = $controller_resolver; $this->request = $request; $this->routeProvider = $route_provider; + $this->moduleHandler = $module_handler; - // @todo caching/compiling, translations and altering. - // $this->alterInfo($module_handler, 'local_tasks'); $this->discovery = new YamlDiscovery('local_tasks', array_values($module_handler->getModuleDirectories())); $this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery); $this->discovery = new ProcessDecorator($this->discovery, array($this, 'processDefinition')); diff --git a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub1.php b/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub1.php index eba9fc4..25f758c 100644 --- a/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub1.php +++ b/core/modules/system/tests/modules/menu_test/lib/Drupal/menu_test/Plugin/Menu/LocalTask/TestTasksSettingsSub1.php @@ -8,10 +8,10 @@ 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; -class TestTasksSettingsSub1 extends LocalTaskBase { +class TestTasksSettingsSub1 extends LocalTaskDefault { /** * {@inheritdoc}