I'm attempting to follow this documentation to add a custom constraint to a field in a Paragraph but I'm having trouble actually accessing that field.

Currently I have my_content_type with paragraph_field. This field accepts a number of different paragraph types. I'd like to be able to add a constraint to a field on a specific paragraph type that could be added to that field.

I've tried using hook_entity_bundle_field_info_alter, hook_form_alter, and hook_field_widget_form_alter but am having trouble actually accessing the proper field.

Is there a better way to do this?


@Vecta I hope this help you

function MYMODULE_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  //with a node
  if ($entity_type->id() === 'node') {
    if ($bundle == 'article') {
      if (isset($fields['title'])) {
        // change constraint id
        $fields['title']->addConstraint('UniqueCode', []);
  //with a paragraph
  if ($entity_type->id() === 'paragraph') {
    // use the paragraph machine name 
    if ($bundle == 'paragraph_name') {
      if (isset($fields['field_name'])) {
        // change constraint id
        $fields['field_name']->addConstraint('ConstraintId', []);
|improve this answer|||||

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.