You are here

public function BackgroundImageManager::getEntityFromCurrentRoute in Background Image 8

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

Retrieves the current route entity object.

Parameters

string $entity_type: The type of entity to retrieve from the current route.

string $bundle: The entity bundle type to retrieve from the current route.

Return value

\Drupal\Core\Entity\EntityInterface|null The current route entity object, if one exists.

Overrides BackgroundImageManagerInterface::getEntityFromCurrentRoute

1 call to BackgroundImageManager::getEntityFromCurrentRoute()
BackgroundImageManager::getBackgroundImage in src/BackgroundImageManager.php
Retrieves a background image based on the current route.

File

src/BackgroundImageManager.php, line 524

Class

BackgroundImageManager

Namespace

Drupal\background_image

Code

public function getEntityFromCurrentRoute($entity_type = NULL, $bundle = NULL) {

  /** @var \Drupal\Core\Entity\EntityInterface $entity */
  $entity = NULL;
  $parameters = $this->routeMatch
    ->getParameters();

  // If an entity type was passed, attempt to retrieve that object by name.
  if ($entity_type && $entity_type !== 'view' && ($parameter = $parameters
    ->get($entity_type)) && $this
    ->validEntity($parameter)) {
    $entity = $parameter;
  }
  else {
    if (($view_id = $parameters
      ->get('view_id')) && ($display_id = $parameters
      ->get('display_id')) && ($view = $this->entityTypeManager
      ->getStorage('view')
      ->load($view_id))) {

      /** @var \Drupal\views\ViewEntityInterface $view */
      $executable = $view
        ->getExecutable();
      $executable
        ->setDisplay($display_id);
      $entity = $view;
    }
    else {
      foreach ($parameters
        ->all() as $parameter) {
        if ($parameter instanceof EntityInterface && $this
          ->validEntity($parameter)) {
          $entity = $parameter;
          break;
        }
      }
    }
  }

  // If no bundle was provided, just return the entity.
  if (!isset($bundle)) {
    return $entity;
  }

  // Check if bundle matches.
  return $entity
    ->bundle() === $bundle ? $entity : NULL;
}