diff --git a/admin_toolbar_tools/admin_toolbar_tools.module b/admin_toolbar_tools/admin_toolbar_tools.module index 55d45a3..bb70b5e 100644 --- a/admin_toolbar_tools/admin_toolbar_tools.module +++ b/admin_toolbar_tools/admin_toolbar_tools.module @@ -25,49 +25,39 @@ function admin_toolbar_tools_toolbar() { '#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']], ]; - // Get primary local task links and inject them into new - // admin_toolbar_local_tasks toolbar item. + // Toolbar item for primary local tasks. // @todo - do we need some configuration to turn this on/off? - $manager = \Drupal::service('plugin.manager.menu.local_task'); - $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0); - if (!empty($links)) { - $local_links = []; - foreach ($links['tabs'] as $route => $link) { - $local_links[$route] = $link['#link']; - } - - $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' => [ - '#type' => 'link', - '#title' => t('Local Tasks'), - '#url' => Url::fromRoute(''), - '#attributes' => [ - 'class' => [ - 'toolbar-icon', - 'toolbar-icon-local-tasks', - ], + $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(''), + '#attributes' => [ + 'class' => [ + 'toolbar-icon', + 'toolbar-icon-local-tasks', ], ], - 'tray' => [ - 'local_links' => [ - // @todo - we need to put some cache context here! - //'#cache' => [], - '#theme' => 'links', - '#attributes' => [ - 'class' => ['toolbar-menu'], - ], - '#links' => $local_links, - ], + ], + 'tray' => [ + 'local_links' => [ + '#lazy_builder' => ['admin_toolbar_tools.toolbar_handler:localTasksTrayLazyBuilder', []], ], - ]; - } + ], + '#attached' => ['library' => ['admin_toolbar_tools/toolbar.icon']], + ]; return $items; } diff --git a/admin_toolbar_tools/css/tools.css b/admin_toolbar_tools/css/tools.css index e233220..23cd869 100644 --- a/admin_toolbar_tools/css/tools.css +++ b/admin_toolbar_tools/css/tools.css @@ -15,7 +15,14 @@ background-image: url(../misc/icons/ffffff/d8-item.svg); } -.toolbar-oriented .toolbar-bar .local-tasks-toolbar-tab, -.toolbar-oriented .toolbar-bar .local-tasks-toolbar-tab .toolbar-menu { +.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); +} diff --git a/admin_toolbar_tools/misc/icons/bebebe/tasks.svg b/admin_toolbar_tools/misc/icons/bebebe/tasks.svg new file mode 100644 index 0000000..7b47a81 --- /dev/null +++ b/admin_toolbar_tools/misc/icons/bebebe/tasks.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/admin_toolbar_tools/src/ToolbarHandler.php b/admin_toolbar_tools/src/ToolbarHandler.php index c46c704..53520e9 100644 --- a/admin_toolbar_tools/src/ToolbarHandler.php +++ b/admin_toolbar_tools/src/ToolbarHandler.php @@ -84,6 +84,30 @@ class ToolbarHandler implements ContainerInjectionInterface { } /** + * 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. + $manager = \Drupal::service('plugin.manager.menu.local_task'); + $links = $manager->getLocalTasks(\Drupal::routeMatch()->getRouteName(), 0); + if (!empty($links)) { + $build['#theme'] = 'links'; + $build['#attributes'] = ['class' => ['toolbar-menu']]; + foreach ($links['tabs'] as $route => $link) { + $build['#links'][$route] = $link['#link']; + } + + return $build; + } + + return []; + } + + /** * Create the menu render array. * * @param string $route