function metatag_get_route_entity in Metatag 8
Returns the entity of the current route.
Return value
Drupal\Core\Entity\EntityInterface The entity or NULL if this is not an entity route.
4 calls to metatag_get_route_entity()
- MetatagManager::getDefaultMetatags in src/
MetatagManager.php - Returns default meta tags for an entity.
- metatag_get_default_tags in ./
metatag.module - Returns default tags for the current route.
- metatag_get_tags_from_route in ./
metatag.module - Load the meta tags by processing the route parameters.
- metatag_tokens in ./
metatag.tokens.inc - Implements hook_tokens().
File
- ./
metatag.module, line 353 - Contains metatag.module.
Code
function metatag_get_route_entity() {
$route_match = \Drupal::routeMatch();
$route_name = $route_match
->getRouteName();
// Look for a canonical entity view page, e.g. node/{nid}, user/{uid}, etc.
$matches = [];
preg_match('/entity\\.(.*)\\.(latest[_-]version|canonical)/', $route_name, $matches);
if (!empty($matches[1])) {
$entity_type = $matches[1];
return $route_match
->getParameter($entity_type);
}
// Look for a rest entity view page, e.g. "node/{nid}?_format=json", etc.
$matches = [];
// Matches e.g. "rest.entity.node.GET.json".
preg_match('/rest\\.entity\\.(.*)\\.(.*)\\.(.*)/', $route_name, $matches);
if (!empty($matches[1])) {
$entity_type = $matches[1];
return $route_match
->getParameter($entity_type);
}
// Look for entity object 'add' pages, e.g. "node/add/{bundle}".
$route_name_matches = [];
preg_match('/(entity\\.)?(.*)\\.add(_form)?/', $route_name, $route_name_matches);
if (!empty($route_name_matches[2])) {
$entity_type = $route_name_matches[2];
$definition = Drupal::entityTypeManager()
->getDefinition($entity_type, FALSE);
if (!empty($definition)) {
$type = $route_match
->getRawParameter($definition
->get('bundle_entity_type'));
if (!empty($type)) {
return \Drupal::entityTypeManager()
->getStorage($entity_type)
->create([
$definition
->get('entity_keys')['bundle'] => $type,
]);
}
}
}
// Look for entity object 'edit' pages, e.g. "node/{entity_id}/edit".
$route_name_matches = [];
preg_match('/entity\\.(.*)\\.edit_form/', $route_name, $route_name_matches);
if (!empty($route_name_matches[1])) {
$entity_type = $route_name_matches[1];
$entity_id = $route_match
->getRawParameter($entity_type);
if (!empty($entity_id)) {
return \Drupal::entityTypeManager()
->getStorage($entity_type)
->load($entity_id);
}
}
// Look for entity object 'add content translation' pages, e.g.
// "node/{nid}/translations/add/{source_lang}/{translation_lang}".
$route_name_matches = [];
preg_match('/(entity\\.)?(.*)\\.content_translation_add/', $route_name, $route_name_matches);
if (!empty($route_name_matches[2])) {
$entity_type = $route_name_matches[2];
$definition = Drupal::entityTypeManager()
->getDefinition($entity_type, FALSE);
if (!empty($definition)) {
$node = $route_match
->getParameter($entity_type);
$type = $node
->bundle();
if (!empty($type)) {
return \Drupal::entityTypeManager()
->getStorage($entity_type)
->create([
$definition
->get('entity_keys')['bundle'] => $type,
]);
}
}
}
// Special handling for the admin user_create page. In this case, there's only
// one bundle and it's named the same as the entity type, so some shortcuts
// can be used.
if ($route_name == 'user.admin_create') {
$entity_type = $type = 'user';
$definition = Drupal::entityTypeManager()
->getDefinition($entity_type);
if (!empty($type)) {
return \Drupal::entityTypeManager()
->getStorage($entity_type)
->create([
$definition
->get('entity_keys')['bundle'] => $type,
]);
}
}
// Trigger hook_metatag_route_entity().
if ($entities = \Drupal::moduleHandler()
->invokeAll('metatag_route_entity', [
$route_match,
])) {
return reset($entities);
}
return NULL;
}