function EntityBase::toUrl

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
  2. 10 core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()
  3. 11.x core/lib/Drupal/Core/Entity/EntityBase.php \Drupal\Core\Entity\EntityBase::toUrl()

Overrides EntityInterface::toUrl

4 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.
EntityTestExternal::toUrl in core/modules/system/tests/modules/entity_test/src/Entity/EntityTestExternal.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 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()));
    }
    // 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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.