Commit 9e354fa9 authored by Dries's avatar Dries

Issue #1965208 by amateescu, Berdir: Convert TaxonomyTermReferenceItem to...

Issue #1965208 by amateescu, Berdir: Convert TaxonomyTermReferenceItem to extend EntityReferenceItem.
parent 31fe58d1
......@@ -277,11 +277,11 @@ function forum_node_validate(EntityInterface $node, $form) {
foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
// If no term was selected (e.g. when no terms exist yet), remove the
// item.
if (empty($item['tid'])) {
if (empty($item['target_id'])) {
unset($node->taxonomy_forums[$langcode][$delta]);
continue;
}
$term = taxonomy_term_load($item['tid']);
$term = taxonomy_term_load($item['target_id']);
if (!$term) {
form_set_error('taxonomy_forums', t('Select a forum.'));
continue;
......@@ -310,13 +310,13 @@ function forum_node_presave(EntityInterface $node) {
reset($node->taxonomy_forums);
$langcode = key($node->taxonomy_forums);
if (!empty($node->taxonomy_forums[$langcode])) {
$node->forum_tid = $node->taxonomy_forums[$langcode][0]['tid'];
$node->forum_tid = $node->taxonomy_forums[$langcode][0]['target_id'];
// Only do a shadow copy check if this is not a new node.
if (!$node->isNew()) {
$old_tid = db_query_range("SELECT f.tid FROM {forum} f INNER JOIN {node} n ON f.vid = n.vid WHERE n.nid = :nid ORDER BY f.vid DESC", 0, 1, array(':nid' => $node->nid))->fetchField();
if ($old_tid && isset($node->forum_tid) && ($node->forum_tid != $old_tid) && !empty($node->shadow)) {
// A shadow copy needs to be created. Retain new term and add old term.
$node->taxonomy_forums[$langcode][] = array('tid' => $old_tid);
$node->taxonomy_forums[$langcode][] = array('target_id' => $old_tid);
}
}
}
......@@ -515,7 +515,7 @@ function forum_field_storage_pre_insert(EntityInterface $entity, &$skip_fields)
$query->values(array(
'nid' => $entity->id(),
'title' => $translation->title->value,
'tid' => $translation->taxonomy_forums->tid,
'tid' => $translation->taxonomy_forums->target_id,
'sticky' => $entity->sticky,
'created' => $entity->created,
'comment_count' => 0,
......@@ -550,7 +550,7 @@ function forum_field_storage_pre_update(EntityInterface $entity, &$skip_fields)
$query->values(array(
'nid' => $entity->nid,
'title' => $entity->title,
'tid' => $item['tid'],
'tid' => $item['target_id'],
'sticky' => $entity->sticky,
'created' => $entity->created,
'comment_count' => 0,
......
......@@ -529,7 +529,7 @@ function createForumTopic($forum, $container = FALSE) {
// Retrieve node object, ensure that the topic was created and in the proper forum.
$node = $this->drupalGetNodeByTitle($title);
$this->assertTrue($node != NULL, format_string('Node @title was loaded', array('@title' => $title)));
$this->assertEqual($node->taxonomy_forums[Language::LANGCODE_NOT_SPECIFIED][0]['tid'], $tid, 'Saved forum topic was in the expected forum');
$this->assertEqual($node->taxonomy_forums[Language::LANGCODE_NOT_SPECIFIED][0]['target_id'], $tid, 'Saved forum topic was in the expected forum');
// View forum topic.
$this->drupalGet('node/' . $node->nid);
......
......@@ -87,7 +87,7 @@ public function testForumPager() {
'nid' => NULL,
'type' => 'forum',
'taxonomy_forums' => array(
array('tid' => $tid)
array('target_id' => $tid),
),
));
}
......
......@@ -110,7 +110,7 @@ public function setUp() {
$entity->name->value = $this->randomName();
$index = $i ? 1 : 0;
$entity->user_id->target_id = $this->accounts[$index]->uid;
$entity->{$this->fieldName}->tid = $this->terms[$index]->id();
$entity->{$this->fieldName}->target_id = $this->terms[$index]->id();
$entity->save();
$this->entities[] = $entity;
}
......@@ -152,7 +152,7 @@ public function testQuery() {
// This returns the 0th entity as that's only one pointing to the 0th
// term (test with specifying the column name).
$this->queryResults = $this->factory->get('entity_test')
->condition("$this->fieldName.tid.entity.name", $this->terms[0]->name->value)
->condition("$this->fieldName.target_id.entity.name", $this->terms[0]->name->value)
->execute();
$this->assertResults(array(0));
// This returns the 1st and 2nd entity as those point to the 1st term.
......
......@@ -102,7 +102,7 @@ function setUp() {
'title' => $this->xss_label,
'type' => 'article',
'promote' => NODE_PROMOTED,
'field_tags' => array(array('tid' => $this->term->id())),
'field_tags' => array(array('target_id' => $this->term->id())),
));
// Create a test comment on the test node.
......
......@@ -33,9 +33,10 @@ class LinkFormatter extends TaxonomyFormatterBase {
public function viewElements(EntityInterface $entity, $langcode, array $items) {
$elements = array();
// Terms without tid do not exist yet, theme such terms as just their name.
// Terms without target_id do not exist yet, theme such terms as just their
// name.
foreach ($items as $delta => $item) {
if (!$item['tid']) {
if (!$item['target_id']) {
$elements[$delta] = array(
'#markup' => check_plain($item['entity']->label()),
);
......
......@@ -31,10 +31,10 @@ class RSSCategoryFormatter extends TaxonomyFormatterBase {
* {@inheritdoc}
*/
public function viewElements(EntityInterface $entity, $langcode, array $items) {
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is
// not set. Theme such terms as just their name.
// Terms whose target_id is 'autocreate' do not exist yet and
// $item['entity'] is not set. Theme such terms as just their name.
foreach ($items as $item) {
if ($item['tid']) {
if ($item['target_id']) {
$value = $item['entity']->label();
$uri = $item['entity']->uri();
......
......@@ -30,8 +30,8 @@ public function prepareView(array $entities, $langcode, array &$items) {
foreach ($entities as $id => $entity) {
foreach ($items[$id] as $delta => $item) {
// Force the array key to prevent duplicates.
if ($item['tid'] !== 0) {
$tids[$item['tid']] = $item['tid'];
if ($item['target_id'] !== 0) {
$tids[$item['target_id']] = $item['target_id'];
}
}
}
......@@ -46,12 +46,12 @@ public function prepareView(array $entities, $langcode, array &$items) {
foreach ($items[$id] as $delta => $item) {
// Check whether the taxonomy term field instance value could be
// loaded.
if (isset($terms[$item['tid']])) {
if (isset($terms[$item['target_id']])) {
// Replace the instance value with the term data.
$items[$id][$delta]['entity'] = $terms[$item['tid']];
$items[$id][$delta]['entity'] = $terms[$item['target_id']];
}
// Terms to be created are not in $terms, but are still legitimate.
elseif ($item['tid'] === 0 && isset($item['entity'])) {
elseif ($item['target_id'] === 0 && isset($item['entity'])) {
// Leave the item in place.
}
// Otherwise, unset the instance value, since the term does not exist.
......
......@@ -50,7 +50,7 @@ public function settingsForm(array $form, array &$form_state) {
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
$tags = array();
foreach ($items as $item) {
$tags[$item['tid']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['tid']);
$tags[$item['target_id']] = isset($item['taxonomy_term']) ? $item['taxonomy_term'] : taxonomy_term_load($item['target_id']);
}
$element += array(
'#type' => 'textfield',
......@@ -86,7 +86,7 @@ public function massageFormValues(array $values, array $form, array &$form_state
// otherwise, create a new term.
if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) {
$term = array_pop($possibilities);
$item = array('tid' => $term->id());
$item = array('target_id' => $term->id());
}
else {
$vocabulary = reset($vocabularies);
......@@ -94,7 +94,7 @@ public function massageFormValues(array $values, array $form, array &$form_state
'vid' => $vocabulary->id(),
'name' => $value,
));
$item = array('tid' => 0, 'entity' => $term);
$item = array('target_id' => 0, 'entity' => $term);
}
$items[] = $item;
}
......
......@@ -77,14 +77,14 @@ public function testTaxonomyTermReferenceItem() {
$tid = $this->term->id();
// Just being able to create the entity like this verifies a lot of code.
$entity = entity_create('entity_test', array());
$entity->field_test_taxonomy->tid = $this->term->id();
$entity->field_test_taxonomy->target_id = $this->term->id();
$entity->name->value = $this->randomName();
$entity->save();
$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->id());
$this->assertEqual($entity->field_test_taxonomy->target_id, $this->term->id());
$this->assertEqual($entity->field_test_taxonomy->entity->name->value, $this->term->name->value);
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid);
$this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid());
......@@ -105,7 +105,7 @@ public function testTaxonomyTermReferenceItem() {
));
$term2->save();
$entity->field_test_taxonomy->tid = $term2->id();
$entity->field_test_taxonomy->target_id = $term2->id();
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $term2->id());
$this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value);
}
......
......@@ -84,7 +84,7 @@ function testTaxonomyTermFieldValidation() {
$langcode = Language::LANGCODE_NOT_SPECIFIED;
$entity = entity_create('entity_test', array());
$term = $this->createTerm($this->vocabulary);
$entity->{$this->field_name}->tid = $term->id();
$entity->{$this->field_name}->target_id = $term->id();
try {
field_attach_validate($entity);
$this->pass('Correct term does not cause validation error.');
......@@ -95,7 +95,7 @@ function testTaxonomyTermFieldValidation() {
$entity = entity_create('entity_test', array());
$bad_term = $this->createTerm($this->createVocabulary());
$entity->{$this->field_name}->tid = $bad_term->id();
$entity->{$this->field_name}->target_id = $bad_term->id();
try {
field_attach_validate($entity);
$this->fail('Wrong term causes validation error.');
......
......@@ -171,7 +171,7 @@ function testTaxonomyIndex() {
$this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
// Update the article to change one term.
$node->{$this->field_name_1}[$langcode] = array(array('tid' => $term_1->id()));
$node->{$this->field_name_1}[$langcode] = array(array('target_id' => $term_1->id()));
$node->save();
// Check that both terms are indexed.
......@@ -187,7 +187,7 @@ function testTaxonomyIndex() {
$this->assertEqual(1, $index_count, 'Term 2 is indexed.');
// Update the article to change another term.
$node->{$this->field_name_2}[$langcode] = array(array('tid' => $term_1->id()));
$node->{$this->field_name_2}[$langcode] = array(array('target_id' => $term_1->id()));
$node->save();
// Check that only one term is indexed.
......
......@@ -55,8 +55,8 @@ function setUp() {
$node = array();
$node['type'] = 'article';
$node['field_views_testing_tags'][]['tid'] = $this->term1->id();
$node['field_views_testing_tags'][]['tid'] = $this->term2->id();
$node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
$node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
$this->nodes[] = $this->drupalCreateNode($node);
$this->nodes[] = $this->drupalCreateNode($node);
}
......
......@@ -7,13 +7,12 @@
namespace Drupal\taxonomy\Type;
use Drupal\Core\Entity\Field\FieldItemBase;
use Drupal\Core\TypedData\TypedDataInterface;
use Drupal\Core\Entity\Field\Type\EntityReferenceItem;
/**
* Defines the 'taxonomy_term_reference' entity field item.
*/
class TaxonomyTermReferenceItem extends FieldItemBase {
class TaxonomyTermReferenceItem extends EntityReferenceItem {
/**
* Property definitions of the contained properties.
......@@ -25,47 +24,11 @@ class TaxonomyTermReferenceItem extends FieldItemBase {
static $propertyDefinitions;
/**
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
* {@inheritdoc}
*/
public function getPropertyDefinitions() {
if (!isset(static::$propertyDefinitions)) {
static::$propertyDefinitions['tid'] = array(
'type' => 'integer',
'label' => t('Referenced taxonomy term id.'),
);
static::$propertyDefinitions['entity'] = array(
'type' => 'entity',
'constraints' => array(
'EntityType' => 'taxonomy_term',
),
'label' => t('Term'),
'description' => t('The referenced taxonomy term'),
// The entity object is computed out of the tid.
'computed' => TRUE,
'read-only' => FALSE,
'settings' => array('id source' => 'tid'),
);
}
return static::$propertyDefinitions;
$this->definition['settings']['target_type'] = 'taxonomy_term';
return parent::getPropertyDefinitions();
}
/**
* Overrides \Drupal\Core\Entity\Field\FieldItemBase::get().
*/
public function setValue($values, $notify = TRUE) {
// Treat the values as value of the entity property, if no array is
// given as this handles entity IDs and objects.
if (isset($values) && !is_array($values)) {
// Directly update the property instead of invoking the parent, so that
// the entity property can take care of updating the ID property.
$this->properties['entity']->setValue($values, $notify);
}
else {
// Make sure that the 'entity' property gets set as 'target_id'.
if (isset($values['tid']) && !isset($values['entity'])) {
$values['entity'] = $values['tid'];
}
parent::setValue($values, $notify);
}
}
}
......@@ -6,6 +6,7 @@
*/
use Drupal\Component\Uuid\Uuid;
use Drupal\field\Plugin\Core\Entity\Field;
/**
* Implements hook_uninstall().
......@@ -175,24 +176,36 @@ function taxonomy_schema() {
function taxonomy_field_schema($field) {
return array(
'columns' => array(
'tid' => array(
'target_id' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
),
),
'indexes' => array(
'tid' => array('tid'),
'target_id' => array('target_id'),
),
'foreign keys' => array(
'tid' => array(
'target_id' => array(
'table' => 'taxonomy_term_data',
'columns' => array('tid' => 'tid'),
'columns' => array('target_id' => 'tid'),
),
),
);
}
/**
* Implements hook_update_dependencies().
*/
function taxonomy_update_dependencies() {
// Convert the 'tid' column of the taxonomy reference field to 'target_id'
// after fields and instances have been moved to the config system.
$dependencies['taxonomy'][8007] = array(
'field' => 8003,
);
return $dependencies;
}
/**
* Remove the {taxonomy_vocabulary}.module field.
*/
......@@ -335,3 +348,41 @@ function taxonomy_update_8006() {
->execute();
}
}
/**
* Update taxonomy_term_reference field tables to use target_id instead of tid.
*/
function taxonomy_update_8007() {
foreach (config_get_storage_names_with_prefix('field.field.') as $config_name) {
$field_config = config($config_name);
// Only update taxonomy reference fields that use the default SQL storage.
if ($field_config->get('type') == 'taxonomy_term_reference' && $field_config->get('storage.type') == 'field_sql_storage') {
$field = new Field($field_config->get());
$tables = array(
_field_sql_storage_tablename($field),
_field_sql_storage_revision_tablename($field),
);
foreach ($tables as $table_name) {
db_change_field($table_name, $field->id() . '_tid', $field->id() . '_target_id', array(
'description' => 'The ID of the target entity.',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
));
// Change the index.
db_drop_index($table_name, $field->id() . '_tid');
db_add_index($table_name, $field->id() . '_target_id', array($field->id() . '_target_id'));
}
// Update the indexes in field config as well.
$indexes = $field_config->get('indexes');
unset($indexes['tid']);
$indexes['target_id'] = array('target_id');
$field_config->set('indexes', $indexes);
$field_config->save();
}
}
}
......@@ -948,8 +948,8 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
// Build an array of existing term IDs so they can be loaded with
// taxonomy_term_load_multiple();
foreach ($items as $delta => $item) {
if (!empty($item['tid']) && $item['tid'] != 'autocreate') {
$tids[] = $item['tid'];
if (!empty($item['target_id']) && $item['target_id'] != 'autocreate') {
$tids[] = $item['target_id'];
}
}
if (!empty($tids)) {
......@@ -959,12 +959,12 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
// allowed values for this field.
foreach ($items as $delta => $item) {
$validate = TRUE;
if (!empty($item['tid']) && $item['tid'] != 'autocreate') {
if (!empty($item['target_id']) && $item['target_id'] != 'autocreate') {
$validate = FALSE;
foreach ($field['settings']['allowed_values'] as $settings) {
// If no parent is specified, check if the term is in the vocabulary.
if (isset($settings['vocabulary']) && empty($settings['parent'])) {
if ($settings['vocabulary'] == $terms[$item['tid']]->bundle()) {
if ($settings['vocabulary'] == $terms[$item['target_id']]->bundle()) {
$validate = TRUE;
break;
}
......@@ -972,7 +972,7 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
// If a parent is specified, then to validate it must appear in the
// array returned by taxonomy_term_load_parents_all().
elseif (!empty($settings['parent'])) {
$ancestors = taxonomy_term_load_parents_all($item['tid']);
$ancestors = taxonomy_term_load_parents_all($item['target_id']);
foreach ($ancestors as $ancestor) {
if ($ancestor->id() == $settings['parent']) {
$validate = TRUE;
......@@ -996,7 +996,7 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
* Implements hook_field_is_empty().
*/
function taxonomy_field_is_empty($item, $field_type) {
return !is_array($item) || (empty($item['tid']) && empty($item['entity']));
return !is_array($item) || (empty($item['target_id']) && empty($item['entity']));
}
/**
......@@ -1153,10 +1153,9 @@ function taxonomy_rdf_mapping() {
*/
function taxonomy_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
foreach ($items as $delta => $item) {
if (!$item['tid'] && isset($item['entity'])) {
unset($item['tid']);
if (!$item['target_id'] && isset($item['target_id'])) {
$item['entity']->save();
$items[$delta]['tid'] = $item['entity']->id();
$items[$delta]['target_id'] = $item['entity']->id();
}
}
}
......@@ -1216,7 +1215,7 @@ function taxonomy_build_node_index($node) {
foreach (field_available_languages('node', $field) as $langcode) {
if (!empty($items[$langcode])) {
foreach ($items[$langcode] as $item) {
$tid_all[$item['tid']] = $item['tid'];
$tid_all[$item['target_id']] = $item['target_id'];
}
}
}
......
......@@ -346,7 +346,7 @@ function taxonomy_field_views_data($field) {
}
// Add the relationship only on the tid field.
$data[$table_name][$field['field_name'] . '_tid']['relationship'] = array(
$data[$table_name][$field['field_name'] . '_target_id']['relationship'] = array(
'id' => 'standard',
'base' => 'taxonomy_term_data',
'base field' => 'tid',
......@@ -380,7 +380,7 @@ function taxonomy_field_views_data_views_data_alter(&$data, $field) {
'id' => 'entity_reverse',
'field_name' => $field['field_name'],
'field table' => _field_sql_storage_tablename($field),
'field field' => $field['field_name'] . '_tid',
'field field' => $field['field_name'] . '_target_id',
'base' => $entity_info['base_table'],
'base field' => $entity_info['entity_keys']['id'],
'label' => t('!field_name', array('!field_name' => $field['field_name'])),
......
......@@ -87,7 +87,7 @@ protected function setUp() {
$term = $this->createTerm($this->vocabulary);
$values = array('created' => $time, 'type' => 'page');
$values[$this->field_name][]['tid'] = $term->id();
$values[$this->field_name][]['target_id'] = $term->id();
// Make every other node promoted.
if ($i % 2) {
......
......@@ -17,7 +17,7 @@ cardinality: '-1'
translatable: '0'
entity_types: { }
indexes:
tid:
- tid
target_id:
- target_id
status: 1
langcode: und
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment