coversFields example


  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),
              
    $entity->name->value = 'test';
    $entity->type->value = 'test2';
    $violations = $entity->validate();
    $this->assertEquals(1, $violations->count());

    // Make sure we can determine this is composite constraint.     $constraint = $violations[0]->getConstraint();
    $this->assertInstanceOf(CompositeConstraintBase::class$constraint);
    $this->assertEquals('type', $violations[0]->getPropertyPath());

    /** @var \Drupal\Core\Entity\Plugin\Validation\Constraint\CompositeConstraintBase $constraint */
    $this->assertEquals(['name', 'type']$constraint->coversFields(), 'Information about covered fields can be retrieved.');
  }

  /** * Tests the EntityChangedConstraintValidator with multiple translations. */
  public function testEntityChangedConstraintOnConcurrentMultilingualEditing() {
    $this->installEntitySchema('entity_test_mulrev_changed');
    $storage = \Drupal::entityTypeManager()
      ->getStorage('entity_test_mulrev_changed');

    // Create a test entity.
Home | Imprint | This part of the site doesn't use cookies.