You are here

protected function LoginDestinationRuleForm::getUserEnteredStringAsUri in Login Destination 8

Same name and namespace in other branches
  1. 8.2 src/Form/LoginDestinationRuleForm.php \Drupal\login_destination\Form\LoginDestinationRuleForm::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

LinkWidget::getUserEnteredStringAsUri()

1 call to LoginDestinationRuleForm::getUserEnteredStringAsUri()
LoginDestinationRuleForm::validateUriElement in src/Form/LoginDestinationRuleForm.php
Form element validation handler for the 'uri' element.

File

src/Form/LoginDestinationRuleForm.php, line 305

Class

LoginDestinationRuleForm
Base for controller for login destination add/edit forms.

Namespace

Drupal\login_destination\Form

Code

protected function getUserEnteredStringAsUri($string) {

  // By default, assume the entered string is an URI.
  $uri = $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 (!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>'));
    }
    if (strpos($string, '[') === 0) {
      $string = '/' . $string;
    }
    $uri = 'internal:' . $string;
  }
  return $uri;
}