You are here

public function EntityPager::getEntity in Entity Pager 8

Same name and namespace in other branches
  1. 2.0.x src/EntityPager.php \Drupal\entity_pager\EntityPager::getEntity()

Gets the entity object this entity pager is for.

Return value

\Drupal\Core\Entity\EntityInterface|null The entity object or NULL if no entity found.

Overrides EntityPagerInterface::getEntity

3 calls to EntityPager::getEntity()
EntityPager::detokenize in src/EntityPager.php
Replaces all tokens in provided string.
EntityPager::getAllLink in src/EntityPager.php
Returns a Display All link render array.
EntityPager::getCurrentRow in src/EntityPager.php
Returns the currently active row from the view results.

File

src/EntityPager.php, line 101

Class

EntityPager
Entity pager object.

Namespace

Drupal\entity_pager

Code

public function getEntity() {
  $routeMatch = \Drupal::routeMatch();
  $route = $routeMatch
    ->getRouteObject();
  if ($route) {
    $parameters = $route
      ->getOption('parameters');
    if ($parameters) {
      foreach ($parameters as $name => $options) {
        if (isset($options['type']) && strpos($options['type'], 'entity:') === 0) {
          $candidate = $routeMatch
            ->getParameter($name);
          if ($candidate instanceof ContentEntityInterface && $candidate
            ->hasLinkTemplate('canonical')) {
            $entity = $candidate;
            break;
          }
        }
      }
    }
  }
  if (!$entity && \Drupal::request()->attributes
    ->has('entity')) {
    $entity = \Drupal::request()->attributes
      ->get('entity');
  }
  return $entity;
}