diff --git a/core/modules/link/config/install/link.settings.yml b/core/modules/link/config/install/link.settings.yml new file mode 100644 index 0000000..8015388 --- /dev/null +++ b/core/modules/link/config/install/link.settings.yml @@ -0,0 +1,4 @@ +autocomplete_target_types: + node: + selection_handler: default + selection_settings: { } diff --git a/core/modules/link/config/schema/link.schema.yml b/core/modules/link/config/schema/link.schema.yml index fd2edb1..eee8bc2 100644 --- a/core/modules/link/config/schema/link.schema.yml +++ b/core/modules/link/config/schema/link.schema.yml @@ -83,3 +83,16 @@ field.value.link: label: 'Link attributes' sequence: type: string + +link.settings: + type: mapping + mapping: + autocomplete_target_types: + type: sequence + sequence: + type: mapping + mapping: + selection_handler: + type: string + selection_settings: + type: sequence diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index e1b1c2a..5eb46ff 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -2,10 +2,10 @@ namespace Drupal\link\Plugin\Field\FieldWidget; -use Drupal\Core\Entity\Element\EntityAutocomplete; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; +use Drupal\dynamic_entity_autocomplete\Element\DynamicEntityAutocomplete; use Drupal\link\LinkItemInterface; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationListInterface; @@ -74,7 +74,7 @@ protected static function getUriAsDisplayableString($uri) { // 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([$entity]); + $displayable_string = DynamicEntityAutocomplete::getEntityLabels([$entity]); } } @@ -103,11 +103,13 @@ protected static function getUserEnteredStringAsUri($string) { $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; + $entities = DynamicEntityAutocomplete::getEntityIdsByEntityTypeFromInput($string); + if (!empty($entities)) { + $entity_type_id = key($entities); + $entity_id = array_shift($entities[$entity_type_id]); + if ($entity_type_id && $entity_id) { + $uri = sprintf('entity:%s/%s', $entity_type_id, $entity_id); + } } // Detect a schemeless string, map to 'internal:' URI. elseif (!empty($string) && parse_url($string, PHP_URL_SCHEME) === NULL) { @@ -175,15 +177,14 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen '#element_validate' => [[get_called_class(), 'validateUriElement']], '#maxlength' => 2048, '#required' => $element['#required'], + '#link_type' => $this->getFieldSetting('link_type'), ]; // If the field is configured to support internal links, it cannot use the // 'url' form element and we have to do the validation ourselves. if ($this->supportsInternalLinks()) { - $element['uri']['#type'] = 'entity_autocomplete'; - // @todo The user should be able to select an entity type. Will be fixed - // in https://www.drupal.org/node/2423093. - $element['uri']['#target_type'] = 'node'; + $element['uri']['#type'] = 'dynamic_entity_autocomplete'; + $element['uri']['#target_types'] = \Drupal::config('link.settings')->get('autocomplete_target_types'); // Disable autocompletion when the first character is '/', '#' or '?'. $element['uri']['#attributes']['data-autocomplete-first-character-blacklist'] = '/#?';