You are here

private function PageContext::setBaseData in Acquia Lift Connector 8.3

Same name and namespace in other branches
  1. 8.4 src/Service/Context/PageContext.php \Drupal\acquia_lift\Service\Context\PageContext::setBaseData()

Set page context - title.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The request object.

\Symfony\Component\Routing\Route $route: The route object.

\Drupal\Core\Controller\TitleResolverInterface $titleResolver: The title resolver.

Drupal\Core\Language\LanguageManagerInterface $languageManager: The language manager.

1 call to PageContext::setBaseData()
PageContext::__construct in src/Service/Context/PageContext.php
Constructor.

File

src/Service/Context/PageContext.php, line 186

Class

PageContext

Namespace

Drupal\acquia_lift\Service\Context

Code

private function setBaseData(Request $request, Route $route, TitleResolverInterface $titleResolver, LanguageManagerInterface $languageManager) {

  // Set language code
  // After investigation, there is no use case where the methods
  // 'getCurrentLanguage' and 'getId' would not exist within LanguageManager.
  // Drupal 8 will ALWAYS set a language code and would not be null,
  // therefore no checks are required.
  $this->htmlHeadContexts['context_language'] = $languageManager
    ->getCurrentLanguage()
    ->getId();

  // Get title
  $title = $titleResolver
    ->getTitle($request, $route);

  // Set title.
  // If markup, strip tags and convert to string.
  if (is_array($title) && isset($title['#markup']) && isset($title['#allowed_tags'])) {
    $allowed_tags = empty($title['#allowed_tags']) ? '' : '<' . implode('><', $title['#allowed_tags']) . '>';
    $title = strip_tags($title['#markup'], $allowed_tags);
  }

  // If still an array or empty, leave title at "Untitled".
  if (is_array($title) || empty($title)) {
    return;
  }

  // Otherwise set title.
  $this->htmlHeadContexts['content_title'] = $title;
}