function PreprocessHooks::breadcrumb

Same name and namespace in other branches
  1. 11.x core/themes/admin/src/Hook/PreprocessHooks.php \Drupal\admin\Hook\PreprocessHooks::breadcrumb()

Implements hook_preprocess_HOOK() for breadcrumb.

Attributes

#[Hook('preprocess_breadcrumb')]

File

core/themes/admin/src/Hook/PreprocessHooks.php, line 168

Class

PreprocessHooks
Provides preprocess implementations.

Namespace

Drupal\admin\Hook

Code

public function breadcrumb(array &$variables) : void {
  if (empty($variables['breadcrumb'])) {
    return;
  }
  $cacheability = new CacheableMetadata();
  $cacheability->addCacheContexts([
    'route',
  ]);
  $entity = NULL;
  $route_name = $this->currentRouteMatch
    ->getRouteName();
  // Entity will be found in the route parameters.
  if (($route = $this->currentRouteMatch
    ->getRouteObject()) && $parameters = $route->getOption('parameters')) {
    // Determine if the current route represents an entity.
    foreach ($parameters as $name => $options) {
      if (isset($options['type']) && str_starts_with($options['type'], 'entity:')) {
        $routeEntity = $this->currentRouteMatch
          ->getParameter($name);
        if ($routeEntity instanceof ContentEntityInterface && $routeEntity->hasLinkTemplate('canonical')) {
          $entity = $routeEntity;
        }
        break;

      }
    }
  }
  $operation_label = NULL;
  if ($entity !== NULL) {
    $url = $entity->toUrl();
    $entity_type_id = $entity->getEntityTypeId();
    $entity_type = $entity->getEntityType();
    $type_label = $entity_type->getSingularLabel();
    $bundle_key = $entity_type->getKey('bundle');
    if ($bundle_key) {
      $bundle_entity = $entity->get($bundle_key)->entity;
      $type_label = $bundle_entity->label();
    }
    if ($entity_type->id() === 'user') {
      $type_label = 'account';
    }
    $operation_labels = [
      '#entity.(?<entityTypeId>.+).canonical#' => $this->t('View @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).delete_form#' => $this->t('Delete @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).delete_multiple_form#' => $this->t('Delete @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).edit_form#' => $this->t('Edit @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).add_form#' => $this->t('Add @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).add_page#' => $this->t('Add @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).reset_form#' => $this->t('Reset @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).cancel_form#' => $this->t('Cancel @bundle', [
        '@bundle' => $type_label,
      ]),
      '#entity.(?<entityTypeId>.+).clone_form#' => $this->t('Clone @bundle', [
        '@bundle' => $type_label,
      ]),
    ];
    foreach ($operation_labels as $regex => $label) {
      if (preg_match($regex, $route_name)) {
        $operation_label = $label;
        break;

      }
    }
    $url_access = $url->access(NULL, TRUE);
    $cacheability->addCacheableDependency($url_access);
    // Media handling.
    if ($entity_type_id === 'media') {
      $media_config = $this->configFactory
        ->get('media.settings');
      $cacheability->addCacheableDependency($media_config);
      if (!$media_config->get('standalone_url')) {
        $url = Url::fromRoute('<front>');
      }
    }
    // Custom block handling (a custom block cannot be viewed standalone).
    if ($entity_type_id === 'block_content') {
      $url = Url::fromRoute('<front>');
    }
  }
  // Back to site item.
  foreach ($variables['breadcrumb'] as $key => $item) {
    if ($key === 0) {
      $variables['breadcrumb'][$key]['text'] = $this->t('Back to site');
      $variables['breadcrumb'][$key]['attributes']['title'] = $this->t('Return to site content');
      if (isset($url, $url_access) && $url_access->isAllowed()) {
        // Link to the canonical route of the entity.
        $variables['breadcrumb'][$key]['url'] = $url;
      }
      else {
        // Let escapeAdmin override the return URL.
        $variables['breadcrumb'][$key]['attributes']['data'] = 'data-gin-toolbar-escape-admin';
      }
    }
    elseif (isset($url) && $item['url'] === $url->setAbsolute(FALSE)
      ->toString()) {
      // Remove as we already have the back to site link set.
      unset($variables['breadcrumb'][$key]);
    }
  }
  // Adjust breadcrumb for nodes: unset all items, except home link.
  if ($entity instanceof NodeInterface) {
    foreach ($variables['breadcrumb'] as $key => $item) {
      if ($key > 0) {
        unset($variables['breadcrumb'][$key]);
      }
    }
  }
  // Adjust breadcrumb for entities.
  if ($operation_label !== NULL) {
    // Add bundle info.
    $variables['breadcrumb'][] = [
      'text' => $operation_label,
      'url' => '',
    ];
  }
  $cacheability->applyTo($variables);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.