Commit 320ebd03 authored by alexpott's avatar alexpott

Issue #1915056 by Arla, Berdir, amateescu | catch: Use entity reference for taxonomy parents.

parent 537cc876
......@@ -93,13 +93,21 @@ public function testTerm() {
$vocabulary = entity_create('taxonomy_vocabulary', array('vid' => 'example_vocabulary'));
$vocabulary->save();
// @todo Until https://www.drupal.org/node/2327935 is fixed, if no parent is
// set, the test fails because target_id => 0 is reserialized to NULL.
$term_parent = entity_create('taxonomy_term', array(
'name' => $this->randomMachineName(),
'vid' => $vocabulary->id(),
));
$term_parent->save();
$term = entity_create('taxonomy_term', array(
'name' => $this->randomMachineName(),
'vid' => $vocabulary->id(),
'description' => array(
'value' => $this->randomMachineName(),
'format' => $this->randomMachineName(),
)
),
'parent' => $term_parent->id(),
));
$term->save();
......
......@@ -93,7 +93,7 @@ public function testTaxonomyTerms() {
$this->assertEqual($term->vid->target_id, $values['vid']);
$this->assertEqual($term->weight->value, $values['weight']);
if ($values['parent'] === array(0)) {
$this->assertEqual($term->parent->value, 0);
$this->assertEqual($term->parent->target_id, 0);
}
else {
$parents = array();
......
......@@ -92,7 +92,7 @@ public function postSave(EntityStorageInterface $storage, $update = TRUE) {
// Only change the parents if a value is set, keep the existing values if
// not.
if (isset($this->parent->value)) {
if (isset($this->parent->target_id)) {
$storage->deleteTermHierarchy(array($this->id()));
$storage->updateTermHierarchy($this);
}
......@@ -161,16 +161,12 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) {
->setDescription(t('The weight of this term in relation to other terms.'))
->setDefaultValue(0);
// @todo Convert this to an entity_reference field, see
// https://drupal.org/node/1915056
$fields['parent'] = BaseFieldDefinition::create('integer')
$fields['parent'] = BaseFieldDefinition::create('entity_reference')
->setLabel(t('Term Parents'))
->setDescription(t('The parents of this term.'))
->setSetting('target_type', 'taxonomy_term')
->setCardinality(BaseFieldDefinition::CARDINALITY_UNLIMITED)
// Save new terms with no parents by default.
->setDefaultValue(0)
->setSetting('unsigned', TRUE)
->addConstraint('TermParent', array());
->setCustomStorage(TRUE);
$fields['changed'] = BaseFieldDefinition::create('changed')
->setLabel(t('Changed'))
......
......@@ -415,7 +415,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
}
// Update any changed parents.
if ($values['term']['parent'] != $term->parents[0]) {
$term->parent->value = $values['term']['parent'];
$term->parent->target_id = $values['term']['parent'];
$changed_terms[$term->id()] = $term;
}
$hierarchy = $term->parents[0] != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy;
......@@ -427,7 +427,7 @@ public function submitForm(array &$form, FormStateInterface $form_state) {
for ($weight; $weight < count($tree); $weight++) {
$term = $tree[$weight];
if ($term->parents[0] == 0 && $term->getWeight() != $weight) {
$term->parent->value = $term->parents[0];
$term->parent->target_id = $term->parents[0];
$term->setWeight($weight);
$changed_terms[$term->id()] = $term;
}
......
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\Validation\Constraint\TermParentConstraint.
*/
namespace Drupal\taxonomy\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Drupal\Component\Annotation\Plugin;
use Drupal\Core\Annotation\Translation;
/**
* Checks if a value is a valid taxonomy term parent (term id or 0).
*
* @Plugin(
* id = "TermParent",
* label = @Translation("Term parent", context = "Validation")
* )
*/
class TermParentConstraint extends Constraint {
/**
* The default violation message.
*
* @var string
*/
public $message = '%id is not a valid parent for this term.';
}
<?php
/**
* @file
* Contains \Drupal\taxonomy\Plugin\Validation\Constraint\TermParentConstraintValidator.
*/
namespace Drupal\taxonomy\Plugin\Validation\Constraint;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
/**
* Validates the TermParent constraint.
*/
class TermParentConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($field_item, Constraint $constraint) {
if ($field_item) {
$parent_term_id = $field_item->value;
// If a non-0 parent term id is specified, ensure it corresponds to a real
// term in the same vocabulary.
if ($parent_term_id && !\Drupal::entityManager()->getStorage('taxonomy_term')->loadByProperties(array('tid' => $parent_term_id, 'vid' => $field_item->getEntity()->vid->value))) {
$this->context->addViolation($constraint->message, array('%id' => $parent_term_id));
}
}
}
}
......@@ -76,7 +76,7 @@ public function updateTermHierarchy(EntityInterface $term) {
foreach ($term->parent as $parent) {
$query->values(array(
'tid' => $term->id(),
'parent' => (int) $parent->value,
'parent' => (int) $parent->target_id,
));
}
$query->execute();
......
......@@ -63,6 +63,10 @@ public function testValidation() {
$term->set('parent', 9999);
$violations = $term->validate();
$this->assertEqual(count($violations), 1, 'Violation found when term parent is invalid.');
$this->assertEqual($violations[0]->getMessage(), format_string('%id is not a valid parent for this term.', array('%id' => 9999)));
$this->assertEqual($violations[0]->getMessage(), format_string('The referenced entity (%type: %id) does not exist.', array('%type' => 'taxonomy_term', '%id' => 9999)));
$term->set('parent', 0);
$violations = $term->validate();
$this->assertEqual(count($violations), 0, 'No violations for parent id 0.');
}
}
......@@ -78,7 +78,7 @@ public function testFilterUI() {
for ($i = 0; $i < 3; $i++) {
for ($j = 0; $j <= $i; $j++) {
$option = $result[$counter++];
$prefix = $terms[$i][$j]->parent->value ? '-' : '';
$prefix = $terms[$i][$j]->parent->target_id ? '-' : '';
$attributes = $option->attributes();
$tid = (string) $attributes->value;
......
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