Issue #1965208 by amateescu, Berdir: Convert TaxonomyTermReferenceItem to extend EntityReferenceItem.
parent
31fe58d108
commit
9e354fa9d2
|
@ -277,11 +277,11 @@ function forum_node_validate(EntityInterface $node, $form) {
|
||||||
foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
|
foreach ($node->taxonomy_forums[$langcode] as $delta => $item) {
|
||||||
// If no term was selected (e.g. when no terms exist yet), remove the
|
// If no term was selected (e.g. when no terms exist yet), remove the
|
||||||
// item.
|
// item.
|
||||||
if (empty($item['tid'])) {
|
if (empty($item['target_id'])) {
|
||||||
unset($node->taxonomy_forums[$langcode][$delta]);
|
unset($node->taxonomy_forums[$langcode][$delta]);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
$term = taxonomy_term_load($item['tid']);
|
$term = taxonomy_term_load($item['target_id']);
|
||||||
if (!$term) {
|
if (!$term) {
|
||||||
form_set_error('taxonomy_forums', t('Select a forum.'));
|
form_set_error('taxonomy_forums', t('Select a forum.'));
|
||||||
continue;
|
continue;
|
||||||
|
@ -310,13 +310,13 @@ function forum_node_presave(EntityInterface $node) {
|
||||||
reset($node->taxonomy_forums);
|
reset($node->taxonomy_forums);
|
||||||
$langcode = key($node->taxonomy_forums);
|
$langcode = key($node->taxonomy_forums);
|
||||||
if (!empty($node->taxonomy_forums[$langcode])) {
|
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.
|
// Only do a shadow copy check if this is not a new node.
|
||||||
if (!$node->isNew()) {
|
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();
|
$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)) {
|
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.
|
// 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(
|
$query->values(array(
|
||||||
'nid' => $entity->id(),
|
'nid' => $entity->id(),
|
||||||
'title' => $translation->title->value,
|
'title' => $translation->title->value,
|
||||||
'tid' => $translation->taxonomy_forums->tid,
|
'tid' => $translation->taxonomy_forums->target_id,
|
||||||
'sticky' => $entity->sticky,
|
'sticky' => $entity->sticky,
|
||||||
'created' => $entity->created,
|
'created' => $entity->created,
|
||||||
'comment_count' => 0,
|
'comment_count' => 0,
|
||||||
|
@ -550,7 +550,7 @@ function forum_field_storage_pre_update(EntityInterface $entity, &$skip_fields)
|
||||||
$query->values(array(
|
$query->values(array(
|
||||||
'nid' => $entity->nid,
|
'nid' => $entity->nid,
|
||||||
'title' => $entity->title,
|
'title' => $entity->title,
|
||||||
'tid' => $item['tid'],
|
'tid' => $item['target_id'],
|
||||||
'sticky' => $entity->sticky,
|
'sticky' => $entity->sticky,
|
||||||
'created' => $entity->created,
|
'created' => $entity->created,
|
||||||
'comment_count' => 0,
|
'comment_count' => 0,
|
||||||
|
|
|
@ -529,7 +529,7 @@ class ForumTest extends WebTestBase {
|
||||||
// Retrieve node object, ensure that the topic was created and in the proper forum.
|
// Retrieve node object, ensure that the topic was created and in the proper forum.
|
||||||
$node = $this->drupalGetNodeByTitle($title);
|
$node = $this->drupalGetNodeByTitle($title);
|
||||||
$this->assertTrue($node != NULL, format_string('Node @title was loaded', array('@title' => $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.
|
// View forum topic.
|
||||||
$this->drupalGet('node/' . $node->nid);
|
$this->drupalGet('node/' . $node->nid);
|
||||||
|
|
|
@ -87,7 +87,7 @@ class NodeAccessPagerTest extends WebTestBase {
|
||||||
'nid' => NULL,
|
'nid' => NULL,
|
||||||
'type' => 'forum',
|
'type' => 'forum',
|
||||||
'taxonomy_forums' => array(
|
'taxonomy_forums' => array(
|
||||||
array('tid' => $tid)
|
array('target_id' => $tid),
|
||||||
),
|
),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
|
@ -110,7 +110,7 @@ class EntityQueryRelationshipTest extends EntityUnitTestBase {
|
||||||
$entity->name->value = $this->randomName();
|
$entity->name->value = $this->randomName();
|
||||||
$index = $i ? 1 : 0;
|
$index = $i ? 1 : 0;
|
||||||
$entity->user_id->target_id = $this->accounts[$index]->uid;
|
$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();
|
$entity->save();
|
||||||
$this->entities[] = $entity;
|
$this->entities[] = $entity;
|
||||||
}
|
}
|
||||||
|
@ -152,7 +152,7 @@ class EntityQueryRelationshipTest extends EntityUnitTestBase {
|
||||||
// This returns the 0th entity as that's only one pointing to the 0th
|
// This returns the 0th entity as that's only one pointing to the 0th
|
||||||
// term (test with specifying the column name).
|
// term (test with specifying the column name).
|
||||||
$this->queryResults = $this->factory->get('entity_test')
|
$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();
|
->execute();
|
||||||
$this->assertResults(array(0));
|
$this->assertResults(array(0));
|
||||||
// This returns the 1st and 2nd entity as those point to the 1st term.
|
// This returns the 1st and 2nd entity as those point to the 1st term.
|
||||||
|
|
|
@ -102,7 +102,7 @@ class EntityFilteringThemeTest extends WebTestBase {
|
||||||
'title' => $this->xss_label,
|
'title' => $this->xss_label,
|
||||||
'type' => 'article',
|
'type' => 'article',
|
||||||
'promote' => NODE_PROMOTED,
|
'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.
|
// Create a test comment on the test node.
|
||||||
|
|
|
@ -33,9 +33,10 @@ class LinkFormatter extends TaxonomyFormatterBase {
|
||||||
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
||||||
$elements = array();
|
$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) {
|
foreach ($items as $delta => $item) {
|
||||||
if (!$item['tid']) {
|
if (!$item['target_id']) {
|
||||||
$elements[$delta] = array(
|
$elements[$delta] = array(
|
||||||
'#markup' => check_plain($item['entity']->label()),
|
'#markup' => check_plain($item['entity']->label()),
|
||||||
);
|
);
|
||||||
|
|
|
@ -31,10 +31,10 @@ class RSSCategoryFormatter extends TaxonomyFormatterBase {
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
public function viewElements(EntityInterface $entity, $langcode, array $items) {
|
||||||
// Terms whose tid is 'autocreate' do not exist yet and $item['entity'] is
|
// Terms whose target_id is 'autocreate' do not exist yet and
|
||||||
// not set. Theme such terms as just their name.
|
// $item['entity'] is not set. Theme such terms as just their name.
|
||||||
foreach ($items as $item) {
|
foreach ($items as $item) {
|
||||||
if ($item['tid']) {
|
if ($item['target_id']) {
|
||||||
$value = $item['entity']->label();
|
$value = $item['entity']->label();
|
||||||
|
|
||||||
$uri = $item['entity']->uri();
|
$uri = $item['entity']->uri();
|
||||||
|
|
|
@ -30,8 +30,8 @@ abstract class TaxonomyFormatterBase extends FormatterBase {
|
||||||
foreach ($entities as $id => $entity) {
|
foreach ($entities as $id => $entity) {
|
||||||
foreach ($items[$id] as $delta => $item) {
|
foreach ($items[$id] as $delta => $item) {
|
||||||
// Force the array key to prevent duplicates.
|
// Force the array key to prevent duplicates.
|
||||||
if ($item['tid'] !== 0) {
|
if ($item['target_id'] !== 0) {
|
||||||
$tids[$item['tid']] = $item['tid'];
|
$tids[$item['target_id']] = $item['target_id'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -46,12 +46,12 @@ abstract class TaxonomyFormatterBase extends FormatterBase {
|
||||||
foreach ($items[$id] as $delta => $item) {
|
foreach ($items[$id] as $delta => $item) {
|
||||||
// Check whether the taxonomy term field instance value could be
|
// Check whether the taxonomy term field instance value could be
|
||||||
// loaded.
|
// loaded.
|
||||||
if (isset($terms[$item['tid']])) {
|
if (isset($terms[$item['target_id']])) {
|
||||||
// Replace the instance value with the term data.
|
// 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.
|
// 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.
|
// Leave the item in place.
|
||||||
}
|
}
|
||||||
// Otherwise, unset the instance value, since the term does not exist.
|
// Otherwise, unset the instance value, since the term does not exist.
|
||||||
|
|
|
@ -50,7 +50,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase {
|
||||||
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
|
public function formElement(array $items, $delta, array $element, $langcode, array &$form, array &$form_state) {
|
||||||
$tags = array();
|
$tags = array();
|
||||||
foreach ($items as $item) {
|
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(
|
$element += array(
|
||||||
'#type' => 'textfield',
|
'#type' => 'textfield',
|
||||||
|
@ -86,7 +86,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase {
|
||||||
// otherwise, create a new term.
|
// otherwise, create a new term.
|
||||||
if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) {
|
if ($possibilities = entity_load_multiple_by_properties('taxonomy_term', array('name' => trim($value), 'vid' => array_keys($vocabularies)))) {
|
||||||
$term = array_pop($possibilities);
|
$term = array_pop($possibilities);
|
||||||
$item = array('tid' => $term->id());
|
$item = array('target_id' => $term->id());
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$vocabulary = reset($vocabularies);
|
$vocabulary = reset($vocabularies);
|
||||||
|
@ -94,7 +94,7 @@ class TaxonomyAutocompleteWidget extends WidgetBase {
|
||||||
'vid' => $vocabulary->id(),
|
'vid' => $vocabulary->id(),
|
||||||
'name' => $value,
|
'name' => $value,
|
||||||
));
|
));
|
||||||
$item = array('tid' => 0, 'entity' => $term);
|
$item = array('target_id' => 0, 'entity' => $term);
|
||||||
}
|
}
|
||||||
$items[] = $item;
|
$items[] = $item;
|
||||||
}
|
}
|
||||||
|
|
|
@ -77,14 +77,14 @@ class TaxonomyTermReferenceItemTest extends FieldUnitTestBase {
|
||||||
$tid = $this->term->id();
|
$tid = $this->term->id();
|
||||||
// Just being able to create the entity like this verifies a lot of code.
|
// Just being able to create the entity like this verifies a lot of code.
|
||||||
$entity = entity_create('entity_test', array());
|
$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->name->value = $this->randomName();
|
||||||
$entity->save();
|
$entity->save();
|
||||||
|
|
||||||
$entity = entity_load('entity_test', $entity->id());
|
$entity = entity_load('entity_test', $entity->id());
|
||||||
$this->assertTrue($entity->field_test_taxonomy instanceof FieldInterface, 'Field implements interface.');
|
$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->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->name->value, $this->term->name->value);
|
||||||
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid);
|
$this->assertEqual($entity->field_test_taxonomy->entity->id(), $tid);
|
||||||
$this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid());
|
$this->assertEqual($entity->field_test_taxonomy->entity->uuid(), $this->term->uuid());
|
||||||
|
@ -105,7 +105,7 @@ class TaxonomyTermReferenceItemTest extends FieldUnitTestBase {
|
||||||
));
|
));
|
||||||
$term2->save();
|
$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->id(), $term2->id());
|
||||||
$this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value);
|
$this->assertEqual($entity->field_test_taxonomy->entity->name->value, $term2->name->value);
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,7 +84,7 @@ class TermFieldTest extends TaxonomyTestBase {
|
||||||
$langcode = Language::LANGCODE_NOT_SPECIFIED;
|
$langcode = Language::LANGCODE_NOT_SPECIFIED;
|
||||||
$entity = entity_create('entity_test', array());
|
$entity = entity_create('entity_test', array());
|
||||||
$term = $this->createTerm($this->vocabulary);
|
$term = $this->createTerm($this->vocabulary);
|
||||||
$entity->{$this->field_name}->tid = $term->id();
|
$entity->{$this->field_name}->target_id = $term->id();
|
||||||
try {
|
try {
|
||||||
field_attach_validate($entity);
|
field_attach_validate($entity);
|
||||||
$this->pass('Correct term does not cause validation error.');
|
$this->pass('Correct term does not cause validation error.');
|
||||||
|
@ -95,7 +95,7 @@ class TermFieldTest extends TaxonomyTestBase {
|
||||||
|
|
||||||
$entity = entity_create('entity_test', array());
|
$entity = entity_create('entity_test', array());
|
||||||
$bad_term = $this->createTerm($this->createVocabulary());
|
$bad_term = $this->createTerm($this->createVocabulary());
|
||||||
$entity->{$this->field_name}->tid = $bad_term->id();
|
$entity->{$this->field_name}->target_id = $bad_term->id();
|
||||||
try {
|
try {
|
||||||
field_attach_validate($entity);
|
field_attach_validate($entity);
|
||||||
$this->fail('Wrong term causes validation error.');
|
$this->fail('Wrong term causes validation error.');
|
||||||
|
|
|
@ -171,7 +171,7 @@ class TermIndexTest extends TaxonomyTestBase {
|
||||||
$this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
|
$this->assertEqual(1, $index_count, 'Term 2 is indexed once.');
|
||||||
|
|
||||||
// Update the article to change one term.
|
// 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();
|
$node->save();
|
||||||
|
|
||||||
// Check that both terms are indexed.
|
// Check that both terms are indexed.
|
||||||
|
@ -187,7 +187,7 @@ class TermIndexTest extends TaxonomyTestBase {
|
||||||
$this->assertEqual(1, $index_count, 'Term 2 is indexed.');
|
$this->assertEqual(1, $index_count, 'Term 2 is indexed.');
|
||||||
|
|
||||||
// Update the article to change another term.
|
// 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();
|
$node->save();
|
||||||
|
|
||||||
// Check that only one term is indexed.
|
// Check that only one term is indexed.
|
||||||
|
|
|
@ -55,8 +55,8 @@ abstract class TaxonomyTestBase extends ViewTestBase {
|
||||||
|
|
||||||
$node = array();
|
$node = array();
|
||||||
$node['type'] = 'article';
|
$node['type'] = 'article';
|
||||||
$node['field_views_testing_tags'][]['tid'] = $this->term1->id();
|
$node['field_views_testing_tags'][]['target_id'] = $this->term1->id();
|
||||||
$node['field_views_testing_tags'][]['tid'] = $this->term2->id();
|
$node['field_views_testing_tags'][]['target_id'] = $this->term2->id();
|
||||||
$this->nodes[] = $this->drupalCreateNode($node);
|
$this->nodes[] = $this->drupalCreateNode($node);
|
||||||
$this->nodes[] = $this->drupalCreateNode($node);
|
$this->nodes[] = $this->drupalCreateNode($node);
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,13 +7,12 @@
|
||||||
|
|
||||||
namespace Drupal\taxonomy\Type;
|
namespace Drupal\taxonomy\Type;
|
||||||
|
|
||||||
use Drupal\Core\Entity\Field\FieldItemBase;
|
use Drupal\Core\Entity\Field\Type\EntityReferenceItem;
|
||||||
use Drupal\Core\TypedData\TypedDataInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Defines the 'taxonomy_term_reference' entity field item.
|
* Defines the 'taxonomy_term_reference' entity field item.
|
||||||
*/
|
*/
|
||||||
class TaxonomyTermReferenceItem extends FieldItemBase {
|
class TaxonomyTermReferenceItem extends EntityReferenceItem {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Property definitions of the contained properties.
|
* Property definitions of the contained properties.
|
||||||
|
@ -25,47 +24,11 @@ class TaxonomyTermReferenceItem extends FieldItemBase {
|
||||||
static $propertyDefinitions;
|
static $propertyDefinitions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements \Drupal\Core\TypedData\ComplexDataInterface::getPropertyDefinitions().
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function getPropertyDefinitions() {
|
public function getPropertyDefinitions() {
|
||||||
if (!isset(static::$propertyDefinitions)) {
|
$this->definition['settings']['target_type'] = 'taxonomy_term';
|
||||||
static::$propertyDefinitions['tid'] = array(
|
return parent::getPropertyDefinitions();
|
||||||
'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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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\Component\Uuid\Uuid;
|
||||||
|
use Drupal\field\Plugin\Core\Entity\Field;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_uninstall().
|
* Implements hook_uninstall().
|
||||||
|
@ -175,24 +176,36 @@ function taxonomy_schema() {
|
||||||
function taxonomy_field_schema($field) {
|
function taxonomy_field_schema($field) {
|
||||||
return array(
|
return array(
|
||||||
'columns' => array(
|
'columns' => array(
|
||||||
'tid' => array(
|
'target_id' => array(
|
||||||
'type' => 'int',
|
'type' => 'int',
|
||||||
'unsigned' => TRUE,
|
'unsigned' => TRUE,
|
||||||
'not null' => FALSE,
|
'not null' => FALSE,
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
'indexes' => array(
|
'indexes' => array(
|
||||||
'tid' => array('tid'),
|
'target_id' => array('target_id'),
|
||||||
),
|
),
|
||||||
'foreign keys' => array(
|
'foreign keys' => array(
|
||||||
'tid' => array(
|
'target_id' => array(
|
||||||
'table' => 'taxonomy_term_data',
|
'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.
|
* Remove the {taxonomy_vocabulary}.module field.
|
||||||
*/
|
*/
|
||||||
|
@ -335,3 +348,41 @@ function taxonomy_update_8006() {
|
||||||
->execute();
|
->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
|
// Build an array of existing term IDs so they can be loaded with
|
||||||
// taxonomy_term_load_multiple();
|
// taxonomy_term_load_multiple();
|
||||||
foreach ($items as $delta => $item) {
|
foreach ($items as $delta => $item) {
|
||||||
if (!empty($item['tid']) && $item['tid'] != 'autocreate') {
|
if (!empty($item['target_id']) && $item['target_id'] != 'autocreate') {
|
||||||
$tids[] = $item['tid'];
|
$tids[] = $item['target_id'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!empty($tids)) {
|
if (!empty($tids)) {
|
||||||
|
@ -959,12 +959,12 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
|
||||||
// allowed values for this field.
|
// allowed values for this field.
|
||||||
foreach ($items as $delta => $item) {
|
foreach ($items as $delta => $item) {
|
||||||
$validate = TRUE;
|
$validate = TRUE;
|
||||||
if (!empty($item['tid']) && $item['tid'] != 'autocreate') {
|
if (!empty($item['target_id']) && $item['target_id'] != 'autocreate') {
|
||||||
$validate = FALSE;
|
$validate = FALSE;
|
||||||
foreach ($field['settings']['allowed_values'] as $settings) {
|
foreach ($field['settings']['allowed_values'] as $settings) {
|
||||||
// If no parent is specified, check if the term is in the vocabulary.
|
// If no parent is specified, check if the term is in the vocabulary.
|
||||||
if (isset($settings['vocabulary']) && empty($settings['parent'])) {
|
if (isset($settings['vocabulary']) && empty($settings['parent'])) {
|
||||||
if ($settings['vocabulary'] == $terms[$item['tid']]->bundle()) {
|
if ($settings['vocabulary'] == $terms[$item['target_id']]->bundle()) {
|
||||||
$validate = TRUE;
|
$validate = TRUE;
|
||||||
break;
|
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
|
// If a parent is specified, then to validate it must appear in the
|
||||||
// array returned by taxonomy_term_load_parents_all().
|
// array returned by taxonomy_term_load_parents_all().
|
||||||
elseif (!empty($settings['parent'])) {
|
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) {
|
foreach ($ancestors as $ancestor) {
|
||||||
if ($ancestor->id() == $settings['parent']) {
|
if ($ancestor->id() == $settings['parent']) {
|
||||||
$validate = TRUE;
|
$validate = TRUE;
|
||||||
|
@ -996,7 +996,7 @@ function taxonomy_field_validate(EntityInterface $entity = NULL, $field, $instan
|
||||||
* Implements hook_field_is_empty().
|
* Implements hook_field_is_empty().
|
||||||
*/
|
*/
|
||||||
function taxonomy_field_is_empty($item, $field_type) {
|
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) {
|
function taxonomy_field_presave(EntityInterface $entity, $field, $instance, $langcode, &$items) {
|
||||||
foreach ($items as $delta => $item) {
|
foreach ($items as $delta => $item) {
|
||||||
if (!$item['tid'] && isset($item['entity'])) {
|
if (!$item['target_id'] && isset($item['target_id'])) {
|
||||||
unset($item['tid']);
|
|
||||||
$item['entity']->save();
|
$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) {
|
foreach (field_available_languages('node', $field) as $langcode) {
|
||||||
if (!empty($items[$langcode])) {
|
if (!empty($items[$langcode])) {
|
||||||
foreach ($items[$langcode] as $item) {
|
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.
|
// 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',
|
'id' => 'standard',
|
||||||
'base' => 'taxonomy_term_data',
|
'base' => 'taxonomy_term_data',
|
||||||
'base field' => 'tid',
|
'base field' => 'tid',
|
||||||
|
@ -380,7 +380,7 @@ function taxonomy_field_views_data_views_data_alter(&$data, $field) {
|
||||||
'id' => 'entity_reverse',
|
'id' => 'entity_reverse',
|
||||||
'field_name' => $field['field_name'],
|
'field_name' => $field['field_name'],
|
||||||
'field table' => _field_sql_storage_tablename($field),
|
'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' => $entity_info['base_table'],
|
||||||
'base field' => $entity_info['entity_keys']['id'],
|
'base field' => $entity_info['entity_keys']['id'],
|
||||||
'label' => t('!field_name', array('!field_name' => $field['field_name'])),
|
'label' => t('!field_name', array('!field_name' => $field['field_name'])),
|
||||||
|
|
|
@ -87,7 +87,7 @@ class DefaultViewsTest extends ViewTestBase {
|
||||||
$term = $this->createTerm($this->vocabulary);
|
$term = $this->createTerm($this->vocabulary);
|
||||||
|
|
||||||
$values = array('created' => $time, 'type' => 'page');
|
$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.
|
// Make every other node promoted.
|
||||||
if ($i % 2) {
|
if ($i % 2) {
|
||||||
|
|
|
@ -17,7 +17,7 @@ cardinality: '-1'
|
||||||
translatable: '0'
|
translatable: '0'
|
||||||
entity_types: { }
|
entity_types: { }
|
||||||
indexes:
|
indexes:
|
||||||
tid:
|
target_id:
|
||||||
- tid
|
- target_id
|
||||||
status: 1
|
status: 1
|
||||||
langcode: und
|
langcode: und
|
||||||
|
|
Loading…
Reference in New Issue