function ContentEntityNormalizer::getEntityUri

Same name in other branches
  1. 8.9.x core/modules/hal/src/Normalizer/ContentEntityNormalizer.php \Drupal\hal\Normalizer\ContentEntityNormalizer::getEntityUri()

Constructs the entity URI.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity.

array $context: Normalization/serialization context.

Return value

string The entity URI.

1 call to ContentEntityNormalizer::getEntityUri()
ContentEntityNormalizer::normalize in core/modules/hal/src/Normalizer/ContentEntityNormalizer.php
1 method overrides ContentEntityNormalizer::getEntityUri()
FileEntityNormalizer::getEntityUri in core/modules/hal/src/Normalizer/FileEntityNormalizer.php
Constructs the entity URI.

File

core/modules/hal/src/Normalizer/ContentEntityNormalizer.php, line 200

Class

ContentEntityNormalizer
Converts the Drupal entity object structure to a HAL array structure.

Namespace

Drupal\hal\Normalizer

Code

protected function getEntityUri(EntityInterface $entity, array $context = []) {
    // Some entity types don't provide a canonical link template.
    if ($entity->isNew()) {
        return '';
    }
    $route_name = 'rest.entity.' . $entity->getEntityTypeId() . '.GET';
    if ($entity->hasLinkTemplate('canonical')) {
        $url = $entity->toUrl('canonical');
    }
    elseif (\Drupal::service('router.route_provider')->getRoutesByNames([
        $route_name,
    ])) {
        $url = Url::fromRoute('rest.entity.' . $entity->getEntityTypeId() . '.GET', [
            $entity->getEntityTypeId() => $entity->id(),
        ]);
    }
    else {
        return '';
    }
    $url->setAbsolute(TRUE);
    if (!$url->isExternal()) {
        $url->setRouteParameter('_format', 'hal_json');
    }
    $generated_url = $url->toString(TRUE);
    $this->addCacheableDependency($context, $generated_url);
    return $generated_url->getGeneratedUrl();
}

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