You are here

function hook_background_image_text_build_alter in Background Image 8

Same name and namespace in other branches
  1. 2.x background_image.api.php \hook_background_image_text_build_alter()
  2. 2.0.x background_image.api.php \hook_background_image_text_build_alter()

Provides extensions a way to alter the text before it's been tokenized.

Parameters

array $element: The text render array element.

array $context: An associative array containing:

1 invocation of hook_background_image_text_build_alter()
BackgroundImageViewBuilder::buildText in src/BackgroundImageViewBuilder.php
Builds the text render array.

File

./background_image.api.php, line 76
API hooks and alters for the background_image module.

Code

function hook_background_image_text_build_alter(array &$element, array &$context) {

  /** @var \Drupal\background_image\BackgroundImageInterface $background_image */
  $background_image = $context['background_image'];

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $entity = $context['entity'];

  // Prepend a header to all full viewport background images.
  if ($background_image
    ->getSetting('full_viewport') && $entity && $entity
    ->hasField('field_header') && ($header = $entity
    ->get('field_header')->value)) {
    $element['#text'] = '<h2>' . $header . '</h2>' . $element['#text'];
  }
}