clean install

This commit is contained in:
2024-12-10 15:08:16 +01:00
commit e14eb2d8fd
31193 changed files with 3555714 additions and 0 deletions

View File

@@ -0,0 +1,174 @@
<?php
namespace Drupal\admin_toolbar_tools;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
/**
* Admin Toolbar Tools helper service.
*/
class AdminToolbarToolsHelper {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The local task manger.
*
* @var \Drupal\Core\Menu\LocalTaskManagerInterface
* The local task manager menu.
*/
protected $localTaskManager;
/**
* The route match interface.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
* The route match.
*/
protected $routeMatch;
/**
* The configuration factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Create an AdminToolbarToolsHelper object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Menu\LocalTaskManagerInterface $local_task_manager
* The local task manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* The configuration factory.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, LocalTaskManagerInterface $local_task_manager, RouteMatchInterface $route_match, ConfigFactoryInterface $config_factory) {
$this->entityTypeManager = $entity_type_manager;
$this->localTaskManager = $local_task_manager;
$this->routeMatch = $route_match;
$this->configFactory = $config_factory;
}
/**
* Generate the toolbar tab and item for primary local tasks.
*
* @return array
* The toolbar render array.
*/
public function buildLocalTasksToolbar() {
$build = [];
$config = $this->configFactory->get('admin_toolbar_tools.settings');
$cacheability = CacheableMetadata::createFromObject($config);
if ($config->get('show_local_tasks')) {
$local_tasks = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName());
$cacheability = $cacheability->merge($local_tasks['cacheability']);
$cacheability = $cacheability->merge(CacheableMetadata::createFromObject($this->localTaskManager));
if (!empty($local_tasks['tabs'])) {
$local_task_links = [
'#theme' => 'links',
'#links' => [],
'#attributes' => [
'class' => ['toolbar-menu'],
],
];
// Sort the links by weight.
Element::children($local_tasks['tabs'], TRUE);
// Only show the accessible local tasks.
foreach (Element::getVisibleChildren($local_tasks['tabs']) as $task) {
$local_task_links['#links'][$task] = $local_tasks['tabs'][$task]['#link'];
if (isset($local_tasks['tabs'][$task]['#active']) && $local_tasks['tabs'][$task]['#active']) {
$local_task_links['#links'][$task]['attributes']['class'][] = 'is-active';
}
}
$build = [
'#type' => 'toolbar_item',
'#wrapper_attributes' => [
'class' => ['local-tasks-toolbar-tab'],
],
// Put it after contextual toolbar item so when float right is applied
// local tasks item will be first.
'#weight' => 10,
'tab' => [
// We can't use #lazy_builder here because
// ToolbarItem::preRenderToolbarItem will insert #attributes before
// lazy_builder callback and this will produce Exception.
// This means that for now we always render Local Tasks item even
// when the tray is empty.
'#type' => 'link',
'#title' => $this->t('Local Tasks'),
'#url' => Url::fromRoute('<none>'),
'#attributes' => [
'class' => [
'toolbar-icon',
'toolbar-icon-local-tasks',
],
],
],
'tray' => [
'local_tasks' => $local_task_links,
],
'#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
];
}
}
$cacheability->applyTo($build);
return $build;
}
/**
* Gets a list of content entities.
*
* @return array
* An array of metadata about content entities.
*/
public function getBundleableEntitiesList() {
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
return $content_entities;
}
/**
* Gets an array of entity types that should trigger a menu rebuild.
*
* @return array
* An array of entity machine names.
*/
public function getRebuildEntityTypes() {
$types = ['menu'];
$content_entities = $this->getBundleableEntitiesList();
$types = array_merge($types, array_column($content_entities, 'content_entity_bundle'));
return $types;
}
}

View File

@@ -0,0 +1,296 @@
<?php
namespace Drupal\admin_toolbar_tools\Controller;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\CronInterface;
use Drupal\Core\Menu\ContextualLinkManager;
use Drupal\Core\Menu\LocalActionManager;
use Drupal\Core\Menu\LocalTaskManager;
use Drupal\Core\Menu\MenuLinkManagerInterface;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Template\TwigEnvironment;
use Drupal\Core\Theme\Registry;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Controller for AdminToolbar Tools.
*
* @package Drupal\admin_toolbar_tools\Controller
*/
class ToolbarController extends ControllerBase {
/**
* A cron instance.
*
* @var \Drupal\Core\CronInterface
*/
protected $cron;
/**
* A menu link manager instance.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* A context link manager instance.
*
* @var \Drupal\Core\Menu\ContextualLinkManager
*/
protected $contextualLinkManager;
/**
* A local task manager instance.
*
* @var \Drupal\Core\Menu\LocalTaskManager
*/
protected $localTaskLinkManager;
/**
* A local action manager instance.
*
* @var \Drupal\Core\Menu\LocalActionManager
*/
protected $localActionLinkManager;
/**
* A cache backend interface instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheRender;
/**
* A date time instance.
*
* @var \Drupal\Component\Datetime\TimeInterface
*/
protected $time;
/**
* A request stack symfony instance.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
*/
protected $requestStack;
/**
* A plugin cache clear instance.
*
* @var \Drupal\Core\Plugin\CachedDiscoveryClearerInterface
*/
protected $pluginCacheClearer;
/**
* The cache menu instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheMenu;
/**
* A TwigEnvironment instance.
*
* @var \Drupal\Core\Template\TwigEnvironment
*/
protected $twig;
/**
* The search theme.registry service.
*
* @var \Drupal\Core\Theme\Registry
*/
protected $themeRegistry;
/**
* Constructs a ToolbarController object.
*
* @param \Drupal\Core\CronInterface $cron
* A cron instance.
* @param \Drupal\Core\Menu\MenuLinkManagerInterface $menuLinkManager
* A menu link manager instance.
* @param \Drupal\Core\Menu\ContextualLinkManager $contextualLinkManager
* A context link manager instance.
* @param \Drupal\Core\Menu\LocalTaskManager $localTaskLinkManager
* A local task manager instance.
* @param \Drupal\Core\Menu\LocalActionManager $localActionLinkManager
* A local action manager instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cacheRender
* A cache backend interface instance.
* @param \Drupal\Component\Datetime\TimeInterface $time
* A date time instance.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* A request stack symfony instance.
* @param \Drupal\Core\Plugin\CachedDiscoveryClearerInterface $plugin_cache_clearer
* A plugin cache clear instance.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_menu
* A cache menu instance.
* @param \Drupal\Core\Template\TwigEnvironment $twig
* A TwigEnvironment instance.
* @param \Drupal\Core\Theme\Registry $theme_registry
* The theme.registry service.
*/
public function __construct(
CronInterface $cron,
MenuLinkManagerInterface $menuLinkManager,
ContextualLinkManager $contextualLinkManager,
LocalTaskManager $localTaskLinkManager,
LocalActionManager $localActionLinkManager,
CacheBackendInterface $cacheRender,
TimeInterface $time,
RequestStack $request_stack,
CachedDiscoveryClearerInterface $plugin_cache_clearer,
CacheBackendInterface $cache_menu,
TwigEnvironment $twig,
// phpcs:ignore Drupal.Functions.MultiLineFunctionDeclaration.MissingTrailingComma
Registry $theme_registry
) {
$this->cron = $cron;
$this->menuLinkManager = $menuLinkManager;
$this->contextualLinkManager = $contextualLinkManager;
$this->localTaskLinkManager = $localTaskLinkManager;
$this->localActionLinkManager = $localActionLinkManager;
$this->cacheRender = $cacheRender;
$this->time = $time;
$this->requestStack = $request_stack;
$this->pluginCacheClearer = $plugin_cache_clearer;
$this->cacheMenu = $cache_menu;
$this->twig = $twig;
$this->themeRegistry = $theme_registry;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('cron'),
$container->get('plugin.manager.menu.link'),
$container->get('plugin.manager.menu.contextual_link'),
$container->get('plugin.manager.menu.local_task'),
$container->get('plugin.manager.menu.local_action'),
$container->get('cache.render'),
$container->get('datetime.time'),
$container->get('request_stack'),
$container->get('plugin.cache_clearer'),
$container->get('cache.menu'),
$container->get('twig'),
$container->get('theme.registry')
);
}
/**
* Reload the previous page.
*/
public function reloadPage() {
$request = $this->requestStack->getCurrentRequest();
if ($request->server->get('HTTP_REFERER')) {
return $request->server->get('HTTP_REFERER');
}
else {
return base_path();
}
}
/**
* Flushes all caches.
*/
public function flushAll() {
$this->messenger()->addMessage($this->t('All caches cleared.'));
drupal_flush_all_caches();
return new RedirectResponse($this->reloadPage());
}
/**
* Flushes css and javascript caches.
*/
public function flushJsCss() {
$this->state()
->set('system.css_js_query_string', base_convert($this->time->getCurrentTime(), 10, 36));
$this->messenger()->addMessage($this->t('CSS and JavaScript cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Flushes plugins caches.
*/
public function flushPlugins() {
$this->pluginCacheClearer->clearCachedDefinitions();
$this->messenger()->addMessage($this->t('Plugins cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Resets all static caches.
*/
public function flushStatic() {
drupal_static_reset();
$this->messenger()->addMessage($this->t('Static cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears all cached menu data.
*/
public function flushMenu() {
$this->cacheMenu->invalidateAll();
$this->menuLinkManager->rebuild();
$this->contextualLinkManager->clearCachedDefinitions();
$this->localTaskLinkManager->clearCachedDefinitions();
$this->localActionLinkManager->clearCachedDefinitions();
$this->messenger()->addMessage($this->t('Routing and links cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears all cached views data.
*/
public function flushViews() {
views_invalidate_cache();
$this->messenger()->addMessage($this->t('Views cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clears the twig cache.
*/
public function flushTwig() {
$this->twig->invalidate();
$this->messenger()->addMessage($this->t('Twig cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Run the cron.
*/
public function runCron() {
$this->cron->run();
$this->messenger()->addMessage($this->t('Cron ran successfully.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Clear the rendered cache.
*/
public function cacheRender() {
$this->cacheRender->invalidateAll();
$this->messenger()->addMessage($this->t('Render cache cleared.'));
return new RedirectResponse($this->reloadPage());
}
/**
* Rebuild the theme registry.
*/
public function themeRebuild() {
$this->themeRegistry->reset();
$this->messenger()->addMessage($this->t('Theme registry rebuilt.'));
return new RedirectResponse($this->reloadPage());
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Drupal\admin_toolbar_tools\Form;
use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Settings form for AdminToolbar Tools.
*
* @package Drupal\admin_toolbar_tools\Form
*/
class AdminToolbarToolsSettingsForm extends ConfigFormBase {
/**
* The cache menu instance.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cacheMenu;
/**
* The menu link manager instance.
*
* @var \Drupal\Core\Menu\MenuLinkManagerInterface
*/
protected $menuLinkManager;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->cacheMenu = $container->get('cache.menu');
$instance->menuLinkManager = $container->get('plugin.manager.menu.link');
return $instance;
}
/**
* {@inheritdoc}
*/
protected function getEditableConfigNames() {
return [
'admin_toolbar_tools.settings',
];
}
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'admin_toolbar_tools_settings';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this->config('admin_toolbar_tools.settings');
$form['max_bundle_number'] = [
'#type' => 'number',
'#title' => $this->t('Maximum number of bundle sub-menus to display'),
'#description' => $this->t('Loading a large number of items can cause performance issues.'),
'#default_value' => $config->get('max_bundle_number'),
];
$form['hoverintent_functionality'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable/Disable the hoverintent functionality'),
'#description' => $this->t('Check it if you want to enable the hoverintent feature.'),
'#default_value' => $config->get('hoverintent_functionality'),
];
$form['show_local_tasks'] = [
'#type' => 'checkbox',
'#title' => $this->t('Enable/Disable local tasks display'),
'#description' => $this->t('Local tasks such as node edit and delete.'),
'#default_value' => $config->get('show_local_tasks'),
];
return parent::buildForm($form, $form_state);
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
$this->config('admin_toolbar_tools.settings')
->set('max_bundle_number', $form_state->getValue('max_bundle_number'))
->set('hoverintent_functionality', $form_state->getValue('hoverintent_functionality'))
->set('show_local_tasks', $form_state->getValue('show_local_tasks'))
->save();
parent::submitForm($form, $form_state);
$this->cacheMenu->invalidateAll();
$this->menuLinkManager->rebuild();
}
}

View File

@@ -0,0 +1,762 @@
<?php
namespace Drupal\admin_toolbar_tools\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Extension\ThemeHandlerInterface;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\system\Entity\Menu;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a default implementation for menu link plugins.
*/
class ExtraLinks extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* The theme handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface
*/
protected $themeHandler;
/**
* The admin toolbar tools configuration.
*
* @var \Drupal\Core\Config\Config
*/
protected $config;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager, ModuleHandlerInterface $module_handler, RouteProviderInterface $route_provider, ThemeHandlerInterface $theme_handler, ConfigFactoryInterface $config_factory, AccountInterface $current_user) {
$this->entityTypeManager = $entity_type_manager;
$this->moduleHandler = $module_handler;
$this->routeProvider = $route_provider;
$this->themeHandler = $theme_handler;
$this->config = $config_factory->get('admin_toolbar_tools.settings');
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity_type.manager'),
$container->get('module_handler'),
$container->get('router.route_provider'),
$container->get('theme_handler'),
$container->get('config.factory'),
$container->get('current_user')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links = [];
$max_bundle_number = $this->config->get('max_bundle_number');
$entity_types = $this->entityTypeManager->getDefinitions();
$content_entities = [];
foreach ($entity_types as $key => $entity_type) {
if ($entity_type->getBundleEntityType() && ($entity_type->get('field_ui_base_route') != '')) {
$content_entities[$key] = [
'content_entity' => $key,
'content_entity_bundle' => $entity_type->getBundleEntityType(),
];
}
}
// Adds common links to entities.
foreach ($content_entities as $entities) {
$content_entity_bundle = $entities['content_entity_bundle'];
$content_entity = $entities['content_entity'];
$content_entity_bundle_storage = $this->entityTypeManager->getStorage($content_entity_bundle);
$bundles_ids = $content_entity_bundle_storage->getQuery()
->accessCheck()
->sort('weight')
->sort($this->entityTypeManager->getDefinition($content_entity_bundle)->getKey('label'))
->pager($max_bundle_number)
->execute();
$bundles = $this->entityTypeManager->getStorage($content_entity_bundle)->loadMultiple($bundles_ids);
if (count($bundles) == $max_bundle_number && $this->routeExists('entity.' . $content_entity_bundle . '.collection')) {
$links[$content_entity_bundle . '.collection'] = [
'title' => $this->t('All types'),
'route_name' => 'entity.' . $content_entity_bundle . '.collection',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'weight' => -999,
] + $base_plugin_definition;
}
foreach ($bundles as $machine_name => $bundle) {
// Normally, the edit form for the bundle would be its root link.
$content_entity_bundle_root = NULL;
if ($this->routeExists('entity.' . $content_entity_bundle . '.overview_form')) {
// Some bundles have an overview/list form that make a better root
// link.
$content_entity_bundle_root = 'entity.' . $content_entity_bundle . '.overview_form.' . $machine_name;
$links[$content_entity_bundle_root] = [
'route_name' => 'entity.' . $content_entity_bundle . '.overview_form',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'route_parameters' => [$content_entity_bundle => $machine_name],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $bundle->getEntityTypeId(),
'entity_id' => $bundle->id(),
],
] + $base_plugin_definition;
$weight = $bundles[$machine_name]->get('weight');
if (isset($weight) && is_numeric($weight)) {
$links[$content_entity_bundle_root]['weight'] = $weight;
}
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.edit_form')) {
$key = 'entity.' . $content_entity_bundle . '.edit_form.' . $machine_name;
$links[$key] = [
'route_name' => 'entity.' . $content_entity_bundle . '.edit_form',
'parent' => 'entity.' . $content_entity_bundle . '.collection',
'route_parameters' => [$content_entity_bundle => $machine_name],
] + $base_plugin_definition;
if (empty($content_entity_bundle_root)) {
$content_entity_bundle_root = $key;
$links[$key]['parent'] = 'entity.' . $content_entity_bundle . '.collection';
// When not grouped by bundle, use bundle name as title.
$links[$key]['class'] = 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity';
$links[$key]['metadata'] = [
'entity_type' => $bundle->getEntityTypeId(),
'entity_id' => $bundle->id(),
];
}
else {
$links[$key]['parent'] = $base_plugin_definition['id'] . ':' . $content_entity_bundle_root;
$links[$key]['title'] = $this->t('Edit');
}
}
if ($this->moduleHandler->moduleExists('field_ui')) {
if ($this->routeExists('entity.' . $content_entity . '.field_ui_fields')) {
$links['entity.' . $content_entity . '.field_ui_fields' . $machine_name] = [
'title' => $this->t('Manage fields'),
'route_name' => 'entity.' . $content_entity . '.field_ui_fields',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 1,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.entity_form_display.' . $content_entity . '.default')) {
$links['entity.entity_form_display.' . $content_entity . '.default' . $machine_name] = [
'title' => $this->t('Manage form display'),
'route_name' => 'entity.entity_form_display.' . $content_entity . '.default',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 2,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.entity_view_display.' . $content_entity . '.default')) {
$links['entity.entity_view_display.' . $content_entity . '.default.' . $machine_name] = [
'title' => $this->t('Manage display'),
'route_name' => 'entity.entity_view_display.' . $content_entity . '.default',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 3,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.' . $bundle->getEntityTypeId() . '.entity_permissions_form')) {
$links['entity.entity_permissions_form.' . $content_entity . '.default.' . $machine_name] = [
'title' => $this->t('Manage permissions'),
'route_name' => 'entity.' . $bundle->getEntityTypeId() . '.entity_permissions_form',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [
$bundle->getEntityTypeId() => $machine_name,
],
'weight' => 3,
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.' . $content_entity_bundle . '.devel_load')) {
$links['entity.' . $content_entity_bundle . '.devel_load.' . $machine_name] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.' . $content_entity_bundle . '.devel_load',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 4,
] + $base_plugin_definition;
}
if ($this->routeExists('entity.' . $content_entity_bundle . '.delete_form')) {
$links['entity.' . $content_entity_bundle . '.delete_form.' . $machine_name] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.' . $content_entity_bundle . '.delete_form',
'parent' => $base_plugin_definition['id'] . ':' . $content_entity_bundle_root,
'route_parameters' => [$content_entity_bundle => $machine_name],
'weight' => 5,
] + $base_plugin_definition;
}
}
}
// Adds user links.
$links['user.admin_create'] = [
'title' => $this->t('Add user'),
'route_name' => 'user.admin_create',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['user.admin_permissions'] = [
'title' => $this->t('Permissions'),
'route_name' => 'user.admin_permissions',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['entity.user_role.collection'] = [
'title' => $this->t('Roles'),
'route_name' => 'entity.user_role.collection',
'parent' => 'entity.user.collection',
] + $base_plugin_definition;
$links['user.logout'] = [
'title' => $this->t('Logout'),
'route_name' => 'user.logout',
'parent' => 'admin_toolbar_tools.help',
'weight' => 10,
] + $base_plugin_definition;
$links['user.role_add'] = [
'title' => $this->t('Add role'),
'route_name' => 'user.role_add',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.collection',
'weight' => -50,
] + $base_plugin_definition;
// Adds sub-links to Account settings link.
if ($this->moduleHandler->moduleExists('field_ui')) {
$links['entity.user.field_ui_fields_'] = [
'title' => $this->t('Manage fields'),
'route_name' => 'entity.user.field_ui_fields',
'parent' => 'entity.user.admin_form',
'weight' => 1,
] + $base_plugin_definition;
$links['entity.entity_form_display.user.default_'] = [
'title' => $this->t('Manage form display'),
'route_name' => 'entity.entity_form_display.user.default',
'parent' => 'entity.user.admin_form',
'weight' => 2,
] + $base_plugin_definition;
$links['entity.entity_view_display.user.default_'] = [
'title' => $this->t('Manage display'),
'route_name' => 'entity.entity_view_display.user.default',
'parent' => 'entity.user.admin_form',
'weight' => 3,
] + $base_plugin_definition;
}
foreach ($this->entityTypeManager->getStorage('user_role')->loadMultiple() as $role) {
$links['entity.user_role.edit_form.' . $role->id()] = [
'route_name' => 'entity.user_role.edit_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.collection',
'weight' => $role->getWeight(),
'route_parameters' => ['user_role' => $role->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $role->getEntityTypeId(),
'entity_id' => $role->id(),
],
] + $base_plugin_definition;
$links['entity.user_role.edit_permissions_form.' . $role->id()] = [
'title' => $this->t('Edit permissions'),
'route_name' => 'entity.user_role.edit_permissions_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
if ($role->id() != 'anonymous' && $role->id() != 'authenticated') {
$links['entity.user_role.delete_form.' . $role->id()] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.user_role.delete_form',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('devel')) {
$links['entity.user_role.devel_load.' . $role->id()] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.user_role.devel_load',
'parent' => $base_plugin_definition['id'] . ':entity.user_role.edit_form.' . $role->id(),
'route_parameters' => ['user_role' => $role->id()],
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('node')) {
$links['node.type_add'] = [
'title' => $this->t('Add content type'),
'route_name' => 'node.type_add',
'parent' => 'entity.node_type.collection',
'weight' => -2,
] + $base_plugin_definition;
$links['node.add'] = [
'title' => $this->t('Add content'),
'route_name' => 'node.add_page',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
// Adds node links for each content type.
foreach ($this->entityTypeManager->getStorage('node_type')->loadMultiple() as $type) {
$links['node.add.' . $type->id()] = [
'route_name' => 'node.add',
'parent' => $base_plugin_definition['id'] . ':node.add',
'route_parameters' => ['node_type' => $type->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $type->getEntityTypeId(),
'entity_id' => $type->id(),
],
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('field_ui')) {
$links['field_ui.entity_form_mode_add'] = [
'title' => $this->t('Add form mode'),
'route_name' => 'field_ui.entity_form_mode_add',
'parent' => 'entity.entity_form_mode.collection',
] + $base_plugin_definition;
$links['field_ui.entity_view_mode_add'] = [
'title' => $this->t('Add view mode'),
'route_name' => 'field_ui.entity_view_mode_add',
'parent' => 'entity.entity_view_mode.collection',
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('taxonomy')) {
$links['entity.taxonomy_vocabulary.add_form'] = [
'title' => $this->t('Add vocabulary'),
'route_name' => 'entity.taxonomy_vocabulary.add_form',
'parent' => 'entity.taxonomy_vocabulary.collection',
'weight' => -998,
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('menu_ui')) {
$links['entity.menu.add_form'] = [
'title' => $this->t('Add menu'),
'route_name' => 'entity.menu.add_form',
'parent' => 'entity.menu.collection',
'weight' => -2,
] + $base_plugin_definition;
// Adds links to /admin/structure/menu.
$menus = $this->entityTypeManager->getStorage('menu')->loadMultiple();
uasort($menus, [Menu::class, 'sort']);
$menus = array_slice($menus, 0, $max_bundle_number);
if (count($menus) == $max_bundle_number) {
$links['entity.menu.collection'] = [
'title' => $this->t('All menus'),
'route_name' => 'entity.menu.collection',
'parent' => 'entity.menu.collection',
'weight' => -1,
] + $base_plugin_definition;
}
$weight = 0;
foreach ($menus as $menu_id => $menu) {
$links['entity.menu.edit_form.' . $menu_id] = [
'route_name' => 'entity.menu.edit_form',
'parent' => 'entity.menu.collection',
'route_parameters' => ['menu' => $menu_id],
'weight' => $weight,
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $menu->getEntityTypeId(),
'entity_id' => $menu->id(),
],
] + $base_plugin_definition;
$links['entity.menu.add_link_form.' . $menu_id] = [
'title' => $this->t('Add link'),
'route_name' => 'entity.menu.add_link_form',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
// Un-deletable menus.
$un_deletable_menus = [
'admin',
'devel',
'footer',
'main',
'tools',
'account',
];
if (!in_array($menu_id, $un_deletable_menus)) {
$links['entity.menu.delete_form.' . $menu_id] = [
'title' => $this->t('Delete'),
'route_name' => 'entity.menu.delete_form',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
}
if ($this->moduleHandler->moduleExists('devel') && $this->routeExists('entity.menu.devel_load')) {
$links['entity.menu.devel_load.' . $menu_id] = [
'title' => $this->t('Devel'),
'route_name' => 'entity.menu.devel_load',
'parent' => $base_plugin_definition['id'] . ':entity.menu.edit_form.' . $menu_id,
'route_parameters' => ['menu' => $menu_id],
] + $base_plugin_definition;
}
$weight++;
}
}
// If module block_content is enabled.
if ($this->moduleHandler->moduleExists('block_content')) {
// Add the custom blocks management under Content.
$links['block_content_page'] = [
'title' => $this->t('Blocks'),
'route_name' => 'entity.block_content.collection',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
$links['add_block'] = [
'title' => $this->t('Add content block'),
'route_name' => 'block_content.add_page',
'parent' => $base_plugin_definition['id'] . ':block_content_page',
] + $base_plugin_definition;
// Adds links for each block_content type.
foreach ($this->entityTypeManager->getStorage('block_content_type')->loadMultiple() as $type) {
$links['block_content.add.' . $type->id()] = [
'route_name' => 'block_content.add_form',
'parent' => $base_plugin_definition['id'] . ':add_block',
'route_parameters' => ['block_content_type' => $type->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $type->getEntityTypeId(),
'entity_id' => $type->id(),
],
] + $base_plugin_definition;
}
if (version_compare(\Drupal::VERSION, '10.1', '<')) {
// Add custom block types management under Structure.
$links['entity.block_content_type.collection'] = [
'title' => $this->t('Block types'),
'route_name' => 'entity.block_content_type.collection',
'parent' => 'block.admin_display',
] + $base_plugin_definition;
}
}
// If module Contact is enabled.
if ($this->moduleHandler->moduleExists('contact')) {
$links['contact.form_add'] = [
'title' => $this->t('Add contact form'),
'route_name' => 'contact.form_add',
'parent' => 'entity.contact_form.collection',
'weight' => -5,
] + $base_plugin_definition;
}
// If module Update Manager is enabled.
if ($this->moduleHandler->moduleExists('update')) {
if (version_compare(\Drupal::VERSION, '11.0.0', '<')) {
$links['update.module_install'] = [
'title' => $this->t('Install new module'),
'route_name' => 'update.module_install',
'parent' => 'system.modules_list',
] + $base_plugin_definition;
}
$links['update.module_update'] = [
'title' => $this->t('Update'),
'route_name' => 'update.module_update',
'parent' => 'system.modules_list',
] + $base_plugin_definition;
if (version_compare(\Drupal::VERSION, '11.0.0', '<')) {
$links['update.theme_install'] = [
'title' => $this->t('Install new theme'),
'route_name' => 'update.theme_install',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
}
$links['update.theme_update'] = [
'title' => $this->t('Update'),
'route_name' => 'update.theme_update',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
}
// If module Devel is enabled.
if ($this->moduleHandler->moduleExists('devel')) {
$links['devel'] = [
'title' => $this->t('Development'),
'route_name' => 'system.admin_config_development',
'parent' => 'admin_toolbar_tools.help',
'weight' => '-8',
] + $base_plugin_definition;
$links['devel.admin_settings'] = [
'title' => $this->t('Devel settings'),
'route_name' => 'devel.admin_settings',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.configs_list'] = [
'title' => $this->t('Config editor'),
'route_name' => 'devel.configs_list',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.reinstall'] = [
'title' => $this->t('Reinstall modules'),
'route_name' => 'devel.reinstall',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.menu_rebuild'] = [
'title' => $this->t('Rebuild menu'),
'route_name' => 'devel.menu_rebuild',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.state_system_page'] = [
'title' => $this->t('State editor'),
'route_name' => 'devel.state_system_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.theme_registry'] = [
'title' => $this->t('Theme registry'),
'route_name' => 'devel.theme_registry',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.entity_info_page'] = [
'title' => $this->t('Entity info'),
'route_name' => 'devel.entity_info_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.session'] = [
'title' => $this->t('Session viewer'),
'route_name' => 'devel.session',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
$links['devel.element_info'] = [
'title' => $this->t('Element Info'),
'route_name' => 'devel.elements_page',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
// Menu link for the Toolbar module.
$links['devel.toolbar.settings'] = [
'title' => $this->t('Devel Toolbar Settings'),
'route_name' => 'devel.toolbar.settings_form',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
if ($this->moduleHandler->moduleExists('webprofiler')) {
$links['devel.webprofiler'] = [
'title' => $this->t('Webprofiler settings'),
'route_name' => 'webprofiler.settings',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
}
// If module Devel PHP is enabled.
if ($this->moduleHandler->moduleExists('devel_php') && $this->routeExists('devel_php.execute_php')) {
$links['devel.devel_php.execute_php'] = [
'title' => $this->t('Execute PHP Code'),
'route_name' => 'devel_php.execute_php',
'parent' => $base_plugin_definition['id'] . ':devel',
] + $base_plugin_definition;
}
}
// If module Views Ui enabled.
if ($this->moduleHandler->moduleExists('views_ui')) {
$links['views_ui.add'] = [
'title' => $this->t('Add view'),
'route_name' => 'views_ui.add',
'parent' => 'entity.view.collection',
'weight' => -5,
] + $base_plugin_definition;
$views = $this->entityTypeManager->getStorage('view')->loadByProperties(['status' => TRUE]);
foreach ($views as $view) {
$links['views_ui.' . $view->id()] = [
'title' => $view->label(),
'route_name' => 'entity.view.edit_form',
'route_parameters' => ['view' => $view->id()],
'parent' => 'entity.view.collection',
] + $base_plugin_definition;
}
$links['views_ui.field_list'] = [
'title' => $this->t('Used in views'),
'route_name' => 'views_ui.reports_fields',
'parent' => 'entity.field_storage_config.collection',
] + $base_plugin_definition;
}
// Adds theme management links.
$links['system.theme_settings'] = [
'title' => $this->t('Settings'),
'route_name' => 'system.theme_settings',
'parent' => 'system.themes_page',
] + $base_plugin_definition;
$installed_themes = $this->installedThemes();
foreach ($installed_themes as $key_theme => $label_theme) {
$links['system.theme_settings_theme.' . $key_theme] = [
'title' => $label_theme,
'route_name' => 'system.theme_settings_theme',
'parent' => $base_plugin_definition['id'] . ':system.theme_settings',
'route_parameters' => ['theme' => $key_theme],
] + $base_plugin_definition;
}
// If module Language enabled.
if ($this->moduleHandler->moduleExists('language')) {
$links['language.negotiation'] = [
'title' => $this->t('Detection and selection'),
'route_name' => 'language.negotiation',
'parent' => 'entity.configurable_language.collection',
] + $base_plugin_definition;
$links['language.add'] = [
'title' => $this->t('Add language'),
'route_name' => 'language.add',
'parent' => 'entity.configurable_language.collection',
] + $base_plugin_definition;
}
// If module Media enabled.
if ($this->moduleHandler->moduleExists('media')) {
$links['media.type_add'] = [
'title' => $this->t('Add media type'),
'route_name' => 'entity.media_type.add_form',
'parent' => 'entity.media_type.collection',
'weight' => -2,
] + $base_plugin_definition;
// Displays media link in toolbar.
$links['media_page'] = [
'title' => $this->t('Media'),
'route_name' => 'entity.media.collection',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
if ($this->moduleHandler->moduleExists('media_library') && $this->routeExists('view.media_library.page')) {
$links['media_library'] = [
'title' => $this->t('Media library'),
'route_name' => 'view.media_library.page',
'parent' => $base_plugin_definition['id'] . ':media_page',
] + $base_plugin_definition;
}
$links['add_media'] = [
'title' => $this->t('Add media'),
'route_name' => 'entity.media.add_page',
'parent' => $base_plugin_definition['id'] . ':media_page',
] + $base_plugin_definition;
// Adds links for each media type.
foreach ($this->entityTypeManager->getStorage('media_type')->loadMultiple() as $type) {
$links['media.add.' . $type->id()] = [
'route_name' => 'entity.media.add_form',
'parent' => $base_plugin_definition['id'] . ':add_media',
'route_parameters' => ['media_type' => $type->id()],
'class' => 'Drupal\admin_toolbar_tools\Plugin\Menu\MenuLinkEntity',
'metadata' => [
'entity_type' => $type->getEntityTypeId(),
'entity_id' => $type->id(),
],
] + $base_plugin_definition;
}
}
// If module Config enabled.
if ($this->moduleHandler->moduleExists('config')) {
$links['config.import'] = [
'title' => $this->t('Import'),
'route_name' => 'config.import_full',
'parent' => 'config.sync',
'weight' => 1,
] + $base_plugin_definition;
$links['config.export'] = [
'title' => $this->t('Export'),
'route_name' => 'config.export_full',
'parent' => 'config.sync',
'weight' => 2,
] + $base_plugin_definition;
}
// Adds a menu link to clear Views cache.
if ($this->moduleHandler->moduleExists('views')) {
$links['flush_views'] = [
'title' => $this->t('Flush views cache'),
'route_name' => 'admin_toolbar_tools.flush_views',
'parent' => 'admin_toolbar_tools.flush',
] + $base_plugin_definition;
// Adding a menu link to Files.
if ($this->moduleHandler->moduleExists('file') && $this->routeExists('view.files.page_1')) {
$links['view.files'] = [
'title' => $this->t('Files'),
'route_name' => 'view.files.page_1',
'parent' => 'system.admin_content',
] + $base_plugin_definition;
}
}
if ($this->moduleHandler->moduleExists('project_browser')) {
$links['project_browser.browse'] = [
'title' => $this->t('Browse'),
'route_name' => 'project_browser.browse',
'parent' => 'system.modules_list',
'weight' => -1,
] + $base_plugin_definition;
}
return $links;
}
/**
* Determine if a route exists by name.
*
* @param string $route_name
* The name of the route to check.
*
* @return bool
* Whether a route with that route name exists.
*/
public function routeExists($route_name) {
return (count($this->routeProvider->getRoutesByNames([$route_name])) === 1);
}
/**
* Lists all installed themes.
*
* @return array
* The list of installed themes.
*/
public function installedThemes() {
$themeHandler = $this->themeHandler;
$all_themes = $themeHandler->listInfo();
$themes_installed = [];
foreach ($all_themes as $key_theme => $theme) {
if ($themeHandler->hasUi($key_theme)) {
$themes_installed[$key_theme] = $themeHandler->getName($key_theme);
}
}
return $themes_installed;
}
}

View File

@@ -0,0 +1,104 @@
<?php
namespace Drupal\admin_toolbar_tools\Plugin\Menu;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\MenuLinkDefault;
use Drupal\Core\Menu\StaticMenuLinkOverridesInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a menu link plugins for configuration entities.
*/
class MenuLinkEntity extends MenuLinkDefault {
/**
* The entity represented in the menu link.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $entity;
/**
* Constructs a new MenuLinkEntity.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Menu\StaticMenuLinkOverridesInterface $static_override
* The static override storage.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, StaticMenuLinkOverridesInterface $static_override, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $static_override);
$this->entity = $entity_type_manager->getStorage($this->pluginDefinition['metadata']['entity_type'])->load($this->pluginDefinition['metadata']['entity_id']);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('menu_link.static.overrides'),
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function getTitle() {
if ($this->entity) {
return (string) $this->entity->label();
}
return $this->pluginDefinition['title'] ?: $this->t('Missing');
}
/**
* {@inheritdoc}
*/
public function getDescription() {
if ($this->entity && method_exists($this->entity, 'getDescription')) {
$description = $this->entity->getDescription();
}
return $description ?? parent::getDescription();
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
if ($this->entity) {
return $this->entity->getCacheContexts();
}
return parent::getCacheContexts();
}
/**
* {@inheritdoc}
*/
public function getCacheTags() {
if ($this->entity) {
return $this->entity->getCacheTags();
}
return parent::getCacheTags();
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
if ($this->entity) {
return $this->entity->getCacheMaxAge();
}
return parent::getCacheMaxAge();
}
}