public function EntityBase::toUrl in Drupal 8
Same name and namespace in other branches
- 9 core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
- 10 core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
Gets the URL object for the entity.
The entity must have an id already. Content entities usually get their IDs by saving them.
URI templates might be set in the links array in an annotation, for example:
links = {
"canonical" = "/node/{node}",
"edit-form" = "/node/{node}/edit",
"version-history" = "/node/{node}/revisions"
}
or specified in a callback function set like:
uri_callback = "comment_uri",
If the path is not set in the links array, the uri_callback function is used for setting the path. If this does not exist and the link relationship type is canonical, the path is set using the default template: entity/entityType/id.
Parameters
string $rel: The link relationship type, for example: canonical or edit-form.
array $options: See \Drupal\Core\Routing\UrlGeneratorInterface::generateFromRoute() for the available options.
Return value
\Drupal\Core\Url The URL object.
Throws
\Drupal\Core\Entity\EntityMalformedException
\Drupal\Core\Entity\Exception\UndefinedLinkTemplateException
Overrides EntityInterface::toUrl
6 calls to EntityBase::toUrl()
- Comment::permalink in core/
modules/ comment/ src/ Entity/ Comment.php - Returns the permalink URL for this comment.
- ConfigEntityBase::toUrl in core/
lib/ Drupal/ Core/ Config/ Entity/ ConfigEntityBase.php - Gets the URL object for the entity.
- EntityBase::toLink in core/
lib/ Drupal/ Core/ Entity/ EntityBase.php - Generates the HTML for a link to this entity.
- EntityBase::url in core/
lib/ Drupal/ Core/ Entity/ EntityBase.php - Gets the public URL for this entity.
- EntityBase::urlInfo in core/
lib/ Drupal/ Core/ Entity/ EntityBase.php - Gets the URL object for the entity.
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 190
Class
- EntityBase
- Defines a base entity class.
Namespace
Drupal\Core\EntityCode
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()));
}
// The links array might contain URI templates set in annotations.
$link_templates = $this
->linkTemplates();
// Links pointing to the current revision point to the actual entity. So
// instead of using the 'revision' link, use the 'canonical' link.
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();
// A bundle-specific callback takes precedence over the generic one for
// the entity type.
$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;
}
// Invoke the callback to get the URI. If there is no callback, use the
// default URI format.
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");
}
}
// Pass the entity data through as options, so that alter functions do not
// need to look up this entity again.
$uri
->setOption('entity_type', $this
->getEntityTypeId())
->setOption('entity', $this);
// Display links by default based on the current language.
// Link relations that do not require an existing entity should not be
// affected by this entity's language, however.
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);
}