Skip to content
Snippets Groups Projects

Attempt to make the Local Tasks menu label more explicit for entity routes.

1 file
+ 35
1
Compare changes
  • Side-by-side
  • Inline
@@ -4,6 +4,7 @@ namespace Drupal\admin_toolbar_tools;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Menu\LocalTaskManagerInterface;
use Drupal\Core\Render\Element;
@@ -102,6 +103,15 @@ class AdminToolbarToolsHelper {
}
}
// Provide a more explicit label for the local tasks menu.
$local_tasks_menu_label = $this->t('Local Tasks');
// Use the entity type label if available.
$entity = $this->getEntityFromRoute();
$entity_type = $entity ? $entity->getEntityType()->getLabel() : NULL;
if (!empty($entity_type)) {
$local_tasks_menu_label = $this->t('This @entity_type', ['@entity_type' => $entity_type]);
}
$build = [
'#type' => 'toolbar_item',
'#wrapper_attributes' => [
@@ -117,7 +127,7 @@ class AdminToolbarToolsHelper {
// This means that for now we always render Local Tasks item even
// when the tray is empty.
'#type' => 'link',
'#title' => $this->t('Local Tasks'),
'#title' => $local_tasks_menu_label,
'#url' => Url::fromRoute('<none>'),
'#attributes' => [
'class' => [
@@ -171,4 +181,28 @@ class AdminToolbarToolsHelper {
return $types;
}
/**
* Helper function to extract the entity for the supplied route.
*
* @return null|\Drupal\Core\Entity\ContentEntityInterface
* The entity object or NULL if not found.
*/
protected function getEntityFromRoute() {
// Entity will be found in the route parameters.
if (($route = $this->routeMatch->getRouteObject()) && ($parameters = $route->getOption('parameters'))) {
// Determine if the current route represents an entity.
foreach ($parameters as $name => $options) {
if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
$entity = $this->routeMatch->getParameter($name);
if ($entity instanceof ContentEntityInterface && $entity->hasLinkTemplate('canonical')) {
return $entity;
}
// Since entity was found, no need to iterate further.
return NULL;
}
}
}
}
}
Loading