public function ParagraphsLibraryItemHasAllowedParagraphsTypeConstraintValidator::validate

File

paragraphs/modules/paragraphs_library/src/Plugin/Validation/Constraint/ParagraphsLibraryItemHasAllowedParagraphsTypeConstraintValidator.php, line 66

Class

ParagraphsLibraryItemHasAllowedParagraphsTypeConstraintValidator
Validates that the library does not bypass Paragraphs type restrictions.

Namespace

Drupal\paragraphs_library\Plugin\Validation\Constraint

Code

public function validate($value, Constraint $constraint) {
  if (!$value instanceof EntityReferenceFieldItemListInterface) {
    throw new UnexpectedTypeException($value, EntityReferenceFieldItemListInterface::class);
  }
  $handler = $this->selectionManager
    ->getSelectionHandler($value
    ->getFieldDefinition());
  if (!$handler instanceof ParagraphSelection) {
    return;
  }
  $allowed_paragraphs_types = array_keys($handler
    ->getSortedAllowedTypes());

  // Look for Paragraphs with library item fields in the restricted field.
  foreach ($value
    ->referencedEntities() as $paragraph) {
    if (!$paragraph instanceof ParagraphInterface) {
      continue;
    }

    // Look for library item fields.
    foreach ($paragraph
      ->getFieldDefinitions() as $field_name => $field_definition) {
      if ($field_definition
        ->getType() !== 'entity_reference') {
        continue;
      }
      if ($field_definition
        ->getSetting('target_type') !== 'paragraphs_library_item') {
        continue;
      }

      /** @var \Drupal\paragraphs_library\LibraryItemInterface $library_item_entity */
      foreach ($paragraph
        ->get($field_name) as $entity_reference_item) {

        // Get the Paragraphs type of the library item.
        if ($library_item_entity = $entity_reference_item->entity) {
          if ($used_paragraphs = $library_item_entity
            ->get('paragraphs')->entity) {
            $used_paragraphs_type = $used_paragraphs
              ->getType();

            // Check if the Paragraphs type of the item is not allowed in the
            // field holding the parent Paragraph.
            if (in_array($used_paragraphs_type, $allowed_paragraphs_types)) {
              continue;
            }
            $paragraphs_type_entity = $this->entityTypeManager
              ->getStorage('paragraphs_type')
              ->load($used_paragraphs_type);
            $this->context
              ->addViolation($constraint->message, [
              '@library_item_field_label' => $field_definition
                ->getLabel(),
              '@paragraphs_type_label' => $paragraphs_type_entity
                ->label(),
              '@paragraph_field_label' => $value
                ->getFieldDefinition()
                ->getLabel(),
            ]);
          }
        }
      }
    }
  }
}