You are here

public function BackgroundImageViewBuilder::buildImage in Background Image 8

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

Builds the image 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::buildImage()
BackgroundImageViewBuilder::build in src/BackgroundImageViewBuilder.php
Builds an entity's view; augments entity defaults.

File

src/BackgroundImageViewBuilder.php, line 48

Class

BackgroundImageViewBuilder

Namespace

Drupal\background_image

Code

public function buildImage(BackgroundImageInterface $background_image, BackgroundImageManagerInterface $manager) {

  // Immediately return if there is no image.
  if (!$background_image
    ->getImageFile()) {
    $build['#access'] = FALSE;
    $build['#cache']['contexts'][] = 'background_image';
    return $build;
  }
  $base_class = $manager
    ->getBaseClass();
  $build = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'container__background_image__inner',
    ],
    '#bootstrap_ignore_pre_render' => TRUE,
    '#bootstrap_ignore_process' => TRUE,
    '#attributes' => [
      'class' => [
        "{$base_class}-inner",
      ],
    ],
  ];
  $build['image'] = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'container__background_image__inner__image',
    ],
    '#attributes' => [
      'class' => [
        $base_class,
        $background_image
          ->getCssClass(),
      ],
    ],
    '#bootstrap_ignore_pre_render' => TRUE,
    '#bootstrap_ignore_process' => TRUE,
  ];
  $build['overlay'] = [
    '#type' => 'container',
    '#theme_wrappers' => [
      'container__background_image__inner__overlay',
    ],
    '#attributes' => [
      'class' => [
        "{$base_class}-overlay",
      ],
    ],
    '#bootstrap_ignore_pre_render' => TRUE,
    '#bootstrap_ignore_process' => TRUE,
  ];

  // Attach the scrolling blur effect JavaScript, if necessary.
  $full_viewport = $background_image
    ->getSetting('full_viewport');
  $blur_type = $background_image
    ->getSetting('blur.type');
  if ($blur_type == BackgroundImageInterface::BLUR_SCROLL || $full_viewport && BackgroundImageInterface::BLUR_SCROLL_FULL_VIEWPORT) {
    $build['#attached']['library'][] = 'background_image/scrolling.blur';
    $build['#attached']['drupalSettings']['backgroundImage']['blur'] = $background_image
      ->getSettings()
      ->drupalSettings('blur');
    $build['#attached']['drupalSettings']['backgroundImage']['fullViewport'] = $background_image
      ->getSettings()
      ->drupalSettings('full_viewport');
  }

  // Preload the necessary background background image.
  // @see https://www.smashingmagazine.com/2016/02/preload-what-is-it-good-for/
  $build['#attached']['html_head_link'][][] = [
    'rel' => 'preload',
    'href' => $background_image
      ->getImageUrl($manager
      ->getPreloadImageStyle()),
    'as' => 'image',
  ];

  // Attach the necessary background image CSS.
  // Due to the dynamic nature of how these are generated, this must be
  // attached via html_head_link instead of a library.
  // @see \Drupal\background_image\Controller\BackgroundImageCssController::deliver
  $build['#attached']['html_head_link'][][] = [
    'rel' => 'stylesheet',
    'href' => file_url_transform_relative(file_create_url($background_image
      ->getCssUri())) . '?' . \Drupal::state()
      ->get('system.css_js_query_string') ?: '0',
    'media' => 'all',
  ];
  $build['#cache']['contexts'][] = 'background_image';
  $build['#cache']['contexts'][] = 'background_image.settings.blur';
  $build['#cache']['contexts'][] = 'background_image.settings.full_viewport';
  $context = [
    'background_image' => $background_image,
    'entity' => $this
      ->getEntity($background_image, $manager),
  ];
  $this
    ->moduleHandler()
    ->alter('background_image_build', $build, $context);
  \Drupal::service('theme.manager')
    ->alter('background_image_build', $build, $context);
  return $build;
}