class EntityConstraintViolationList

Same name in other branches
  1. 9 core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList
  2. 10 core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList
  3. 11.x core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php \Drupal\Core\Entity\EntityConstraintViolationList

Implements an entity constraint violation list.

Hierarchy

Expanded class hierarchy of EntityConstraintViolationList

3 files declare their use of EntityConstraintViolationList
EntityConstraintViolationListTest.php in core/tests/Drupal/Tests/Core/Entity/EntityConstraintViolationListTest.php
EntityResourceValidationTraitTest.php in core/modules/rest/tests/src/Unit/EntityResourceValidationTraitTest.php
TemporaryJsonapiFileFieldUploader.php in core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php

File

core/lib/Drupal/Core/Entity/EntityConstraintViolationList.php, line 15

Namespace

Drupal\Core\Entity
View source
class EntityConstraintViolationList extends ConstraintViolationList implements EntityConstraintViolationListInterface {
    use StringTranslationTrait;
    
    /**
     * The entity that has been validated.
     *
     * @var \Drupal\Core\Entity\FieldableEntityInterface
     */
    protected $entity;
    
    /**
     * Violations offsets of entity level violations.
     *
     * @var int[]|null
     */
    protected $entityViolationOffsets;
    
    /**
     * Violation offsets grouped by field.
     *
     * Keys are field names, values are arrays of violation offsets.
     *
     * @var array[]|null
     */
    protected $violationOffsetsByField;
    
    /**
     * {@inheritdoc}
     *
     * @param \Drupal\Core\Entity\FieldableEntityInterface $entity
     *   The entity that has been validated.
     * @param array $violations
     *   The array of violations.
     */
    public function __construct(FieldableEntityInterface $entity, array $violations = []) {
        parent::__construct($violations);
        $this->entity = $entity;
    }
    
    /**
     * Groups violation offsets by field and entity level.
     *
     * Sets the $violationOffsetsByField and $entityViolationOffsets properties.
     */
    protected function groupViolationOffsets() {
        if (!isset($this->violationOffsetsByField)) {
            $this->violationOffsetsByField = [];
            $this->entityViolationOffsets = [];
            foreach ($this as $offset => $violation) {
                if ($path = $violation->getPropertyPath()) {
                    // An example of $path might be 'title.0.value'.
                    list($field_name) = explode('.', $path, 2);
                    if ($this->entity
                        ->hasField($field_name)) {
                        $this->violationOffsetsByField[$field_name][$offset] = $offset;
                    }
                    else {
                        $this->entityViolationOffsets[$offset] = $offset;
                    }
                }
                else {
                    $this->entityViolationOffsets[$offset] = $offset;
                }
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function getEntityViolations() {
        $this->groupViolationOffsets();
        $violations = [];
        foreach ($this->entityViolationOffsets as $offset) {
            $violations[] = $this->get($offset);
        }
        return new static($this->entity, $violations);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getByField($field_name) {
        return $this->getByFields([
            $field_name,
        ]);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getByFields(array $field_names) {
        $this->groupViolationOffsets();
        $violations = [];
        foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) {
            foreach ($offsets as $offset) {
                $violations[] = $this->get($offset);
            }
        }
        return new static($this->entity, $violations);
    }
    
    /**
     * {@inheritdoc}
     */
    public function filterByFields(array $field_names) {
        $this->groupViolationOffsets();
        $new_violations = [];
        foreach (array_intersect_key($this->violationOffsetsByField, array_flip($field_names)) as $field_name => $offsets) {
            foreach ($offsets as $offset) {
                $violation = $this->get($offset);
                // Take care of composite field violations and re-map them to some
                // covered field if necessary.
                if ($violation->getConstraint() instanceof CompositeConstraintBase) {
                    $covered_fields = $violation->getConstraint()
                        ->coversFields();
                    // Keep the composite field if it covers some remaining field and put
                    // a violation on some other covered field instead.
                    if ($remaining_fields = array_diff($covered_fields, $field_names)) {
                        $message_params = [
                            '%field_name' => $field_name,
                        ];
                        $violation = new ConstraintViolation($this->t('The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params), 'The validation failed because the value conflicts with the value in %field_name, which you cannot access.', $message_params, $violation->getRoot(), reset($remaining_fields), $violation->getInvalidValue(), $violation->getPlural(), $violation->getCode(), $violation->getConstraint(), $violation->getCause());
                        $new_violations[] = $violation;
                    }
                }
                $this->remove($offset);
            }
        }
        foreach ($new_violations as $violation) {
            $this->add($violation);
        }
        return $this;
    }
    
    /**
     * {@inheritdoc}
     */
    public function filterByFieldAccess(AccountInterface $account = NULL) {
        $filtered_fields = [];
        foreach ($this->getFieldNames() as $field_name) {
            if (!$this->entity
                ->get($field_name)
                ->access('edit', $account)) {
                $filtered_fields[] = $field_name;
            }
        }
        return $this->filterByFields($filtered_fields);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getFieldNames() {
        $this->groupViolationOffsets();
        return array_keys($this->violationOffsetsByField);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getEntity() {
        return $this->entity;
    }
    
    /**
     * {@inheritdoc}
     */
    public function add(ConstraintViolationInterface $violation) {
        parent::add($violation);
        $this->violationOffsetsByField = NULL;
        $this->entityViolationOffsets = NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function remove($offset) {
        parent::remove($offset);
        $this->violationOffsetsByField = NULL;
        $this->entityViolationOffsets = NULL;
    }
    
    /**
     * {@inheritdoc}
     */
    public function set($offset, ConstraintViolationInterface $violation) {
        parent::set($offset, $violation);
        $this->violationOffsetsByField = NULL;
        $this->entityViolationOffsets = NULL;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
EntityConstraintViolationList::$entity protected property The entity that has been validated.
EntityConstraintViolationList::$entityViolationOffsets protected property Violations offsets of entity level violations.
EntityConstraintViolationList::$violationOffsetsByField protected property Violation offsets grouped by field.
EntityConstraintViolationList::add public function
EntityConstraintViolationList::filterByFieldAccess public function Filters this violation list to apply for accessible fields only. Overrides EntityConstraintViolationListInterface::filterByFieldAccess
EntityConstraintViolationList::filterByFields public function Filters this violation list by the given fields. Overrides EntityConstraintViolationListInterface::filterByFields
EntityConstraintViolationList::getByField public function Gets the violations of the given field. Overrides EntityConstraintViolationListInterface::getByField
EntityConstraintViolationList::getByFields public function Gets the violations of the given fields. Overrides EntityConstraintViolationListInterface::getByFields
EntityConstraintViolationList::getEntity public function The entity which has been validated. Overrides EntityConstraintViolationListInterface::getEntity
EntityConstraintViolationList::getEntityViolations public function Gets violations flagged on entity level, not associated with any field. Overrides EntityConstraintViolationListInterface::getEntityViolations
EntityConstraintViolationList::getFieldNames public function Returns the names of all violated fields. Overrides EntityConstraintViolationListInterface::getFieldNames
EntityConstraintViolationList::groupViolationOffsets protected function Groups violation offsets by field and entity level.
EntityConstraintViolationList::remove public function
EntityConstraintViolationList::set public function
EntityConstraintViolationList::__construct public function
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.