You are here

public function EntityHelper::getEntityFromUrlObject in Simple XML sitemap 8.3

Same name and namespace in other branches
  1. 8.2 src/EntityHelper.php \Drupal\simple_sitemap\EntityHelper::getEntityFromUrlObject()

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/EntityHelper.php, line 164

Class

EntityHelper
Helper class for working with entities.

Namespace

Drupal\simple_sitemap

Code

public function getEntityFromUrlObject(Url $url_object) {
  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;
}