From 7bb4e4d16437a46e74517af8f9c994b52ff704bd Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 5 Jan 2015 11:54:57 +0000 Subject: [PATCH] Issue #2401195 by idebr, corbacho: Vocabulary restriction not working in Entity reference fields --- .../selection/TermSelection.php | 3 +- .../src/Tests/TermEntityReferenceTest.php | 83 +++++++++++++++++++ 2 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php diff --git a/core/modules/taxonomy/src/Plugin/entity_reference/selection/TermSelection.php b/core/modules/taxonomy/src/Plugin/entity_reference/selection/TermSelection.php index 50f3bc14ba..58b3034acf 100644 --- a/core/modules/taxonomy/src/Plugin/entity_reference/selection/TermSelection.php +++ b/core/modules/taxonomy/src/Plugin/entity_reference/selection/TermSelection.php @@ -60,7 +60,8 @@ public function getReferenceableEntities($match = NULL, $match_operator = 'CONTA $options = array(); $bundles = entity_get_bundles('taxonomy_term'); - $bundle_names = !empty($this->instance['settings']['handler_settings']['target_bundles']) ? $this->instance['settings']['handler_settings']['target_bundles'] : array_keys($bundles); + $handler_settings = $this->fieldDefinition->getSetting('handler_settings'); + $bundle_names = !empty($handler_settings['target_bundles']) ? $handler_settings['target_bundles'] : array_keys($bundles); foreach ($bundle_names as $bundle) { if ($vocabulary = entity_load('taxonomy_vocabulary', $bundle)) { diff --git a/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php new file mode 100644 index 0000000000..5f4e930035 --- /dev/null +++ b/core/modules/taxonomy/src/Tests/TermEntityReferenceTest.php @@ -0,0 +1,83 @@ +createVocabulary(); + $vocabulary2 = $this->createVocabulary(); + + $term = $this->createTerm($vocabulary); + $term2 = $this->createTerm($vocabulary2); + + // Create an entity reference field. + $field_name = 'taxonomy_' . $vocabulary->id(); + $field_storage = FieldStorageConfig::create(array( + 'field_name' => $field_name, + 'entity_type' => 'entity_test', + 'translatable' => FALSE, + 'settings' => array( + 'target_type' => 'taxonomy_term', + ), + 'type' => 'entity_reference', + 'cardinality' => 1, + )); + $field_storage->save(); + $field = FieldConfig::create(array( + 'field_storage' => $field_storage, + 'entity_type' => 'entity_test', + 'bundle' => 'test_bundle', + 'settings' => array( + 'handler' => 'default', + 'handler_settings' => array( + // Restrict selection of terms to a single vocabulary. + 'target_bundles' => array( + $vocabulary->id() => $vocabulary->id(), + ), + ), + ), + )); + $field->save(); + + $handler = $this->container->get('plugin.manager.entity_reference.selection')->getSelectionHandler($field); + $result = $handler->getReferenceableEntities(); + + $expected_result = array( + $vocabulary->id() => array( + $term->id() => $term->getName(), + ), + ); + + $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.'); + } +} -- GitLab