diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php index 62d73b6..8fea63a 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidget.php @@ -63,10 +63,13 @@ public function elementValidate($element, &$form_state, $form) { $value = ''; if (!empty($element['#value'])) { // Take "label (entity id)', match the id from parenthesis. - if (preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) { + if ($this->isContentReferenced() && preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) { $value = $matches[1]; } - else { + elseif (preg_match("/.+\(([\w.]+)\)/", $element['#value'], $matches)) { + $matches[1]; + } + if (!$value) { // Try to get a match from the input string when the user didn't use the // autocomplete but filled in a value manually. $handler = \Drupal::service('plugin.manager.entity_reference.selection')->getSelectionHandler($this->fieldDefinition); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php index 42b565f..d23916c 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/field/widget/AutocompleteWidgetBase.php @@ -195,4 +195,14 @@ protected function getSelectionHandlerSetting($setting_name) { return isset($settings[$setting_name]) ? $settings[$setting_name] : NULL; } + /** + * Checks whether a content entity is referenced. + * + * @return bool + */ + protected function isContentReferenced() { + $target_type = $this->getFieldSetting('target_type'); + $target_type_info = \Drupal::entityManager()->getDefinition($target_type); + return is_subclass_of($target_type_info['class'], '\Drupal\Core\Entity\ContentEntityInterface'); + } }