1

I used hook_entity_bundle_field_info_alter to validate fields in node as below:

function my_module_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node' && !empty($fields['title'])) {
    $fields['title']->addConstraint('UniqueField', []);
  }
}

The validation I wrote in UniqueFieldConstraintValidator Plugin works for "article" bundle but not for any other bundles. What am I doing wrong?

1 Answer 1

2

You're using the bundle alter hook, which is only called on by-bundle fields.

Title is a base field but it can be overridden by a per-bundle through field override.. this is what happens if you chose a different title through the node type settings form. I guess that's why it's only available sometimes.

Try to use my_module_entity_base_field_info_alter(). Due to the override, which completely replaces the base field, you actually might need both hooks.. I don't know if the constraint is transported to the field override correctly and certainly not if the field override already exists when you add the alter hook. Not much that can be done about that I fear.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.