public function EntityBase::toUrl in Drupal 10
Same name and namespace in other branches
- 8 core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
- 9 core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
2 methods override EntityBase::toUrl()
- ConfigEntityBase::toUrl in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
- Gets the URL object for the entity.
- EntityTestExternal::toUrl in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.php
- Gets the URL object for the entity.
File
- core/lib/Drupal/Core/Entity/EntityBase.php, line 159
Class
- EntityBase
- Defines a base entity class.
Namespace
Drupal\Core\Entity
Code
public function toUrl($rel = 'canonical', array $options = []) {
if ($this
->id() === NULL) {
throw new EntityMalformedException(sprintf('The "%s" entity cannot have a URI as it does not have an ID', $this
->getEntityTypeId()));
}
$link_templates = $this
->linkTemplates();
if ($rel === 'revision' && $this instanceof RevisionableInterface && $this
->isDefaultRevision()) {
$rel = 'canonical';
}
if (isset($link_templates[$rel])) {
$route_parameters = $this
->urlRouteParameters($rel);
$route_name = "entity.{$this->entityTypeId}." . str_replace([
'-',
'drupal:',
], [
'_',
'',
], $rel);
$uri = new Url($route_name, $route_parameters);
}
else {
$bundle = $this
->bundle();
$bundles = $this
->entityTypeBundleInfo()
->getBundleInfo($this
->getEntityTypeId());
if (isset($bundles[$bundle]['uri_callback'])) {
$uri_callback = $bundles[$bundle]['uri_callback'];
}
elseif ($entity_uri_callback = $this
->getEntityType()
->getUriCallback()) {
$uri_callback = $entity_uri_callback;
}
if (isset($uri_callback) && is_callable($uri_callback)) {
$uri = call_user_func($uri_callback, $this);
}
else {
throw new UndefinedLinkTemplateException("No link template '{$rel}' found for the '{$this->getEntityTypeId()}' entity type");
}
}
$uri
->setOption('entity_type', $this
->getEntityTypeId())
->setOption('entity', $this);
if (!in_array($rel, [
'collection',
'add-page',
'add-form',
], TRUE)) {
$options += [
'language' => $this
->language(),
];
}
$uri_options = $uri
->getOptions();
$uri_options += $options;
return $uri
->setOptions($uri_options);
}