diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php index 9560ed3..d2744ea 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Tests/TaxonomyTermReferenceItemTest.php @@ -16,12 +16,18 @@ * Tests the new entity API for the taxonomy term reference field type. */ class TaxonomyTermReferenceItemTest extends WebTestBase { + + /** + * Modules to enable. + * + * @var array + */ public static $modules = array('field', 'field_sql_storage', 'taxonomy', 'entity_test', 'options'); public static function getInfo() { return array( 'name' => 'Taxonomy reference API', - 'description' => 'Tests the new entity API for the taxonomy term reference field type.', + 'description' => 'Tests using entity fields of the taxonomy term reference field type.', 'group' => 'Taxonomy', ); } @@ -33,7 +39,7 @@ public function setUp() { 'machine_name' => drupal_strtolower($this->randomName()), 'langcode' => LANGUAGE_NOT_SPECIFIED, )); - taxonomy_vocabulary_save($vocabulary); + $vocabulary->save(); $field = array( 'field_name' => 'field_test_taxonomy', 'type' => 'taxonomy_term_reference', @@ -62,9 +68,12 @@ public function setUp() { 'vid' => $vocabulary->vid, 'langcode' => LANGUAGE_NOT_SPECIFIED, )); - taxonomy_term_save($this->term); + $this->term->save(); } + /** + * Tests using entity fields of the taxonomy term reference field type. + */ public function testTaxonomyTermReferenceItem() { $tid = $this->term->id(); // Just being able to create the entity like this verifies a lot of code. @@ -72,21 +81,33 @@ public function testTaxonomyTermReferenceItem() { $entity->field_test_taxonomy->tid = $this->term->tid; $entity->name->value = $this->randomName(); $entity->save(); - $id = $entity->id(); - $entities = entity_load_multiple('entity_test', array($id), TRUE); - $entity = reset($entities); + + $entity = entity_load('entity_test', $entity->id()); $this->assertTrue($entity->field_test_taxonomy instanceof FieldInterface, 'Field implements interface.'); $this->assertTrue($entity->field_test_taxonomy[0] instanceof FieldItemInterface, 'Field item implements interface.'); + $this->assertEqual($entity->field_test_taxonomy->tid, $this->term->tid); $this->assertEqual($entity->field_test_taxonomy->entity->name, $this->term->name); $this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid); $this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid()); + // Change the name of the term via the reference. $new_name = $this->randomName(); $entity->field_test_taxonomy->entity->name = $new_name; $entity->field_test_taxonomy->entity->save(); // Verify it is the correct name. - $terms = entity_load_multiple('taxonomy_term', array($tid), TRUE); - $term = reset($terms); + $term = entity_load('taxonomy_term', $tid); $this->assertEqual($term->name, $new_name); + + // Make sure the computed term reflects updates to the term id. + $term2 = entity_create('taxonomy_term', array( + 'name' => $this->randomName(), + 'vid' => $this->term->vid, + 'langcode' => LANGUAGE_NOT_SPECIFIED, + )); + $term2->save(); + + $entity->field_test_taxonomy->tid = $term2->tid; + $this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->tid); + $this->assertEqual($entity->field_test_taxonomy->entity->name, $term2->name); } } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php index 108d06c..53106ee 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Type/TaxonomyTermReferenceItem.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\taxonomy\Type\taxonomyItem. + * Definition of Drupal\taxonomy\Type\TaxonomyTermReferenceItem. */ namespace Drupal\taxonomy\Type; @@ -16,7 +16,7 @@ class TaxonomyTermReferenceItem extends FieldItemBase { /** - * Field definitions of the contained properties. + * Property definitions of the contained properties. * * @see self::getPropertyDefinitions() * @@ -40,7 +40,7 @@ public function getPropertyDefinitions() { 'entity type' => 'taxonomy_term', ), 'label' => t('Term'), - 'description' => t('The referenced taxonom term'), + 'description' => t('The referenced taxonomy term'), // The entity object is computed out of the tid. 'computed' => TRUE, 'read-only' => FALSE, @@ -65,8 +65,11 @@ public function setValue($values) { if (isset($values['tid'])) { $this->properties['tid']->setValue($values['tid']); } + elseif (isset($values['entity'])) { + $this->properties['entity']->setValue($values['entity']); + } else { - $this->properties['entity']->setValue(isset($values['entity']) ? $values['entity'] : NULL); + $this->properties['entity']->setValue(NULL); } unset($values['entity'], $values['tid']); if ($values) {