From a53ba5946db6efd1c5268c2df95693f8ceb411a7 Mon Sep 17 00:00:00 2001 From: Trent Crawford Date: Thu, 2 May 2024 15:14:55 +0200 Subject: [PATCH] Issue 2423093: Re-roll #60 as it was failing silently to fully apply. --- .../Plugin/Field/FieldWidget/LinkWidget.php | 25 +++++++++++++++---- .../EntityAutocompleteController.php | 9 +++++-- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php index 97015d5bef..d1736c4229 100644 --- a/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php +++ b/core/modules/link/src/Plugin/Field/FieldWidget/LinkWidget.php @@ -9,8 +9,10 @@ use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\link\LinkItemInterface; +use Drupal\taxonomy\Entity\Term; use Symfony\Component\Validator\ConstraintViolation; use Symfony\Component\Validator\ConstraintViolationListInterface; +use Drupal\node\Entity\Node; /** * Plugin implementation of the 'link' widget. @@ -98,13 +100,15 @@ protected static function getUriAsDisplayableString($uri) { * * @param string $string * The user-entered string. + * @param string $langcode + * The langcode. * * @return string * The URI, if a non-empty $uri was passed. * * @see static::getUriAsDisplayableString() */ - protected static function getUserEnteredStringAsUri($string) { + protected static function getUserEnteredStringAsUri($string, $langcode = NULL) { // By default, assume the entered string is a URI. $uri = trim($string); @@ -114,6 +118,17 @@ protected static function getUserEnteredStringAsUri($string) { // @todo Support entity types other than 'node'. Will be fixed in // https://www.drupal.org/node/2423093. $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. elseif (in_array($string, ['', '', '