Chris@18: installEntitySchema('entity_test'); Chris@18: $this->installEntitySchema('taxonomy_term'); Chris@18: $this->installEntitySchema('user'); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Tests an entity reference field restricted to a single vocabulary. Chris@18: * Chris@18: * Creates two vocabularies with a term, then set up the entity reference Chris@18: * field to limit the target vocabulary to one of them, ensuring that Chris@18: * the restriction applies. Chris@18: */ Chris@18: public function testSelectionTestVocabularyRestriction() { Chris@18: // Create two vocabularies. Chris@18: $vocabulary = Vocabulary::create([ Chris@18: 'name' => 'test1', Chris@18: 'vid' => 'test1', Chris@18: ]); Chris@18: $vocabulary->save(); Chris@18: $vocabulary2 = Vocabulary::create([ Chris@18: 'name' => 'test2', Chris@18: 'vid' => 'test2', Chris@18: ]); Chris@18: $vocabulary2->save(); Chris@18: Chris@18: $term = Term::create([ Chris@18: 'name' => 'term1', Chris@18: 'vid' => $vocabulary->id(), Chris@18: ]); Chris@18: $term->save(); Chris@18: $term2 = Term::create([ Chris@18: 'name' => 'term2', Chris@18: 'vid' => $vocabulary2->id(), Chris@18: ]); Chris@18: $term2->save(); Chris@18: Chris@18: // Create an entity reference field. Chris@18: $field_name = 'taxonomy_' . $vocabulary->id(); Chris@18: $field_storage = FieldStorageConfig::create([ Chris@18: 'field_name' => $field_name, Chris@18: 'entity_type' => 'entity_test', Chris@18: 'translatable' => FALSE, Chris@18: 'settings' => [ Chris@18: 'target_type' => 'taxonomy_term', Chris@18: ], Chris@18: 'type' => 'entity_reference', Chris@18: 'cardinality' => 1, Chris@18: ]); Chris@18: $field_storage->save(); Chris@18: $field = FieldConfig::create([ Chris@18: 'field_storage' => $field_storage, Chris@18: 'entity_type' => 'entity_test', Chris@18: 'bundle' => 'test_bundle', Chris@18: 'settings' => [ Chris@18: 'handler' => 'default', Chris@18: 'handler_settings' => [ Chris@18: // Restrict selection of terms to a single vocabulary. Chris@18: 'target_bundles' => [ Chris@18: $vocabulary->id() => $vocabulary->id(), Chris@18: ], Chris@18: ], Chris@18: ], Chris@18: ]); Chris@18: $field->save(); Chris@18: Chris@18: $handler = $this->container->get('plugin.manager.entity_reference_selection')->getSelectionHandler($field); Chris@18: $result = $handler->getReferenceableEntities(); Chris@18: Chris@18: $expected_result = [ Chris@18: $vocabulary->id() => [ Chris@18: $term->id() => $term->getName(), Chris@18: ], Chris@18: ]; Chris@18: Chris@18: $this->assertIdentical($result, $expected_result, 'Terms selection restricted to a single vocabulary.'); Chris@18: } Chris@18: Chris@18: }