diff --git a/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php b/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php deleted file mode 100644 index a60e65e..0000000 --- a/core/lib/Drupal/Core/Entity/Field/Type/EntityReferenceItem.php +++ /dev/null @@ -1,82 +0,0 @@ -definition['settings']['entity type']; - - if (!isset(self::$propertyDefinitions[$entity_type])) { - self::$propertyDefinitions[$entity_type]['value'] = array( - // @todo: Lookup the entity type's ID data type and use it here. - 'type' => 'integer', - 'label' => t('Entity ID'), - ); - self::$propertyDefinitions[$entity_type]['entity'] = array( - 'type' => 'entity', - 'constraints' => array( - 'entity type' => $entity_type, - ), - 'label' => t('Entity'), - 'description' => t('The referenced entity'), - // The entity object is computed out of the entity id. - 'computed' => TRUE, - 'read-only' => FALSE, - 'settings' => array('id source' => 'value'), - ); - } - return self::$propertyDefinitions[$entity_type]; - } - - /** - * Overrides FieldItemBase::setValue(). - */ - public function setValue($values) { - // Treat the values as property value of the entity field, if no array - // is given. - if (!is_array($values)) { - $values = array('entity' => $values); - } - - // Entity is computed out of the ID, so we only need to update the ID. Only - // set the entity field if no ID is given. - if (isset($values['value'])) { - $this->properties['value']->setValue($values['value']); - } - else { - $this->properties['entity']->setValue(isset($values['entity']) ? $values['entity'] : NULL); - } - unset($values['entity'], $values['value']); - if ($values) { - throw new InvalidArgumentException('Property ' . key($values) . ' is unknown.'); - } - } -} diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index bd93686..fe05898 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -29,11 +29,29 @@ function entity_reference_field_info() { ), 'default_widget' => 'entity_reference_autocomplete', 'default_formatter' => 'entity_reference_label', + 'field item class' => 'Drupal\entity_reference\Type\EntityReferenceItem', ); return $field_info; } /** + * Implements hook_entity_field_info(). + */ +function entity_reference_entity_field_info($entity_type) { + $property_info = array(); + foreach (field_info_instances($entity_type) as $bundle_name => $instances) { + foreach ($instances as $field_name => $instance) { + $field = field_info_field($field_name); + if ($field['type'] != 'entity_reference') { + continue; + } + $property_info['definitions'][$field_name]['settings']['target_type'] = $field['settings']['target_type']; + } + } + return $property_info; +} + +/** * Implements hook_menu(). */ function entity_reference_menu() { @@ -93,7 +111,7 @@ function entity_reference_field_presave($entity_type, $entity, $field, $instance $entity_info = entity_get_info($target_type); // Get the bundle. - if (count($instance['settings']['handler_settings']['target_bundles']) == 1) { + if (!empty($instance['settings']['handler_settings']['target_bundles']) && count($instance['settings']['handler_settings']['target_bundles']) == 1) { $bundle = reset($instance['settings']['handler_settings']['target_bundles']); } else { diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php index 897fb7e..bcda1e6 100644 --- a/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/views/style/EntityReference.php @@ -90,7 +90,9 @@ public function render() { foreach ($sets as $records) { foreach ($records as $values) { // Sanitize html, remove line breaks and extra whitespace. - $results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $this->row_plugin->render($values)))); + $output = $this->row_plugin->render($values); + $output = drupal_render($output); + $results[$values->{$id_field_alias}] = filter_xss_admin(preg_replace('/\s\s+/', ' ', str_replace("\n", '', $output))); $this->view->row_index++; } } diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php new file mode 100644 index 0000000..eda7f7d --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Tests/EntityReferenceItemTest.php @@ -0,0 +1,103 @@ + 'Entity reference field API', + 'description' => 'Tests using entity fields of the entity-reference field type.', + 'group' => 'Entity Reference', + ); + } + + public function setUp() { + parent::setUp(); + + $field = array( + 'translatable' => FALSE, + 'entity_types' => array(), + 'settings' => array( + 'target_type' => 'node', + ), + 'field_name' => 'field_test', + 'type' => 'entity_reference', + 'cardinality' => FIELD_CARDINALITY_UNLIMITED, + ); + + field_create_field($field); + + $instance = array( + 'entity_type' => 'entity_test', + 'field_name' => 'field_test', + 'bundle' => 'entity_test', + 'widget' => array( + 'type' => 'options_select', + ), + 'settings' => array( + 'handler' => 'base', + 'handler_settings' => array(), + ), + ); + field_create_instance($instance); + } + + /** + * Tests using entity fields of the taxonomy term reference field type. + */ + public function testEntityReferenceItem() { + // Create a node. + $node1 = $this->drupalCreateNode(); + $nid = $node1->id(); + + // Just being able to create the entity like this verifies a lot of + // code. + $entity = entity_create('entity_test', array('name' => 'foo')); + $entity->field_test->target_id = $nid; + $entity->save(); + + $this->assertTrue($entity->field_test instanceof FieldInterface, 'Field implements interface.'); + $this->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEqual($entity->field_test->target_id, $nid); + $this->assertEqual($entity->field_test->entity->title, $node1->label()); + $this->assertEqual($entity->field_test->entity->id(), $nid); + $this->assertEqual($entity->field_test->entity->uuid(), $node1->uuid()); + + // Change the name of the term via the reference. + $new_name = $this->randomName(); + $entity->field_test->entity->title = $new_name; + $entity->field_test->entity->save(); + + // Verify it is the correct name. + $node = node_load($nid); + $this->assertEqual($node->label(), $new_name); + + // Make sure the computed node reflects updates to the node id. + $node2 = $this->drupalCreateNode(); + + $entity->field_test->target_id = $node2->nid; + $this->assertEqual($entity->field_test->entity->id(), $node2->id()); + $this->assertEqual($entity->field_test->entity->title, $node2->label()); + } +} diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/Type/EntityReferenceItem.php b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/EntityReferenceItem.php new file mode 100644 index 0000000..44d8b0c --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/Type/EntityReferenceItem.php @@ -0,0 +1,81 @@ +definition['settings']['target_type']; + + if (!isset(self::$propertyDefinitions[$target_type])) { + self::$propertyDefinitions[$target_type]['value'] = array( + // @todo: Lookup the entity type's ID data type and use it here. + 'type' => 'integer', + 'label' => t('Entity ID'), + ); + self::$propertyDefinitions[$target_type]['entity'] = array( + 'type' => 'entity', + 'constraints' => array( + 'entity type' => $target_type, + ), + 'label' => t('Entity'), + 'description' => t('The referenced entity'), + // The entity object is computed out of the entity ID. + 'computed' => TRUE, + 'read-only' => FALSE, + 'settings' => array('id source' => 'value'), + ); + } + return self::$propertyDefinitions[$target_type]; + } + + /** + * Overrides Drupal\Core\Entity\Field\FieldItemBase::setValue(). + */ + public function setValue($values) { + // Treat the values as property value of the entity field, if no array + // is given. + if (!is_array($values)) { + $values = array('entity' => $values); + } + + // Entity is computed out of the ID, so we only need to update the ID. Only + // set the entity field if no ID is given. + if (isset($values['value'])) { + $this->properties['value']->setValue($values['value']); + } + else { + $this->properties['entity']->setValue(isset($values['entity']) ? $values['entity'] : NULL); + } + unset($values['entity'], $values['value']); + if ($values) { + throw new \InvalidArgumentException('Property ' . key($values) . ' is unknown.'); + } + } +} diff --git a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php index 66767cf..b71deb0 100644 --- a/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php +++ b/core/modules/jsonld/lib/Drupal/jsonld/JsonldEntityReferenceNormalizer.php @@ -25,7 +25,7 @@ class JsonldEntityReferenceNormalizer extends JsonldNormalizerBase implements De * * @var string */ - protected static $supportedInterfaceOrClass = 'Drupal\Core\Entity\Field\Type\EntityReferenceItem'; + protected static $supportedInterfaceOrClass = 'Drupal\entity_reference\Type\EntityReferenceItem'; /** * Implements \Symfony\Component\Serializer\Normalizer\NormalizerInterface::normalize() diff --git a/core/modules/system/system.module b/core/modules/system/system.module index eaaebc2..de91fb3 100644 --- a/core/modules/system/system.module +++ b/core/modules/system/system.module @@ -2110,12 +2110,6 @@ function system_data_type_info() { 'class' => '\Drupal\Core\Entity\Field\Type\LanguageItem', 'list class' => '\Drupal\Core\Entity\Field\Type\Field', ), - 'entityreference_field' => array( - 'label' => t('Entity reference field item'), - 'description' => t('An entity field containing an entity reference.'), - 'class' => '\Drupal\Core\Entity\Field\Type\EntityReferenceItem', - 'list class' => '\Drupal\Core\Entity\Field\Type\Field', - ), ); } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.info b/core/modules/system/tests/modules/entity_test/entity_test.info index ce49e8a..c21bbff 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.info +++ b/core/modules/system/tests/modules/entity_test/entity_test.info @@ -4,4 +4,5 @@ package = Testing version = VERSION core = 8.x dependencies[] = field +dependencies[] = entity_reference hidden = TRUE diff --git a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php index e3b45fa..501ca78 100644 --- a/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php +++ b/core/modules/system/tests/modules/entity_test/lib/Drupal/entity_test/EntityTestStorageController.php @@ -168,8 +168,8 @@ public function baseFieldDefinitions() { $fields['user_id'] = array( 'label' => t('User ID'), 'description' => t('The ID of the associated user.'), - 'type' => 'entityreference_field', - 'settings' => array('entity type' => 'user'), + 'type' => 'entity_reference_field', + 'settings' => array('target_type' => 'user'), 'translatable' => TRUE, ); return $fields; diff --git a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php index ea6bd2e..e073e39 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php +++ b/core/modules/views/lib/Drupal/views/Plugin/entity_reference/selection/ViewsSelection.php @@ -157,7 +157,7 @@ public function getReferencableEntities($match = NULL, $match_operator = 'CONTAI if ($result) { foreach($this->view->result as $row) { $entity = $row->_entity; - $return[$entity->bundle()][$entity->id()] = $result[$entity->id()]; + $return[$entity->bundle()][$entity->id()] = $entity->label(); } } return $return;