You are here

public function EasyBreadcrumbBuilder::getTitleString in Easy Breadcrumb 8

Same name and namespace in other branches
  1. 2.x src/EasyBreadcrumbBuilder.php \Drupal\easy_breadcrumb\EasyBreadcrumbBuilder::getTitleString()

Get string title for route.

Parameters

\Symfony\Component\HttpFoundation\Request $route_request: A request object.

\Drupal\Core\Routing\RouteMatchInterface $route_match: A RouteMatch object.

array $replacedTitles: A array replaced titles.

Return value

string|null Either the current title string or NULL if unable to determine it.

1 call to EasyBreadcrumbBuilder::getTitleString()
EasyBreadcrumbBuilder::build in src/EasyBreadcrumbBuilder.php
Builds the breadcrumb.

File

src/EasyBreadcrumbBuilder.php, line 685

Class

EasyBreadcrumbBuilder
Primary implementation for the Easy Breadcrumb builder.

Namespace

Drupal\easy_breadcrumb

Code

public function getTitleString(Request $route_request, RouteMatchInterface $route_match, array $replacedTitles) {
  try {
    $title = $this->titleResolver
      ->getTitle($route_request, $route_match
      ->getRouteObject());
  } catch (\InvalidArgumentException $exception) {
    $title = NULL;
  }
  $this
    ->applyTitleReplacement($title, $replacedTitles);

  // Title resolver only returns title if route defines a _title or
  // _title_callback but some core routes like node.edit or block_content.edit
  // uses $main_content['#title'] to set a title. Add an special case to set a
  // title for {entity_type_id}.{operation} when it's possible.
  if (NULL === $title && ($entityForm = $route_match
    ->getRouteObject()
    ->getDefault('_entity_form'))) {
    $entityFormParts = explode('.', $entityForm);
    if (2 === count($entityFormParts)) {
      $entity_type_id = $entityFormParts[0];
      $operation = $entityFormParts[1];

      // Operations that can be used as a title: add, edit or delete.
      if (in_array($operation, [
        'add',
        'edit',
        'delete',
      ])) {
        $title = $operation;
      }
      elseif (in_array($operation, [
        'default',
        'view',
        'preview',
      ])) {
        if ($entity = $route_match
          ->getParameter($entity_type_id)) {
          if (is_object($entity)) {
            if (method_exists($entity, 'getTitle')) {
              $title = $entity
                ->getTitle();
            }
            elseif (method_exists($entity, 'label')) {
              $title = $entity
                ->label();
            }
          }
        }
      }
    }
  }

  // If title is object then try to render it.
  if ($title instanceof MarkupInterface) {
    $title = strip_tags(Html::decodeEntities($title));
  }
  elseif (is_array($title) && array_key_exists('#markup', $title)) {

    // If this render array has #allowed tags use that instead of default.
    $tags = array_key_exists('#allowed_tags', $title) ? $title['#allowed_tags'] : NULL;
    $title = Html::decodeEntities(Xss::filter($title['#markup'], $tags));
  }
  if (!is_string($title)) {
    return NULL;
  }
  return $title;
}