Skip to content

Instantly share code, notes, and snippets.

@slivorezka
Created March 29, 2017 18:26
Show Gist options
  • Save slivorezka/312a0a890e2314cf67554bab930b6c35 to your computer and use it in GitHub Desktop.
Save slivorezka/312a0a890e2314cf67554bab930b6c35 to your computer and use it in GitHub Desktop.
Drupal 8: Get Entity By Alias
<?php
// Some entity alias (node, taxonomy_term, user, etc).
$alias = '/about';
// Get URL obj.
$url = Url::fromUri('internal:' . $alias);
// Check exist alias.
if ($url->isRouted()) {
$params = $url->getRouteParameters();
$entity_type = key($params);
// GEt entity.
$entity = $this->entityTypeManager->getStorage($entity_type)->load($params[$entity_type]);
// Return like "node" or "taxonomy_term".
dsm($entity->getEntityTypeId());
// Return entity obj.
dsm($entity);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment