Skip to content
Snippets Groups Projects

Resolve #2423093 "Allow multiple target"

2 files
+ 28
7
Compare changes
  • Side-by-side
  • Inline
Files
2
@@ -10,6 +10,8 @@
@@ -10,6 +10,8 @@
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Field\WidgetBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\link\LinkItemInterface;
use Drupal\link\LinkItemInterface;
 
use Drupal\node\Entity\Node;
 
use Drupal\taxonomy\Entity\Term;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
@@ -75,7 +77,7 @@ protected static function getUriAsDisplayableString($uri) {
@@ -75,7 +77,7 @@ protected static function getUriAsDisplayableString($uri) {
// Show the 'entity:' URI as the entity autocomplete would.
// Show the 'entity:' URI as the entity autocomplete would.
// @todo Support entity types other than 'node'. Will be fixed in
// @todo Support entity types other than 'node'. Will be fixed in
// https://www.drupal.org/node/2423093.
// https://www.drupal.org/node/2423093.
if ($entity_type == 'node' && $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id)) {
if (in_array($entity_type, ['node', 'taxonomy_term']) && $entity = \Drupal::entityTypeManager()->getStorage($entity_type)->load($entity_id)) {
$displayable_string = EntityAutocomplete::getEntityLabels([$entity]);
$displayable_string = EntityAutocomplete::getEntityLabels([$entity]);
}
}
}
}
@@ -97,13 +99,15 @@ protected static function getUriAsDisplayableString($uri) {
@@ -97,13 +99,15 @@ protected static function getUriAsDisplayableString($uri) {
*
*
* @param string $string
* @param string $string
* The user-entered string.
* The user-entered string.
 
* @param string $langcode
 
* The langcode.
*
*
* @return string
* @return string
* The URI, if a non-empty $uri was passed.
* The URI, if a non-empty $uri was passed.
*
*
* @see static::getUriAsDisplayableString()
* @see static::getUriAsDisplayableString()
*/
*/
protected static function getUserEnteredStringAsUri($string) {
protected static function getUserEnteredStringAsUri($string, $langcode = NULL) {
// By default, assume the entered string is a URI.
// By default, assume the entered string is a URI.
$uri = trim($string);
$uri = trim($string);
@@ -113,6 +117,17 @@ protected static function getUserEnteredStringAsUri($string) {
@@ -113,6 +117,17 @@ protected static function getUserEnteredStringAsUri($string) {
// @todo Support entity types other than 'node'. Will be fixed in
// @todo Support entity types other than 'node'. Will be fixed in
// https://www.drupal.org/node/2423093.
// https://www.drupal.org/node/2423093.
$uri = 'entity:node/' . $entity_id;
$uri = 'entity:node/' . $entity_id;
 
$node = Node::load($entity_id);
 
// On layout builder pages, the langcode is not passed.
 
if (is_null($langcode) && str_contains(\Drupal::routeMatch()->getRouteName(), 'layout_builder')) {
 
$langcode = \Drupal::languageManager()->getCurrentLanguage()->getId();
 
}
 
if ($node && isset($langcode) && $node->isTranslatable() && $node->hasTranslation($langcode)) {
 
$node = $node->getTranslation($langcode);
 
}
 
if (!$node || (!str_contains($string, $node->label()) && Term::load($entity_id) !== NULL)) {
 
$uri = 'entity:taxonomy_term/' . $entity_id;
 
}
}
}
// Support linking to nothing.
// Support linking to nothing.
elseif (in_array($string, ['<nolink>', '<none>', '<button>'], TRUE)) {
elseif (in_array($string, ['<nolink>', '<none>', '<button>'], TRUE)) {
@@ -139,7 +154,7 @@ protected static function getUserEnteredStringAsUri($string) {
@@ -139,7 +154,7 @@ protected static function getUserEnteredStringAsUri($string) {
* Disallows saving inaccessible or untrusted URLs.
* Disallows saving inaccessible or untrusted URLs.
*/
*/
public static function validateUriElement($element, FormStateInterface $form_state, $form) {
public static function validateUriElement($element, FormStateInterface $form_state, $form) {
$uri = static::getUserEnteredStringAsUri($element['#value']);
$uri = static::getUserEnteredStringAsUri($element['#value'], $form_state->get('langcode'));
$form_state->setValueForElement($element, $uri);
$form_state->setValueForElement($element, $uri);
// If getUserEnteredStringAsUri() mapped the entered value to an 'internal:'
// If getUserEnteredStringAsUri() mapped the entered value to an 'internal:'
@@ -215,9 +230,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
@@ -215,9 +230,9 @@ public function formElement(FieldItemListInterface $items, $delta, array $elemen
// 'url' form element and we have to do the validation ourselves.
// 'url' form element and we have to do the validation ourselves.
if ($this->supportsInternalLinks()) {
if ($this->supportsInternalLinks()) {
$element['uri']['#type'] = 'entity_autocomplete';
$element['uri']['#type'] = 'entity_autocomplete';
// @todo The user should be able to select an entity type. Will be fixed
// Dirty hack around https://www.drupal.org/node/2423093
// in https://www.drupal.org/node/2423093.
// @todo: remove when the issue is fixed.
$element['uri']['#target_type'] = 'node';
$element['uri']['#target_type'] = 'node,taxonomy_term';
// Disable autocompletion when the first character is '/', '#' or '?'.
// Disable autocompletion when the first character is '/', '#' or '?'.
$element['uri']['#attributes']['data-autocomplete-first-character-denylist'] = '/#?';
$element['uri']['#attributes']['data-autocomplete-first-character-denylist'] = '/#?';
Loading