diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php index ec75f35..d85f200 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/Core/Entity/Term.php @@ -80,6 +80,7 @@ public function getName() { * {@inheritdoc} */ public function getParents() { + $parents = array(); foreach ($this->get('parent') as $parent) { $parents[] = $parent->value; @@ -90,6 +91,26 @@ public function getParents() { /** * {@inheritdoc} */ + public function getSoleParent() { + $parents = $this->getParents(); + if (count($parents) == 1) { + return $parents[0]; + } + else { + return FALSE; + } + } + + /** + * {@inheritdoc} + */ + public function hasParents() { + return count($this->getParents() != 0); + } + + /** + * {@inheritdoc} + */ public function getWeight() { return $this->get('weight')->value; } @@ -118,8 +139,23 @@ public function setName($name) { /** * {@inheritdoc} */ - public function setParent($parent) { - return $this->set('parent', $parent); + public function clearParents() { + return $this->set('parent', array()); + } + + /** + * {@inheritdoc} + */ + public function addParents($parents) { + + if (!is_array($parents)) { + $parents = array($parents); + } + + $current_parents = $this->getParents(); + + return $this->set('parent', array_merge($parents, $current_parents)); + } /** @@ -171,7 +207,7 @@ public static function postDelete(EntityStorageControllerInterface $storage_cont public function postSave(EntityStorageControllerInterface $storage_controller, $update = TRUE) { // Only change the parents if a value is set, keep the existing values if // not. - if (count($this->getParents())) { + if ($this->hasParents()) { $storage_controller->deleteTermHierarchy(array($this->id())); $storage_controller->updateTermHierarchy($this); } diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermInterface.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermInterface.php index f5572d6..12dd4d8 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermInterface.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermInterface.php @@ -47,6 +47,24 @@ public function getName(); public function getParents(); /** + * If this term has exactly one parent, returns the parent's tid, + * otherwise returns FALSE. + * + * @return int|FALSE + * The tid of the parent or FALSE. + */ + public function getSoleParent(); + + /** + * Returns TRUE is this term has parents. Returning FALSE indicates + * that this is a top-level term. + * + * @return boolean + * TRUE if term has parents, FALSE if it has no parents. + */ + public function hasParents(); + + /** * Returns the weight of this term. * * @return int @@ -85,14 +103,21 @@ public function setFormat($format); public function setName($name); /** - * Returns the parent term(s) for this term. + * Adds parents to this term. * - * @param int $weight - * The term's parent - * @return int - * The parent term + * @param mixed $parent + * A tid or array of tids to add to the list of parent terms + * for this term. + * @return void + */ + public function addParents($parents); + + /** + * Removes any parents from this term + * + * @return void */ - public function setParent($parent); + public function clearParents(); /** * Returns the weight of this term. diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php b/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php index cb0821c..85a28b9 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/TermStorageController.php @@ -17,6 +17,20 @@ class TermStorageController extends DatabaseStorageControllerNG implements TermStorageControllerInterface { /** + * Attempt to load the term parents + * + * {@inheritdoc} + */ + protected function attachLoad(&$terms, $load_revision = FALSE) { + parent::attachLoad($terms, $load_revision); + foreach($terms as $idx => $term) { + $parents = array_keys(taxonomy_term_load_parents($term->id())); + $term->parent->setValue($parents); + $terms[$idx] = $term; + } + } + + /** * Overrides Drupal\Core\Entity\DatabaseStorageController::create(). * * @param array $values diff --git a/core/modules/taxonomy/taxonomy.admin.inc b/core/modules/taxonomy/taxonomy.admin.inc index 274e30e..02c3bcd 100644 --- a/core/modules/taxonomy/taxonomy.admin.inc +++ b/core/modules/taxonomy/taxonomy.admin.inc @@ -178,7 +178,7 @@ function taxonomy_overview_terms($form, &$form_state, Vocabulary $vocabulary) { '#type' => 'hidden', // Yes, default_value on a hidden. It needs to be changeable by the // javascript. - '#default_value' => $term->getParents(), + '#default_value' => $term->getSoleParent(), '#attributes' => array( 'class' => array('term-parent'), ), @@ -329,12 +329,12 @@ function taxonomy_overview_terms_submit($form, &$form_state) { $weight = 0; $term = $tree[0]; while ($term->id() != $form['#first_tid']) { - if (count($term->getParents()) == 0 && $term->getWeight() != $weight) { + if (!count($term->getParents()) && $term->getWeight() != $weight) { $term->setWeight($weight); $changed_terms[$term->id()] = $term; } $weight++; - $hierarchy = count($term->getParents()) != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy; + $hierarchy = count($term->getParents()) == 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy; $term = $tree[$weight]; } @@ -350,15 +350,16 @@ function taxonomy_overview_terms_submit($form, &$form_state) { } // Terms not at the root level can safely start from 0 because they're all on this page. elseif ($values['term']['parent'] > 0) { - $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']->getParent()] + 1 : 0; + $level_weights[$values['term']['parent']] = isset($level_weights[$values['term']['parent']]) ? $level_weights[$values['term']['parent']] + 1 : 0; if ($level_weights[$values['term']['parent']] != $term->getWeight()) { $term->setWeight($level_weights[$values['term']['parent']]); $changed_terms[$term->id()] = $term; } } // Update any changed parents. - if ($values['term']['parent'] != $term->getParent()) { - $term->setParent($values['term']['parent']); + if ($values['term']['parent'] != $term->getSoleParent()) { + $term->clearParents(); + $term->addParents($values['term']['parent']); $changed_terms[$term->id()] = $term; } $hierarchy = count($term->getParents()) == 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy; @@ -373,7 +374,7 @@ function taxonomy_overview_terms_submit($form, &$form_state) { $term->setWeight($weight); $changed_terms[$term->id()] = $term; } - $hierarchy = $term->setParent() != 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy; + $hierarchy = count($term->getParents()) == 0 ? TAXONOMY_HIERARCHY_SINGLE : $hierarchy; } // Save all updated terms. diff --git a/core/modules/taxonomy/taxonomy.module b/core/modules/taxonomy/taxonomy.module index c21b665..96952b3 100644 --- a/core/modules/taxonomy/taxonomy.module +++ b/core/modules/taxonomy/taxonomy.module @@ -716,9 +716,7 @@ function taxonomy_get_tree($vid, $parent = 0, $max_depth = NULL, $load_entities $term = clone $term; } $term->depth = $depth; - unset($term->parent); $tid = $load_entities ? $term->id() : $term->tid; - $term->parents = $parents[$vid][$tid]; $tree[] = $term; if (!empty($children[$vid][$tid])) { $has_children = TRUE;