Skip to content
Snippets Groups Projects
Commit c950496e authored by Dave Reid's avatar Dave Reid Committed by Adrian Cid Almaguer
Browse files

Issue #2759345: Option to move local tasks (tabs) into the Admin Toolbar

parent f4991a19
No related branches found
No related tags found
2 merge requests!44Issue #3286466: Tabbing order does not satisfy 508 accessibility requirements,!13Issue #2759345: Option to move local tasks (tabs) into the Admin Toolbar
......@@ -8,6 +8,7 @@
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Cache\CacheableMetadata;
/**
* Implements hook_toolbar().
......@@ -24,6 +25,49 @@ function admin_toolbar_tools_toolbar() {
],
'#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
];
// Toolbar item for primary local tasks.
$config = \Drupal::config('admin_toolbar_tools.settings')->get('show_local_tasks');
$items['admin_toolbar_local_tasks'] = [];
CacheableMetadata::createFromObject($config)->applyTo($items['admin_toolbar_local_tasks']);
if ($config) {
$items['admin_toolbar_local_tasks'] += [
'#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' => t('Local Tasks'),
'#url' => Url::fromRoute('<none>'),
'#attributes' => [
'class' => [
'toolbar-icon',
'toolbar-icon-local-tasks',
],
],
],
'tray' => [
'local_links' => [
'#lazy_builder' => [
'admin_toolbar_tools.helper:localTasksTrayLazyBuilder',
[],
],
],
],
'#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']],
];
}
return $items;
}
......
......@@ -3,3 +3,5 @@ services:
class: Drupal\admin_toolbar_tools\AdminToolbarToolsHelper
arguments:
- '@entity_type.manager'
- '@plugin.manager.menu.local_task'
- '@current_route_match'
\ No newline at end of file
max_bundle_number: 20
hoverintent_functionality: true
show_local_tasks: false
......@@ -8,3 +8,6 @@ admin_toolbar_tools.settings:
hoverintent_functionality:
type: boolean
label: 'Enable or disable hoverintent functionality'
show_local_tasks:
type: boolean
label: 'Show local tasks in toolbar'
......@@ -30,3 +30,15 @@
.toolbar-icon-8 .toolbar-icon-admin-toolbar-tools-help.active:before {
background-image: url(../misc/icons/ffffff/drupal-8-logo.svg);
}
.toolbar-oriented .toolbar-bar .local-tasks-toolbar-tab {
float: right;
}
.toolbar-horizontal .local-tasks-toolbar-tab .toolbar-menu {
float: right;
}
.toolbar-bar .toolbar-icon-local-tasks:before {
background-image: url(../misc/icons/bebebe/tasks.svg);
}
\ No newline at end of file
<svg fill="#bebebe" height="24" viewBox="0 0 24 24" width="24" xmlns="http://www.w3.org/2000/svg">
<path d="M4 10.5c-.83 0-1.5.67-1.5 1.5s.67 1.5 1.5 1.5 1.5-.67 1.5-1.5-.67-1.5-1.5-1.5zm0-6c-.83 0-1.5.67-1.5 1.5S3.17 7.5 4 7.5 5.5 6.83 5.5 6 4.83 4.5 4 4.5zm0 12c-.83 0-1.5.68-1.5 1.5s.68 1.5 1.5 1.5 1.5-.68 1.5-1.5-.67-1.5-1.5-1.5zM7 19h14v-2H7v2zm0-6h14v-2H7v2zm0-8v2h14V5H7z"/>
<path d="M0 0h24v24H0V0z" fill="none"/>
</svg>
......@@ -3,11 +3,15 @@
namespace Drupal\admin_toolbar_tools;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\LocalTaskManager;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Security\TrustedCallbackInterface;
/**
* Admin Toolbar Tools helper service.
*/
class AdminToolbarToolsHelper {
class AdminToolbarToolsHelper implements TrustedCallbackInterface {
/**
* The entity type manager.
......@@ -16,14 +20,73 @@ class AdminToolbarToolsHelper {
*/
protected $entityTypeManager;
/**
* The local task manger.
*
* @var \Drupal\Core\Menu\LocalTaskManager
* The local task manager menu.
*/
protected $localTaskManager;
/**
* The route match interface.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
* The route match.
*/
protected $routeMatch;
/**
* Create an AdminToolbarToolsHelper object.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
* @param \Drupal\Core\Menu\LocalTaskManager $local_task_manager
* The local task manager.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
public function __construct(EntityTypeManagerInterface $entity_type_manager, LocalTaskManager $local_task_manager, RouteMatchInterface $route_match) {
$this->entityTypeManager = $entity_type_manager;
$this->localTaskManager = $local_task_manager;
$this->routeMatch = $route_match;
}
/**
* {@inheritdoc}
*/
public static function trustedCallbacks() {
return ['localTasksTrayLazyBuilder'];
}
/**
* Lazy builder callback for the admin_toolbar_local_tasks tray items.
*
* @return array
* A renderable array as expected by the renderer service.
*/
public function localTasksTrayLazyBuilder() {
// Get primary local task links and inject them into new
// admin_toolbar_local_tasks toolbar tray.
$links = $this->localTaskManager->getLocalTasks($this->routeMatch->getRouteName(), 0);
if (!empty($links['tabs'])) {
$build = [
'#theme' => 'links',
'#links' => [],
'#attributes' => [
'class' => ['toolbar-menu'],
],
];
Element::children($links['tabs'], TRUE);
$routes = Element::getVisibleChildren($links['tabs']);
foreach ($routes as $route) {
$build['#links'][$route] = $links['tabs'][$route]['#link'];
}
$links['cacheability']->applyTo($build);
return $build;
}
return [];
}
/**
......
......@@ -92,6 +92,13 @@ class AdminToolbarToolsSettingsForm extends ConfigFormBase {
'#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);
}
......@@ -102,6 +109,7 @@ class AdminToolbarToolsSettingsForm extends ConfigFormBase {
$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();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment