Created
March 29, 2017 18:26
-
-
Save slivorezka/312a0a890e2314cf67554bab930b6c35 to your computer and use it in GitHub Desktop.
Drupal 8: Get Entity By Alias
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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