W3cubDocs

/Drupal 8

public function Entity::toUrl

public 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.

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

File

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

Class

Entity
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()));
  }

  // 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(array('-', 'drupal:'), array('_', ''), $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->entityManager()->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);
}

© 2001–2016 by the original authors
Licensed under the GNU General Public License, version 2 and later.
Drupal is a registered trademark of Dries Buytaert.
https://api.drupal.org/api/drupal/core!lib!Drupal!Core!Entity!Entity.php/function/Entity::toUrl/8.1.x