You are here

public function BackgroundImageViewBuilder::buildText in Background Image 8

Same name and namespace in other branches
  1. 2.x src/BackgroundImageViewBuilder.php \Drupal\background_image\BackgroundImageViewBuilder::buildText()
  2. 2.0.x src/BackgroundImageViewBuilder.php \Drupal\background_image\BackgroundImageViewBuilder::buildText()

Builds the text render array.

Parameters

\Drupal\background_image\BackgroundImageInterface $background_image: The background image being processed.

\Drupal\background_image\BackgroundImageManagerInterface $manager: The Background Image Manager service.

Return value

array The built render array element.

1 call to BackgroundImageViewBuilder::buildText()
BackgroundImageViewBuilder::build in src/BackgroundImageViewBuilder.php
Builds an entity's view; augments entity defaults.

File

src/BackgroundImageViewBuilder.php, line 139

Class

BackgroundImageViewBuilder

Namespace

Drupal\background_image

Code

public function buildText(BackgroundImageInterface $background_image, BackgroundImageManagerInterface $manager) {
  $text = trim($background_image
    ->getSetting('text.value', ''));

  // Immediately return if there is no text.
  if (!$text) {
    $build['#access'] = FALSE;
    $build['#cache']['contexts'][] = 'background_image.settings.text';
    return $build;
  }
  $base_class = $manager
    ->getBaseClass();
  $build = [
    '#type' => 'processed_text',
    '#theme_wrappers' => [
      'container__background_image__text',
    ],
    '#attributes' => [
      'class' => [
        "{$base_class}-text",
      ],
    ],
    '#format' => $background_image
      ->getSetting('text.format', 'full_html'),
    '#langcode' => $background_image
      ->language()
      ->getId(),
    '#text' => $text,
  ];
  $build['#cache']['contexts'][] = 'background_image.settings.text';

  // Add entity to token data.
  $token_data = [
    'background_image' => $background_image,
  ];
  $entity = $this
    ->getEntity($background_image, $manager);
  if ($entity) {
    $token_data[$entity
      ->getEntityTypeId()] = $entity instanceof ViewEntityInterface ? $entity
      ->getExecutable() : $entity;
  }

  // Allow extensions a chance to alter the text before it's tokenized.
  $context = [
    'background_image' => $background_image,
    'entity' => $entity,
    'token_data' => $token_data,
    'token_options' => [
      'clear' => TRUE,
      'langcode' => &$build['#langcode'],
    ],
  ];
  $this
    ->moduleHandler()
    ->alter('background_image_text_build', $build, $context);
  \Drupal::service('theme.manager')
    ->alter('background_image_text_build', $build, $context);

  // Perform token replacements.
  $build['#text'] = \Drupal::token()
    ->replace($build['#text'], $context['token_data'], $context['token_options']);

  // Allow extensions a chance to alter the text after it's tokenized.
  $this
    ->moduleHandler()
    ->alter('background_image_text_after_build', $build, $context);
  \Drupal::service('theme.manager')
    ->alter('background_image_text_after_build', $build, $context);
  return $build;
}