8.5.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.0.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.1.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.2.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.3.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.4.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])
8.6.x Entity.php Entity::toUrl($rel = 'canonical', array $options = [])

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.


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.




Overrides EntityInterface::toUrl

4 calls to Entity::toUrl()
ConfigEntityBase::toUrl in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
Gets the URL object for the entity.
Entity::toLink in core/lib/Drupal/Core/Entity/Entity.php
Generates the HTML for a link to this entity.
Entity::url in core/lib/Drupal/Core/Entity/Entity.php
Gets the public URL for this entity.
Entity::urlInfo in core/lib/Drupal/Core/Entity/Entity.php
Gets the URL object for the entity.
1 method overrides Entity::toUrl()
ConfigEntityBase::toUrl in core/lib/Drupal/Core/Config/Entity/ConfigEntityBase.php
Gets the URL object for the entity.


core/lib/Drupal/Core/Entity/Entity.php, line 177


Defines a base entity class.




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

  // The links array might contain URI templates set in annotations.
  $link_templates = $this

  // 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
    $route_name = "entity.{$this-&gt;<span class="php-function-or-constant property member-of-self">entityTypeId</span>}." . str_replace(array(
    ), array(
    ), $rel);
    $uri = new Url($route_name, $route_parameters);
  else {
    $bundle = $this

    // A bundle-specific callback takes precedence over the generic one for
    // the entity type.
    $bundles = $this
    if (isset($bundles[$bundle]['uri_callback'])) {
      $uri_callback = $bundles[$bundle]['uri_callback'];
    elseif ($entity_uri_callback = $this
      ->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
        -&gt;<span class="php-function-or-constant function member-of-self">getEntityTypeId</span>()}' entity type");

  // Pass the entity data through as options, so that alter functions do not
  // need to look up this entity again.
    ->setOption('entity_type', $this
    ->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, [
  ], TRUE)) {
    $options += [
      'language' => $this
  $uri_options = $uri
  $uri_options += $options;
  return $uri