diff --git a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php index 5a663b5..47b7c26 100644 --- a/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php +++ b/core/modules/taxonomy/lib/Drupal/taxonomy/Plugin/field/field_type/TaxonomyTermReferenceItem.php @@ -7,13 +7,9 @@ namespace Drupal\taxonomy\Plugin\field\field_type; -use Drupal\Core\Entity\Annotation\FieldType; -use Drupal\Core\Annotation\Translation; use Drupal\field\FieldInterface; use Drupal\field\Plugin\Type\FieldType\ConfigEntityReferenceItemBase; use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemInterface; -use Drupal\Core\Session\AccountInterface; -use Drupal\Core\TypedData\AllowedValuesInterface; /** * Plugin implementation of the 'term_reference' field type. @@ -36,44 +32,7 @@ * default_formatter = "taxonomy_term_reference_link" * ) */ -class TaxonomyTermReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface, AllowedValuesInterface { - - /** - * {@inheritdoc} - */ - public function getPossibleValues(AccountInterface $account = NULL) { - // Flatten options firstly, because Possible Options may contain group - // arrays. - $flatten_options = $this->flattenOptions($this->getPossibleOptions($account)); - return array_keys($flatten_options); - } - - /** - * {@inheritdoc} - */ - public function getPossibleOptions(AccountInterface $account = NULL) { - return $this->getSettableOptions($account); - } - - /** - * {@inheritdoc} - */ - public function getSettableValues(AccountInterface $account = NULL) { - // Flatten options firstly, because Settable Options may contain group - // arrays. - $flatten_options = $this->flattenOptions($this->getSettableOptions($account)); - return array_keys($flatten_options); - } - - /** - * {@inheritdoc} - */ - public function getSettableOptions(AccountInterface $account = NULL) { - $instance = $this->getFieldDefinition(); - $entity = $this->getParent()->getParent(); - $function = $this->getFieldSetting('options_list_callback') ? $this->getFieldSetting('options_list_callback') : 'taxonomy_allowed_values'; - return $function($instance, $entity); - } +class TaxonomyTermReferenceItem extends ConfigEntityReferenceItemBase implements ConfigFieldItemInterface { /** * {@inheritdoc} @@ -149,6 +108,38 @@ public function instanceSettingsForm(array $form, array &$form_state) { } /** + * {@inheritdoc} + * + * Taxonomy field settings allow for either a single vocabulary ID, multiple + * vocabulary IDs, or sub-trees of a vocabulary to be specified as allowed + * values, although only the first of these is supported via the field UI. + * Confirm that terms entered as values meet at least one of these conditions. + */ + public function getConstraints() { + $target_id = $this->get('target_id')->getValue(); + + $constraint_manager = \Drupal::typedData()->getValidationConstraintManager(); + $constraints = parent::getConstraints(); + + $instance = $this->getFieldDefinition(); + $entity = $entity = $this->getParent()->getParent(); + $function = $this->getFieldSetting('options_list_callback') ? $this->getFieldSetting('options_list_callback') : 'taxonomy_allowed_values'; + $choice = array_keys($this->flattenOptions($function($instance, $entity))); + + if (!empty($target_id)) { + $constraints[] = $constraint_manager->create('ComplexData', array( + 'target_id' => array( + 'Choice' => array( + 'choices' => $choice, + 'message' => t('%name: illegal value.', array('%name' => $instance['label'])), + ), + ), + )); + } + return $constraints; + } + + /** * Flattens an array of allowed values. * * @param array $array