diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php index 95514b7fb1..cb88279514 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/Term/TermHalJsonAnonTest.php @@ -52,15 +52,8 @@ protected function getExpectedNormalizedEntity() { $expected_parent_normalization_links = FALSE; $expected_parent_normalization_embedded = FALSE; + switch($parent_term_ids) { - case [TermInterface::ROOT_ID]: - $expected_parent_normalization_links = [ - NULL, - ]; - $expected_parent_normalization_embedded = [ - NULL, - ]; - break; case [2]: $expected_parent_normalization_links = [ [ @@ -83,30 +76,6 @@ protected function getExpectedNormalizedEntity() { ], ]; break; - case [TermInterface::ROOT_ID, 2]: - $expected_parent_normalization_links = [ - NULL, - [ - 'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json', - ], - ]; - $expected_parent_normalization_embedded = [ - NULL, - [ - '_links' => [ - 'self' => [ - 'href' => $this->baseUrl . '/taxonomy/term/2?_format=hal_json', - ], - 'type' => [ - 'href' => $this->baseUrl . '/rest/type/taxonomy_term/camelids', - ], - ], - 'uuid' => [ - ['value' => Term::load(2)->uuid()], - ], - ], - ]; - break; case [3, 2]: $expected_parent_normalization_links = [ [ diff --git a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php index d31155bfa7..9eb65303f4 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/Term/TermResourceTestBase.php @@ -105,13 +105,6 @@ protected function getExpectedNormalizedEntity() { $expected_parent_normalization = FALSE; switch($parent_term_ids) { - case [TermInterface::ROOT_ID]: - $expected_parent_normalization = [ - [ - 'target_id' => TermInterface::ROOT_ID, - ], - ]; - break; case [2]: $expected_parent_normalization = [ [ @@ -122,19 +115,6 @@ protected function getExpectedNormalizedEntity() { ], ]; break; - case [TermInterface::ROOT_ID, 2]: - $expected_parent_normalization = [ - [ - 'target_id' => TermInterface::ROOT_ID, - ], - [ - 'target_id' => 2, - 'target_type' => 'taxonomy_term', - 'target_uuid' => Term::load(2)->uuid(), - 'url' => base_path() . 'taxonomy/term/2', - ], - ]; - break; case [3, 2]: $expected_parent_normalization = [ [ @@ -334,15 +314,9 @@ public function testGetTermWithParent(array $parent_term_ids) { public function providerTestGetTermWithParent() { return [ - 'root parent: [0] (= no parent)' => [ - [TermInterface::ROOT_ID] - ], - 'non-root parent: [2]' => [ + 'one parent: [2]' => [ [2] ], - 'multiple parents: [0,2] (root + non-root parent)' => [ - [TermInterface::ROOT_ID, 2] - ], 'multiple parents: [3,2] (both non-root parents)' => [ [3, 2] ], diff --git a/core/modules/taxonomy/src/Entity/Term.php b/core/modules/taxonomy/src/Entity/Term.php index 26fb973482..439fe400d8 100644 --- a/core/modules/taxonomy/src/Entity/Term.php +++ b/core/modules/taxonomy/src/Entity/Term.php @@ -91,17 +91,6 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti } } - /** - * {@inheritdoc} - */ - public function preSave(EntityStorageInterface $storage) { - parent::preSave($storage); - // Terms with no parents are mandatory children of . - if (!$this->get('parent')->count()) { - $this->parent->target_id = static::ROOT_ID; - } - } - /** * {@inheritdoc} */ diff --git a/core/modules/taxonomy/src/TermInterface.php b/core/modules/taxonomy/src/TermInterface.php index cfba19d5a5..3087a56ecb 100644 --- a/core/modules/taxonomy/src/TermInterface.php +++ b/core/modules/taxonomy/src/TermInterface.php @@ -10,11 +10,6 @@ */ interface TermInterface extends ContentEntityInterface, EntityChangedInterface { - /** - * Root ID for terms with no parent. - */ - const ROOT_ID = 0; - /** * Gets the term's description. * diff --git a/core/modules/taxonomy/src/TermStorage.php b/core/modules/taxonomy/src/TermStorage.php index 2db0392aea..f13ceb931c 100644 --- a/core/modules/taxonomy/src/TermStorage.php +++ b/core/modules/taxonomy/src/TermStorage.php @@ -107,39 +107,13 @@ public function loadParents($tid) { * Returns a list of parents of this term. * * @return \Drupal\taxonomy\TermInterface[] - * The parent taxonomy term entities keyed by term ID. If this term has a - * parent, that item is keyed with ROOT_ID and will have NULL as - * value. + * The parent taxonomy term entities keyed by term ID. * * @internal * @todo Refactor away when TreeInterface is introduced. */ protected function getParents(TermInterface $term) { - $parents = $ids = []; - // Cannot use $this->get('parent')->referencedEntities() here because that - // strips out the '0' reference. - foreach ($term->get('parent') as $item) { - if ($item->target_id == $term::ROOT_ID) { - // The parent. - $parents[0] = NULL; - continue; - } - $ids[] = $item->target_id; - } - - // @todo Better way to do this? AND handle the NULL/0 parent? - // Querying the terms again so that the same access checks are run when - // getParents() is called as in Drupal version prior to 8.3. - $loaded_parents = []; - - if ($ids) { - $query = \Drupal::entityQuery('taxonomy_term') - ->condition('tid', $ids, 'IN'); - - $loaded_parents = static::loadMultiple($query->execute()); - } - - return $parents + $loaded_parents; + return $term->get('parent')->referencedEntities(); } /** diff --git a/core/modules/taxonomy/src/VocabularyStorage.php b/core/modules/taxonomy/src/VocabularyStorage.php index 0538e71b49..f06a15759e 100644 --- a/core/modules/taxonomy/src/VocabularyStorage.php +++ b/core/modules/taxonomy/src/VocabularyStorage.php @@ -24,7 +24,7 @@ public function resetCache(array $ids = NULL) { public function getToplevelTids($vids) { $tids = \Drupal::entityQuery('taxonomy_term') ->condition('vid', $vids, 'IN') - ->condition('parent.target_id', TermInterface::ROOT_ID) + ->notExists('parent.target_id') ->execute(); return array_values($tids); diff --git a/core/modules/taxonomy/taxonomy.install b/core/modules/taxonomy/taxonomy.install index 702ca7ce83..b53767367c 100644 --- a/core/modules/taxonomy/taxonomy.install +++ b/core/modules/taxonomy/taxonomy.install @@ -27,19 +27,22 @@ function taxonomy_update_8502(&$sandbox) { if (!isset($sandbox['current'])) { // Set batch ops sandbox. $sandbox['current'] = 0; - $sandbox['max'] = $database->select('taxonomy_term_hierarchy') - ->countQuery() - ->execute() - ->fetchField(); + + $query = $database->select('taxonomy_term_hierarchy', 'h'); + $query->condition('h.parent', 0, '>'); + $query->countQuery(); + + $sandbox['max'] = $query->execute()->fetchField(); } // Save the hierarchy. $select = $database->select('taxonomy_term_hierarchy', 'h'); $select->join('taxonomy_term_data', 'd', 'h.tid = d.tid'); - $hierarchy = $select - ->fields('h', ['tid', 'parent']) + $select->fields('h', ['tid', 'parent']) ->fields('d', ['vid', 'langcode']) - ->range($sandbox['current'], $sandbox['current'] + 100) + ->condition('h.parent', 0, '>'); + + $hierarchy = $select->range($sandbox['current'], $sandbox['current'] + 100) ->execute() ->fetchAll(); @@ -74,9 +77,8 @@ function taxonomy_update_8502(&$sandbox) { if ($sandbox['#finished'] >= 1) { // Drop the legacy table. $database->schema()->dropTable('taxonomy_term_hierarchy'); - return t('Taxonomy term legacy tables dropped.'); - return t('Taxonomy term hierarchy converted to default entity reference storage.'); + return t('Taxonomy term hierarchy converted to default entity reference storage and legacy term hierarchy table (taxonomy_term_hierarchy) dropped.'); } } diff --git a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateTaxonomyTermTest.php b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateTaxonomyTermTest.php index b6cf73f65c..fcf25fac9f 100644 --- a/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateTaxonomyTermTest.php +++ b/core/modules/taxonomy/tests/src/Kernel/Migrate/d6/MigrateTaxonomyTermTest.php @@ -93,7 +93,7 @@ public function testTaxonomyTerms() { $this->assertSame($values['vid'], $term->vid->target_id); $this->assertSame((string) $values['weight'], $term->weight->value); if ($values['parent'] === [0]) { - $this->assertSame((int) $term->parent->target_id, TermInterface::ROOT_ID); + $this->assertNull($term->parent->target_id); } else { $parents = [];