diff --git a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php index a9a62f3..401789a 100644 --- a/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php +++ b/core/lib/Drupal/Core/Entity/Plugin/DataType/EntityReferenceItem.php @@ -66,11 +66,6 @@ public function getPropertyDefinitions() { static::$propertyDefinitions[$key]['target_id'] = array( 'type' => 'string', 'label' => t('Entity ID'), - 'constraints' => array( - 'Length' => array( - 'min' => 0, - ), - ), ); } 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 8fea63a..884cd44 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,11 +63,13 @@ public function elementValidate($element, &$form_state, $form) { $value = ''; if (!empty($element['#value'])) { // Take "label (entity id)', match the id from parenthesis. + // @todo: Lookup the entity type's ID data type and use it here. + // https://drupal.org/node/2107249 if ($this->isContentReferenced() && preg_match("/.+\((\d+)\)/", $element['#value'], $matches)) { $value = $matches[1]; } elseif (preg_match("/.+\(([\w.]+)\)/", $element['#value'], $matches)) { - $matches[1]; + $value = $matches[1]; } if (!$value) { // Try to get a match from the input string when the user didn't use the 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 d23916c..31a357e 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 @@ -205,4 +205,5 @@ protected function isContentReferenced() { $target_type_info = \Drupal::entityManager()->getDefinition($target_type); return is_subclass_of($target_type_info['class'], '\Drupal\Core\Entity\ContentEntityInterface'); } + } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php index 1ef3608..4392471 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceAdminTest.php @@ -15,7 +15,7 @@ class EntityReferenceAdminTest extends WebTestBase { public static function getInfo() { return array( - 'name' => 'Entity Reference UI', + 'name' => 'Entity Reference admin UI', 'description' => 'Tests for the administrative UI.', 'group' => 'Entity Reference', ); diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceIntegrationTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceIntegrationTest.php new file mode 100644 index 0000000..2d17a78 --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceIntegrationTest.php @@ -0,0 +1,95 @@ + 'Entity reference components (widgets, formatters, etc.)', + 'description' => 'Tests for various Entity reference components.', + 'group' => 'Entity Reference', + ); + } + + /** + * {@inheritdoc} + */ + public function setUp() { + parent::setUp(); + + // Create a test user. + $web_user = $this->drupalCreateUser(array('administer entity_test content')); + $this->drupalLogin($web_user); + } + + /** + * Tests the autocomplete widget when targeting a config entity type. + */ + public function testConfigAutocompleteWidget() { + // Create an Entity reference field targeting a config entity type. + entity_reference_create_instance($this->entityType, $this->bundle, $this->fieldName, 'Field test', 'config_test'); + + // Add the field to the default form mode. + entity_get_form_display($this->entityType, $this->bundle, 'default')->setComponent($this->fieldName)->save(); + + // Create a test config entity. + $config_entity_id = $this->randomName(); + $config_entity_label = $this->randomString(); + $config_entity = entity_create('config_test', array('id' => $config_entity_id, 'label' => $config_entity_label)); + $config_entity->save(); + + $entity_name = $this->randomName(); + $edit = array( + 'name' => $entity_name, + 'user_id' => mt_rand(0, 128), + $this->fieldName . '[0][target_id]' => $config_entity_label . ' (' . $config_entity_id . ')', + ); + $this->drupalPostForm($this->entityType . '/add', $edit, t('Save')); + $entity = current(entity_load_multiple_by_properties($this->entityType, array('name' => $entity_name))); + + $this->assertTrue($entity, format_string('%entity_type: Entity found in the database.', array('%entity_type' => $this->entityType))); + $this->assertEqual($entity->{$this->fieldName}->target_id, $config_entity_id); + $this->assertEqual($entity->{$this->fieldName}->entity->id(), $config_entity_id); + $this->assertEqual($entity->{$this->fieldName}->entity->label(), $config_entity_label); + } + +}