0

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

4
  • When you say "the weight is modified manually" - how is that happening exactly? Drag and drop is just a JS layer over the weight selects, it updates those same selects for the form submission, and as field items don't store a "weight" other than the delta, I would've assumed that weight and delta are one and the same thing (in terms of sort order) in all contexts. Is there another mechanism you can use to pass/set the weight of a field item?
    – Clive
    Oct 21, 2021 at 16:10
  • With the drag&drop feature natively implemented in Drupal. You always have the option to switch to a manual management of the items order adapting the weight through the weight field. As you correctly assume, the weight is then used for the recalculating of the delta and is not stored. When validating the form I need to get the submitted weight values in a non drag&drop scenario but they don't seem accessible.
    – abelass
    Oct 22, 2021 at 6:34
  • I doubt it’ll be available without hacking at the validation process, constraints probably need to be able to validate without external influence as they can be invoked without a form being involved. But what information does that weight give you that you can’t infer from the delta anyway? Understanding what you’re trying to do might help to at least suggest an alternative if there’s no easy direct answer to the question
    – Clive
    Oct 22, 2021 at 8:18
  • Thanks @Clive, I needed to test the order against the paragraph item's date to be sure that the top item is always the most recent one. This is a historical feature from the application in d7. But after some more reflection I 'd better reorder following the date after submitting, this is more user friendly anyway.
    – abelass
    Oct 22, 2021 at 10:28

1 Answer 1

0

In the meantime i found this tutorial https://philfrilling.com/blog/2018-09/custom-validation-multiple-dependent-entity-fields-drupal-8 that would have solved my problem. Making a restriction on the whole entity instead of the field only gives access to the info I needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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