3

I've just started to use Drupal 8's Entity Validation API and I'm applying constraints to entities in the following way using hook_entity_bundle_field_info_alter:

MYMODULE_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {

  switch ($bundle) {
    case 'example_content_type':
      $overlay_text = $fields['field_example_text']; 
      $overlay_text->addConstraint('NotNull');
        break;
  }
}

This successfully prevents the an instance of this content type being created if no value is entered in the field_example_text.

However, in this content type, I'm using conditional fields. In this scenario, I would only want to apply the NotNull constraint to field_example_text if another field, field_colors, a text list checkbox field where the user can check any number of options, has a certain value (e.g. 'Green') checked.

Conditional fields is hiding and showing dependent fields nicely based on which values in field_colors are checked, but I need to set these fields as not null via the constraints only when field_colors has a particular value. Is this possible?

1

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.