function LinkWidget::getUserEnteredStringAsUri

Same name in other branches
  1. 8.9.x core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
  2. 10 core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()
  3. 11.x core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php \Drupal\link\Plugin\Field\FieldWidget\LinkWidget::getUserEnteredStringAsUri()

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 $string: The user-entered string.

Return value

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

See also

static::getUriAsDisplayableString()

2 calls to LinkWidget::getUserEnteredStringAsUri()
LinkWidget::massageFormValues in core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Massages the form values into the format expected for field values.
LinkWidget::validateUriElement in core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php
Form element validation handler for the 'uri' element.

File

core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php, line 107

Class

LinkWidget
Plugin implementation of the 'link' widget.

Namespace

Drupal\link\Plugin\Field\FieldWidget

Code

protected static function getUserEnteredStringAsUri($string) {
    // By default, assume the entered string is a URI.
    $uri = trim($string);
    // Detect entity autocomplete string, map to 'entity:' URI.
    $entity_id = EntityAutocomplete::extractEntityIdFromAutocompleteInput($string);
    if ($entity_id !== NULL) {
        // @todo Support entity types other than 'node'. Will be fixed in
        //   https://www.drupal.org/node/2423093.
        $uri = 'entity:node/' . $entity_id;
    }
    elseif (in_array($string, [
        '<nolink>',
        '<none>',
        '<button>',
    ], TRUE)) {
        $uri = 'route:' . $string;
    }
    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;
}

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