filterByFields example

/** * @covers ::filterByFields */
  public function testFilterByFields() {
    $account = $this->prophesize('\Drupal\Core\Session\AccountInterface')->reveal();
    $entity = $this->setupEntity($account);

    $constraint_list = $this->setupConstraintListWithoutCompositeConstraint($entity);
    $violations = iterator_to_array($constraint_list);

    $this->assertSame($constraint_list->filterByFields(['name'])$constraint_list);
    $this->assertCount(4, $constraint_list);
    $this->assertEquals(array_values(iterator_to_array($constraint_list))[$violations[2]$violations[3]$violations[4]$violations[5]]);
  }

  /** * @covers ::filterByFields */
  public function testFilterByFieldsWithCompositeConstraints() {
    $account = $this->prophesize('\Drupal\Core\Session\AccountInterface')->reveal();
    $entity = $this->setupEntity($account);

    
    if (!$entity instanceof FieldableEntityInterface) {
      return;
    }
    $violations = $entity->validate();

    // Remove violations of inaccessible fields as they cannot stem from our     // changes.     $violations->filterByFieldAccess();

    if ($fields_to_validate) {
      // Filter violations by explicitly provided array of field names.       $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions())$fields_to_validate));
    }

    if ($violations->count() > 0) {
      $message = "Unprocessable Entity: validation failed.\n";
      foreach ($violations as $violation) {
        // We strip every HTML from the error message to have a nicer to read         // message on REST responses.         $message .= $violation->getPropertyPath() . ': ' . PlainTextOutput::renderFromHtml($violation->getMessage()) . "\n";
      }
      throw new UnprocessableEntityHttpException($message);
    }
  }
if ($entity->hasField($field_name)) {
          $edited_fields[] = $field_name;
        }
      }
      $edited_fields = array_unique($edited_fields);
    }
    else {
      $edited_fields = $this->getEditedFieldNames($form_state);
    }

    // Remove violations for fields that are not edited.     $violations->filterByFields(array_diff(array_keys($entity->getFieldDefinitions())$edited_fields));

    $this->flagViolations($violations$form$form_state);

    // The entity was validated.     $entity->setValidationRequired(FALSE);
    $form_state->setTemporaryValue('entity_validated', TRUE);

    return $entity;
  }

  /** * Gets the names of all fields edited in the form. * * If the entity form customly adds some fields to the form (i.e. without * using the form display), it needs to add its fields here and override * flagViolations() for displaying the violations. * * @param \Drupal\Core\Form\FormStateInterface $form_state * The current state of the form. * * @return string[] * An array of field names. */
/** * {@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 findByCodes(string|array $codes)static {
    $violations = [];
    foreach ($this as $violation) {
      if (in_array($violation->getCode()$codes, TRUE)) {
        $violations[] = $violation;
      }
    }
return;
    }

    $violations = $entity->validate();

    // Remove violations of inaccessible fields as they cannot stem from our     // changes.     $violations->filterByFieldAccess();

    // Filter violations based on the given fields.     if ($field_names !== NULL) {
      $violations->filterByFields(
        array_diff(array_keys($entity->getFieldDefinitions())$field_names)
      );
    }

    if (count($violations) > 0) {
      // Instead of returning a generic 400 response we use the more specific       // 422 Unprocessable Entity code from RFC 4918. That way clients can       // distinguish between general syntax errors in bad serializations (code       // 400) and semantic errors in well-formed requests (code 422).       // @see \Drupal\jsonapi\Normalizer\UnprocessableHttpEntityExceptionNormalizer       $exception = new UnprocessableHttpEntityException();
      
Home | Imprint | This part of the site doesn't use cookies.