Score:1

How to validate a paragraph field?

sa flag

I have a paragraph type with 2 fields: year and quarter

I have a content type with a paragraph field (field_year_and_quarter) targeting ONLY this paragraph type

I am trying to create a constraint to disallow the encoding of a duplicate (same year/quarter)

I have tried:

1-A constraint at the node level which is looping into field_year_and_quarter with $node->get('field_year_and_quarter')->referencedEntities() to check if any duplicate

=>This is not working as it is not taking into account what is encoded in the widget, only the last saved version is checked

2-A constraint at the paragraph level which is getting its Parent to be able to loop into field_year_and_quarter with $node_parent->get('field_year_and_quarter')->referencedEntities() to check if any duplicate [I replace one item by the paragraph provided in the parameter ]

Here is the code:

  public function validate($certificate_p, Constraint $constraint) {
    /* @var \Drupal\paragraphs\Entity\Paragraph $certificate_p */
    if ($certificate_p->bundle() == 'qoc') {
      $prod_company = $certificate_p->getParentEntity();
      $certificates = $prod_company->get('field_year_and_quarter')
        ->referencedEntities();
      $all_certificates = [];
      foreach ($certificates as $certificate) {
        // use the paragraph provided in the function parameter
        $certif_to_check = ($certificate_p->id() == $certificate->id()) ? $certificate_p : $certificate;
        $year = $certif_to_check->get('field_qoc_year')->value;
        $quarter = $certif_to_check->get('field_qoc_quarter')->value;
        // Year/Quarter are already encoded
        if (isset($all_certificates[$year][$quarter])) {
          $this->context->addViolation($constraint->duplicate_certif, [
            '%quarter' => surround_by_nbsp($quarter),
            '%year' => surround_by_nbsp($year),
          ]);
          break;
        }
        $all_certificates[$year][$quarter] = TRUE;
      }
    }

=>This is not working for the same reason

3- A constraint at the field level... here is the code:

  public function validate($items, Constraint $constraint) {
    $certificates = $items->referencedEntities();
    $all_certificates = [];
    foreach ($certificates as $certificate) {
      $year = $certificate->get('field_qoc_year')->value;
      $quarter = $certificate->get('field_qoc_quarter')->value;
      // Year/Quarter are already encoded
      if (isset($all_certificates[$year][$quarter])) {
        $this->context->addViolation($constraint->duplicate_certif, [
          '%quarter' => surround_by_nbsp($quarter),
          '%year' => surround_by_nbsp($year),
        ]);
        break;
      }
      $all_certificates[$year][$quarter] = TRUE;
    }
  }

=>Not working for the same reason

What should I do? (I have also tried to validate in the form without luck)

4uk4 avatar
cn flag
It's no problem to validate multiple paragraphs in a constraint, see https://drupal.stackexchange.com/questions/278067/adding-constraint-to-paragraph-field. Could this be conneced with the IEF issue from the last question? Also why is the variable a core EntityReferenceFieldItemList and not an [ERR](https://www.drupal.org/project/entity_reference_revisions) field?
Baud avatar
sa flag
1-I have edited the question to remove the wrong reference. 2-I have removed the IEF. 3-The example you gave will works because it is not checking the internal values... This is a constraint at the field level (like my example 3) but, in my case, it keep saying I have a duplicate even if I edit the form to remove it (or this opposite)
miststudent2011 avatar
fr flag
Related https://drupal.stackexchange.com/questions/294733/adding-constraint-to-entity-referenced-paragraph-field
Score:1
cn flag

OK, looking at the ERR implementations of $items->referencedEntities() and $item->entity it seems like the first function is able to handle new entities, but only the second computed field property is able to handle unsaved entities.

So try to loop over $items and then get the paragraph from the singe field item:

foreach ($items as $item) {
  $paragaph = $item->entity;
  $value = $paragraph->field_foo->value;
  // ...
}
Baud avatar
sa flag
you saved my day!!!! Many thanks!
4uk4 avatar
cn flag
Great find by the way! Didn't know this makes a difference. I cross-link it with the linked example.
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.