I am validating a multi value paragraph field using constraints.
/**
* Implements hook_entity_bundle_field_info_alter().
*/
function cnfpt_internet_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
if ($bundle === 'manifestation') {
if (isset($fields['field_occurence'])) {
// Add a validation.
$fields['field_occurence']->addConstraint('Occurrence', []);
}
}
}
/**
* Checks the mandatory fields of an occurence and ordere of the occurrences.
*
* @Constraint(
* id = "Occurrence",
* label = @Translation("Champ obligatoire par type d'occurence", context = "Validation"),
* type = "item"
* )
*/
class OccurrenceConstraint extends Constraint {
// Modalité requise pour occurrence distante.
public $modaliteRequise = 'Le champ Modalité est requis';
// Ville requise pour occurrence presentiel.
public $villeRequise = 'Le champ Ville est requis';
// Order of occurence
public $ordreOccurrence = 'Les occurrences doivent être placées dans l\'ordre chronologique';
// First occurence needs a contact.
public $premiereOccurrenceContact = 'Un contact est obligatoire pour la première occurence';
}
field_occurence
ist the paragraph field
My validate method starts like
* Validates the Occurrence constraint.
*/
class OccurrenceConstraintValidator extends ConstraintValidator {
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
I have to make a validation taking into account the order of the paragraphs. If the paragraph is ordered with drag&drop there is no issue, I can take the delta value of items.
But if the weight is modified manually, I don't see how to get the modified weight value.
Any idea how to get the manually modified weight value?
Thanks
Rainer