You are here

public function BackgroundImageManager::getBackgroundImage in Background Image 8

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

Retrieves a background image based on the current route.

Parameters

string $langcode: (optional) The language of the current context. Defaults to the current content language.

array $context: (optional) An associative array of arbitrary data that can be useful to determine the proper fallback sequence.

Return value

\Drupal\background_image\BackgroundImageInterface|null A background image entity, if one exists.

Overrides BackgroundImageManagerInterface::getBackgroundImage

File

src/BackgroundImageManager.php, line 332

Class

BackgroundImageManager

Namespace

Drupal\background_image

Code

public function getBackgroundImage($langcode = NULL, array $context = []) {
  $hash = Crypt::hashBase64(serialize(func_get_args()));
  if (!isset($this->backgroundImages[$hash])) {
    if ($entity = $this
      ->getEntityFromCurrentRoute()) {
      if ($entity instanceof EntityInterface && ($this->backgroundImages[$hash] = $this
        ->getEntityBackgroundImage($entity, $langcode, $context))) {
        return $this->backgroundImages[$hash];
      }
      else {
        if ($entity instanceof EntityInterface && ($this->backgroundImages[$hash] = $this
          ->getEntityBundleBackgroundImage($entity, $langcode, $context))) {
          return $this->backgroundImages[$hash];
        }
        else {
          if ($entity instanceof ViewEntityInterface && ($this->backgroundImages[$hash] = $this
            ->getViewBackgroundImage($entity, $langcode, $context))) {
            return $this->backgroundImages[$hash];
          }
        }
      }
    }
    $this->backgroundImages[$hash] = $this
      ->getGlobalBackgroundImage($langcode, $context) ?: FALSE;
  }
  return $this->backgroundImages[$hash];
}