Change record status: 
Project: 
Introduced in branch: 
8.x
Description: 

An entity can have any number of public URLs, like the canonical path where it is viewed or any of its forms. Sometimes the raw values are needed, in order to use for a link, or as part of a larger renderable item.

Getting the public URL for an entity:

// Before
$uri = $entity->uri();
$url = url($uri['path'], $uri['options']);

// After
$url = $entity->url();

Getting the internal path for an entity:

// Before
$uri = $entity->uri();
$path = $uri['path'];

// After
$path = $entity->urlInfo();

Getting the raw URL information:

// Before
$uri = $entity->uri();
$path = $uri['path'];
$options = $uri['options'];

// After
$url = $entity->urlInfo();
$path = $url->getPath();
$route_name = $url->getRouteName();
$route_parameters = $url->getRouteParameters();
$options = $url->getOptions();

Adding URL information to an array of links:

// Before
$uri = $entity->uri();
$operations['enable'] = array(
  'title' => t('Enable'),
  'href' => $uri['path'] . '/enable',
  'options' => $uri['options'],
  'weight' => -10,
);

// After
$operations['enable'] = array(
  'title' => t('Enable'),
  'weight' => -10,
) + $entity->urlInfo('enable')->toArray();

Adding URL information to a render array:

// Before
$uri = $entity->uri();
$elements[$delta] = array(
  '#type' => 'link',
  '#title' => $label,
  '#href' => $uri['path'],
  '#options' => $uri['options'],
);

// After
$elements[$delta] = array(
  '#type' => 'link',
  '#title' => $label,
) + $entity->urlInfo()->toRenderArray();

Internally, each entity type was able to provide extra values during the generation of the route parameters. Previously, this was done by a method uriPlaceholderReplacements() that specified its keys wrapped with curly braces {}.
Now it is specified by urlRouteParameters() and the keys are not wrapped.

Impacts: 
Module developers

Comments

duaelfr’s picture

Beware!
EntityInterface::getSystemPath() has been removed in favor of EntityInterface::urlInfo()
See #2476059: Remove EntityInterface->getSystemPath(), all its implementations and related usage

Not a man. Empathy buddy & NVC trainee. Ally.