diff --git a/core/lib/Drupal/Core/Menu/LocalTaskDerivativeBase.php b/core/lib/Drupal/Core/Menu/LocalTaskDerivativeBase.php index da6aca3..179817d 100644 --- a/core/lib/Drupal/Core/Menu/LocalTaskDerivativeBase.php +++ b/core/lib/Drupal/Core/Menu/LocalTaskDerivativeBase.php @@ -7,6 +7,34 @@ namespace Drupal\Core\Menu; -class LocalTaskDerivativeBase { +use Drupal\Component\Plugin\Derivative\DerivativeBase; -} +/** + * Provides a getPluginIdFromRoute method for local task derivatives. + */ +class LocalTaskDerivativeBase extends DerivativeBase { + + /** + * Finds the local task ID of a route given the route name. + * + * @param string $route_name + * The route name. + * @param array $local_tasks + * An array of all local task definitions. + * + * @return string|null + * Returns the local task ID of the given route or NULL if none is found. + */ + protected function getPluginIdFromRoute($route_name, &$local_tasks) { + $local_task_id = NULL; + foreach ($local_tasks as $plugin_id => $local_task) { + if ($local_task['route_name'] == $route_name) { + $local_task_id = $plugin_id; + break; + } + } + + return $local_task_id; + } + +} diff --git a/core/modules/comment/comment.module b/core/modules/comment/comment.module index 7bbf934..d889a73 100644 --- a/core/modules/comment/comment.module +++ b/core/modules/comment/comment.module @@ -227,6 +227,8 @@ function comment_menu_alter(&$items) { /** * Returns a menu title which includes the number of unapproved comments. + * + * @todo Move to the comment manager and replace by a entity query? */ function comment_count_unpublished() { $count = db_query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array( diff --git a/core/modules/comment/lib/Drupal/comment/Plugin/Menu/LocalTask/UnapprovedComments.php b/core/modules/comment/lib/Drupal/comment/Plugin/Menu/LocalTask/UnapprovedComments.php index a92fe71..35d0033 100644 --- a/core/modules/comment/lib/Drupal/comment/Plugin/Menu/LocalTask/UnapprovedComments.php +++ b/core/modules/comment/lib/Drupal/comment/Plugin/Menu/LocalTask/UnapprovedComments.php @@ -7,61 +7,18 @@ namespace Drupal\comment\Plugin\Menu\LocalTask; -use Drupal\Core\Database\Connection; use Drupal\Core\Menu\LocalTaskDefault; -use Drupal\Core\Plugin\ContainerFactoryPluginInterface; -use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides a local task that shows the amount of unapproved comments */ -class UnapprovedComments extends LocalTaskDefault implements ContainerFactoryPluginInterface { - - /** - * The database connection. - * - * @var \Drupal\Core\Database\Connection - */ - protected $database; - - /** - * Constructs a new UnapprovedComments instance. - * - * @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\Database\Connection $database - * The database connection. - */ - public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $database) { - parent::__construct($configuration, $plugin_id, $plugin_definition); - - $this->database = $database; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) { - return new static( - $configuration, - $plugin_id, - $plugin_definition, - $container->get('database') - ); - } +class UnapprovedComments extends LocalTaskDefault { /** * {@inheritdoc} */ public function getTitle() { - $count = $this->database->query('SELECT COUNT(cid) FROM {comment} WHERE status = :status', array( - ':status' => COMMENT_NOT_PUBLISHED, - ))->fetchField(); - return $this->t('Unapproved comments (@count)', array('@count' => $count)); + return comment_count_unpublished(); } } diff --git a/core/modules/config_translation/lib/Drupal/config_translation/Plugin/Derivative/ConfigTranslationLocalTasks.php b/core/modules/config_translation/lib/Drupal/config_translation/Plugin/Derivative/ConfigTranslationLocalTasks.php index 7ee6e6e..c7ccba3 100644 --- a/core/modules/config_translation/lib/Drupal/config_translation/Plugin/Derivative/ConfigTranslationLocalTasks.php +++ b/core/modules/config_translation/lib/Drupal/config_translation/Plugin/Derivative/ConfigTranslationLocalTasks.php @@ -8,15 +8,14 @@ namespace Drupal\config_translation\Plugin\Derivative; use Drupal\config_translation\ConfigMapperManagerInterface; -use Drupal\Component\Plugin\Derivative\DerivativeBase; -use Drupal\Component\Utility\Unicode; +use Drupal\Core\Menu\LocalTaskDerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides dynamic local tasks for config translation. */ -class ConfigTranslationLocalTasks extends DerivativeBase implements ContainerDerivativeInterface { +class ConfigTranslationLocalTasks extends LocalTaskDerivativeBase implements ContainerDerivativeInterface { /** * The mapper plugin discovery service. @@ -80,7 +79,7 @@ public function alterLocalTasks(array &$local_tasks) { /** @var \Drupal\config_translation\ConfigMapperInterface $mapper */ $route_name = $mapper->getOverviewRouteName(); $translation_tab = $this->basePluginId . ':' . $route_name; - $tab_root_id = $this->getTaskFromRoute($mapper->getBaseRouteName(), $local_tasks); + $tab_root_id = $this->getPluginIdFromRoute($mapper->getBaseRouteName(), $local_tasks); if (!empty($tab_root_id)) { $local_tasks[$translation_tab]['tab_root_id'] = $tab_root_id; } @@ -90,27 +89,5 @@ public function alterLocalTasks(array &$local_tasks) { } } - /** - * Find the local task ID of the parent route given the route name. - * - * @param string $route_name - * The route name of the parent local task. - * @param array $local_tasks - * An array of all local task definitions. - * - * @return bool|string - * Returns the local task ID of the parent task, otherwise return FALSE. - */ - protected function getTaskFromRoute($route_name, array &$local_tasks) { - $root_local_task = FALSE; - foreach ($local_tasks as $plugin_id => $local_task) { - if ($local_task['route_name'] == $route_name) { - $root_local_task = $plugin_id; - break; - } - } - - return $root_local_task; - } } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php index 0d6e1ba..e906fd9 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationLocalTasks.php @@ -7,15 +7,15 @@ namespace Drupal\content_translation\Plugin\Derivative; -use Drupal\Component\Plugin\Derivative\DerivativeBase; use Drupal\content_translation\ContentTranslationManagerInterface; +use Drupal\Core\Menu\LocalTaskDerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Provides dynamic local tasks for content translation. */ -class ContentTranslationLocalTasks extends DerivativeBase implements ContainerDerivativeInterface { +class ContentTranslationLocalTasks extends LocalTaskDerivativeBase implements ContainerDerivativeInterface { /** * The base plugin ID @@ -84,31 +84,8 @@ public function alterLocalTasks(array &$local_tasks) { $translation_route_name = $entity_info['links']['drupal:content-translation-overview']; $translation_tab = $this->basePluginId . ':' . $translation_route_name; - $local_tasks[$translation_tab]['tab_root_id'] = $this->getTaskFromRoute($entity_route_name, $local_tasks); + $local_tasks[$translation_tab]['tab_root_id'] = $this->getPluginIdFromRoute($entity_route_name, $local_tasks); } } - /** - * Find the local task ID of the parent route given the route name. - * - * @param string $route_name - * The route name of the parent local task. - * @param array $local_tasks - * An array of all local task definitions. - * - * @return bool|string - * Returns the local task ID of the parent task, otherwise return FALSE. - */ - protected function getTaskFromRoute($route_name, &$local_tasks) { - $parent_local_task = FALSE; - foreach ($local_tasks as $plugin_id => $local_task) { - if ($local_task['route_name'] == $route_name) { - $parent_local_task = $plugin_id; - break; - } - } - - return $parent_local_task; - } - } diff --git a/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php b/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php index a75a495..9a7c208 100644 --- a/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php +++ b/core/modules/field_ui/lib/Drupal/field_ui/Plugin/Derivative/FieldUiLocalTask.php @@ -7,8 +7,8 @@ namespace Drupal\field_ui\Plugin\Derivative; -use Drupal\Component\Plugin\Derivative\DerivativeBase; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Menu\LocalTaskDerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\Core\StringTranslation\TranslationInterface; @@ -17,7 +17,7 @@ /** * Provides local task definitions for all entity bundles. */ -class FieldUiLocalTask extends DerivativeBase implements ContainerDerivativeInterface { +class FieldUiLocalTask extends LocalTaskDerivativeBase implements ContainerDerivativeInterface { /** * The route provider. @@ -200,29 +200,6 @@ public function alterLocalTasks(&$local_tasks) { } /** - * Finds the local task ID of a route given the route name. - * - * @param string $route_name - * The route name. - * @param array $local_tasks - * An array of all local task definitions. - * - * @return string|null - * Returns the local task ID of the given route or NULL if none is found. - */ - protected function getPluginIdFromRoute($route_name, &$local_tasks) { - $local_task_id = NULL; - foreach ($local_tasks as $plugin_id => $local_task) { - if ($local_task['route_name'] == $route_name) { - $local_task_id = $plugin_id; - break; - } - } - - return $local_task_id; - } - - /** * Translates a string to the current language or to a given language. * * See the t() documentation for details. diff --git a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsLocalTask.php b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsLocalTask.php index c97e8e4..8dcf3da 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsLocalTask.php +++ b/core/modules/views/lib/Drupal/views/Plugin/Derivative/ViewsLocalTask.php @@ -7,8 +7,8 @@ namespace Drupal\views\Plugin\Derivative; -use Drupal\Component\Plugin\Derivative\DerivativeBase; use Drupal\Core\KeyValueStore\KeyValueStoreInterface; +use Drupal\Core\Menu\LocalTaskDerivativeBase; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Drupal\Core\Routing\RouteProviderInterface; use Drupal\views\Views; @@ -17,7 +17,7 @@ /** * Provides local task definitions for all views configured as local tasks. */ -class ViewsLocalTask extends DerivativeBase implements ContainerDerivativeInterface { +class ViewsLocalTask extends LocalTaskDerivativeBase implements ContainerDerivativeInterface { /** * The route provider. @@ -140,29 +140,6 @@ public function alterLocalTasks(&$local_tasks) { } /** - * Finds the local task ID of a route given the route name. - * - * @param string $route_name - * The route name. - * @param array $local_tasks - * An array of all local task definitions. - * - * @return string|null - * Returns the local task ID of the given route or NULL if none is found. - */ - protected function getPluginIdFromRoute($route_name, &$local_tasks) { - $local_task_id = NULL; - foreach ($local_tasks as $plugin_id => $local_task) { - if ($local_task['route_name'] == $route_name) { - $local_task_id = $plugin_id; - break; - } - } - - return $local_task_id; - } - - /** * Return a list of all views and display IDs that have a menu entry. * * @return array