diff --git a/core/modules/taxonomy/src/Entity/Term.php b/core/modules/taxonomy/src/Entity/Term.php index 51e295a5177fc21a6b06d45a4501c83a2e8c8807..77cefcc0dca268ea713ccfea2b2e3263f7ecbd13 100644 --- a/core/modules/taxonomy/src/Entity/Term.php +++ b/core/modules/taxonomy/src/Entity/Term.php @@ -176,6 +176,16 @@ public static function baseFieldDefinitions(EntityTypeInterface $entity_type) { return $fields; } + /** + * {@inheritdoc} + */ + public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions) { + // Only terms in the same bundle can be a parent. + $fields['parent'] = clone $base_field_definitions['parent']; + $fields['parent']->setSetting('handler_settings', ['target_bundles' => [$bundle => $bundle]]); + return $fields; + } + /** * {@inheritdoc} */ diff --git a/core/modules/taxonomy/src/Tests/TermTest.php b/core/modules/taxonomy/src/Tests/TermTest.php index abcde83d33017c96165708734cb862f21b7d7e2c..54309c4237e2a8f5089c93fc2c1aea53880a7eaf 100644 --- a/core/modules/taxonomy/src/Tests/TermTest.php +++ b/core/modules/taxonomy/src/Tests/TermTest.php @@ -72,6 +72,15 @@ protected function setUp() { ->save(); } + /** + * The "parent" field must restrict references to the same vocabulary. + */ + public function testParentHandlerSettings() { + $vocabulary_fields = \Drupal::service('entity_field.manager')->getFieldDefinitions('taxonomy_term', $this->vocabulary->id()); + $parent_target_bundles = $vocabulary_fields['parent']->getSetting('handler_settings')['target_bundles']; + $this->assertIdentical([$this->vocabulary->id() => $this->vocabulary->id()], $parent_target_bundles); + } + /** * Test terms in a single and multiple hierarchy. */