You are here

protected static function LinkAutocompleteFormTrait::getUserEnteredStringAsUri in Helper 8

Gets the user-entered string as a URI.

The following two forms of input are mapped to URIs:

  • entity autocomplete ("label (entity id)") strings: to 'entity:' URIs;
  • strings without a detectable scheme: to 'internal:' URIs.

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

Parameters

string $entity_type: The entity type.

string $string: The user-entered string.

Return value

string The URI, if a non-empty $uri was passed.

See also

static::getUriAsDisplayableString()

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

1 call to LinkAutocompleteFormTrait::getUserEnteredStringAsUri()
LinkAutocompleteFormTrait::validateLinkAutocompleteElement in src/LinkAutocompleteFormTrait.php
Validation callback for URL elements.

File

src/LinkAutocompleteFormTrait.php, line 151

Class

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

Namespace

Drupal\helper

Code

protected static function getUserEnteredStringAsUri($entity_type, $string) {

  // By default, assume the entered string is an URI.
  $uri = trim($string);

  // Detect entity autocomplete string, map to 'entity:' URI.
  $entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
  if ($entity_id !== NULL) {
    $uri = 'entity:' . $entity_type . '/' . $entity_id;
  }
  elseif (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) {

    // @todo '<front>' is valid input for BC reasons, may be removed by https://www.drupal.org/node/2421941
    // - '<front>' -> '/'
    // - '<front>#foo' -> '/#foo'
    if (strpos($string, '<front>') === 0) {
      $string = '/' . substr($string, strlen('<front>'));
    }
    $uri = 'internal:' . $string;
  }
  return $uri;
}