You are here

public function EntityHelper::getEntityFromUrlObject in Simple XML sitemap 4.x

Gets the entity from URL object.

Parameters

\Drupal\Core\Url $url_object: The URL object.

Return value

\Drupal\Core\Entity\EntityInterface|null An entity object. NULL if no matching entity is found.

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

File

src/Entity/EntityHelper.php, line 161

Class

EntityHelper
Helper class for working with entities.

Namespace

Drupal\simple_sitemap\Entity

Code

public function getEntityFromUrlObject(Url $url_object) : ?EntityInterface {
  if ($url_object
    ->isRouted()) {

    // Fix for the homepage, see
    // https://www.drupal.org/project/simple_sitemap/issues/3194130.
    if ($url_object
      ->getRouteName() === '<front>' && !empty($uri = $this->configFactory
      ->get('system.site')
      ->get('page.front'))) {
      $url_object = Url::fromUri('internal:' . $uri);
    }
    if (!empty($route_parameters = $url_object
      ->getRouteParameters()) && $this->entityTypeManager
      ->getDefinition($entity_type_id = key($route_parameters), FALSE)) {
      return $this->entityTypeManager
        ->getStorage($entity_type_id)
        ->load($route_parameters[$entity_type_id]);
    }
  }
  return NULL;
}