Score:0

Custom validation constraints multi value paragraphs : how to get weight value

nl flag

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

cn flag
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?
abelass avatar
nl flag
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.
cn flag
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
abelass avatar
nl flag
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.
Score:0
nl flag

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.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.