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