Issue #2977882 by Wim Leers: Term entity type's "parent" field does not specify "target_bundles" setting: a term from *any* vocabulary can be chosen

merge-requests/1654/head
Nathaniel Catchpole 2018-06-19 11:44:01 +01:00
parent 49a45db6f8
commit e60979f160
2 changed files with 19 additions and 0 deletions

View File

@ -176,6 +176,16 @@ class Term extends ContentEntityBase implements TermInterface {
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}
*/

View File

@ -72,6 +72,15 @@ class TermTest extends TaxonomyTestBase {
->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.
*/