You are here

protected static function LinkAutocompleteFormTrait::getUriAsDisplayableString in Helper 8

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 The displayable string.

See also

static::getUserEnteredStringAsUri()

\Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUriAsDisplayableString()

1 call to LinkAutocompleteFormTrait::getUriAsDisplayableString()
LinkAutocompleteFormTrait::getLinkAutocompleteElement in src/LinkAutocompleteFormTrait.php
Generates a autocomplete link element for a form.

File

src/LinkAutocompleteFormTrait.php, line 98

Class

LinkAutocompleteFormTrait
Provides helpers for adding a content autocomplete link element to a form.

Namespace

Drupal\helper

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') {
    [
      $entity_type,
      $entity_id,
    ] = explode('/', substr($uri, 7), 2);

    // Show the 'entity:' URI as the entity autocomplete would.
    if ($entity = \Drupal::entityTypeManager()
      ->getStorage($entity_type)
      ->load($entity_id)) {
      $displayable_string = EntityAutocomplete::getEntityLabels([
        $entity,
      ]);
    }
  }
  elseif ($scheme === 'route') {
    $displayable_string = ltrim($displayable_string, 'route:');
  }
  return $displayable_string;
}