You are here

protected static function LinkWidget::getUriAsDisplayableString in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUriAsDisplayableString()

Gets the URI without the 'internal:' or 'entity:' scheme.

The following two forms of URIs are transformed:

  • 'entity:' URIs: to entity autocomplete ("label (entity id)") strings;
  • 'internal:' URIs: the scheme is stripped.

This method is the inverse of ::getUserEnteredStringAsUri().

Parameters

string $uri: The URI to get the displayable string for.

Return value

string

See also

static::getUserEnteredStringAsUri()

2 calls to LinkWidget::getUriAsDisplayableString()
LinkWidget::flagErrors in core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Override the '%uri' message parameter, to ensure that 'internal:' URIs show a validation error message that doesn't mention that scheme.
LinkWidget::formElement in core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Returns the form for a single field widget.

File

core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php, line 57
Contains \Drupal\link\Plugin\Field\FieldWidget\LinkWidget.

Class

LinkWidget
Plugin implementation of the 'link' widget.

Namespace

Drupal\link\Plugin\Field\FieldWidget

Code

protected static function getUriAsDisplayableString($uri) {
  $scheme = parse_url($uri, PHP_URL_SCHEME);

  // By default, the displayable string is the URI.
  $displayable_string = $uri;

  // A different displayable string may be chosen in case of the 'internal:'
  // or 'entity:' built-in schemes.
  if ($scheme === 'internal') {
    $uri_reference = explode(':', $uri, 2)[1];

    // @todo '<front>' is valid input for BC reasons, may be removed by
    //   https://www.drupal.org/node/2421941
    $path = parse_url($uri, PHP_URL_PATH);
    if ($path === '/') {
      $uri_reference = '<front>' . substr($uri_reference, 1);
    }
    $displayable_string = $uri_reference;
  }
  elseif ($scheme === 'entity') {
    list($entity_type, $entity_id) = explode('/', substr($uri, 7), 2);

    // Show the 'entity:' URI as the entity autocomplete would.
    $entity_manager = \Drupal::entityManager();
    if ($entity_manager
      ->getDefinition($entity_type, FALSE) && ($entity = \Drupal::entityManager()
      ->getStorage($entity_type)
      ->load($entity_id))) {
      $displayable_string = EntityAutocomplete::getEntityLabels(array(
        $entity,
      ));
    }
  }
  return $displayable_string;
}