Skip to main content
added correct code.
Source Link
miststudent2011
  • 1.8k
  • 1
  • 12
  • 36

You need to add entity_type_id aswell in the hook.

It should be

namespace Drupal\my_module\Plugin\Validation\Constraint;

use Drupal\Core\Entity\EntityTypeInterface;Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;


/**
 * ImplementsValidates hook_entity_bundle_field_info_alter()the MyConstraint .
 */
class MyConstraintValidator extends ConstraintValidator {


  /**
   * {@inheritdoc}
   */
  public function mymodule_entity_bundle_field_info_altervalidate(&$fields$items, EntityTypeInterface $entity_type,Constraint $bundle$constraint)
  { 

    if ($entity_type!$item = $items->id>first()) ==={
 'paragraph' && $bundle === 'my_paragraph_bundle' &&return;
 isset   }

    $field_name = $items->getFieldDefinition($fields['my_paragraph_field'])->getName();

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $items->getEntity();

    if ($entity->hasField('my_paragraph_field')) {

      $fields['my_paragraph_field']$value = $entity->addConstraint>my_paragraph_field->value;


      // Check if the value matches or not.
      if ('MyConstraint'$value != 10) {

        $this->context->addViolation($constraint->message, [][
          '%value' => 10
        ]);
      }

    }

  }

}

For reference : Adding Constraint to entity referenced Paragraph field

You need to add entity_type_id aswell in the hook.

It should be

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
 {
   if ($entity_type->id() === 'paragraph' && $bundle === 'my_paragraph_bundle' && isset($fields['my_paragraph_field'])) {

      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

For reference : Adding Constraint to entity referenced Paragraph field

You need to add

namespace Drupal\my_module\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;


/**
 * Validates the MyConstraint .
 */
class MyConstraintValidator extends ConstraintValidator {


  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) { 

    if (!$item = $items->first()) {
      return;
    }

    $field_name = $items->getFieldDefinition()->getName();

    /** @var \Drupal\Core\Entity\EntityInterface $entity */
    $entity = $items->getEntity();

    if ($entity->hasField('my_paragraph_field')) {

      $value = $entity->my_paragraph_field->value;


      // Check if the value matches or not.
      if ($value != 10) {

        $this->context->addViolation($constraint->message, [
          '%value' => 10
        ]);
      }

    }

  }

}

For reference : Adding Constraint to entity referenced Paragraph field

added 4 characters in body
Source Link
miststudent2011
  • 1.8k
  • 1
  • 12
  • 36

You need to add entity_type_id aswell in the hook.

It should be

use Drupal\Core\Entity\EntityTypeInterface;

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
{
   if ($entity_type->id() === 'paragraph' && $bundle === 'my_paragraph_bundle' && isset($fields['my_paragraph_field'])) {

      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

For reference : Adding Constraint to entity referenced Paragraph field

You need to add entity_type_id aswell in the hook.

It should be

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
{
   if ($entity_type->id() === 'paragraph' && $bundle === 'my_paragraph_bundle' && isset($fields['my_paragraph_field'])) {

      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

For reference : Adding Constraint to entity referenced Paragraph field

You need to add entity_type_id aswell in the hook.

It should be

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
{
   if ($entity_type->id() === 'paragraph' && $bundle === 'my_paragraph_bundle' && isset($fields['my_paragraph_field'])) {

      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

For reference : Adding Constraint to entity referenced Paragraph field

Source Link
miststudent2011
  • 1.8k
  • 1
  • 12
  • 36

You need to add entity_type_id aswell in the hook.

It should be

use Drupal\Core\Entity\EntityTypeInterface;

/**
 * Implements hook_entity_bundle_field_info_alter().
 */

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
{
   if ($entity_type->id() === 'paragraph' && $bundle === 'my_paragraph_bundle' && isset($fields['my_paragraph_field'])) {

      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

For reference : Adding Constraint to entity referenced Paragraph field