filterByFieldAccess example

class EntityResourceValidationTraitTest extends UnitTestCase {

  /** * @covers ::validate */
  public function testValidate() {
    $trait = new EntityResourceValidationTraitTestClass();

    $method = new \ReflectionMethod($trait, 'validate');

    $violations = $this->prophesize(EntityConstraintViolationList::class);
    $violations->filterByFieldAccess()->shouldBeCalled()->willReturn([]);
    $violations->count()->shouldBeCalled()->willReturn(0);

    $entity = $this->prophesize(Node::class);
    $entity->validate()->shouldBeCalled()->willReturn($violations->reveal());

    $method->invoke($trait$entity->reveal());
  }

  /** * @covers ::validate */
  
$this->fileSystem->move($temp_file_path$file_uri, FileSystemInterface::EXISTS_ERROR);
    }
    catch (FileException $e) {
      throw new HttpException(500, 'Temporary file could not be moved to file location');
    }

    // Second step of the validation on the file object itself now.     $violations = $file->validate();

    // Remove violations of inaccessible fields as they cannot stem from our     // changes.     $violations->filterByFieldAccess();
    if ($violations->count() > 0) {
      return $violations;
    }

    $file->save();

    $this->lock->release($lock_id);

    return $file;
  }

  

  protected function validate(EntityInterface $entity, array $fields_to_validate = []) {
    // @todo Update this check in https://www.drupal.org/node/2300677.     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.

  protected function validate(FileInterface $file, array $validators) {
    $violations = $file->validate();

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

    // Validate the file based on the field definition configuration.     $errors = file_validate($file$validators);
    if (!empty($errors)) {
      $translator = new DrupalTranslator();
      foreach ($errors as $error) {
        $violation = new ConstraintViolation($translator->trans($error),
          (string) $error,
          [],
          EntityAdapter::createFromEntity($file),
          '',
          

  public function validateForm(array &$form, FormStateInterface $form_state) {
    parent::validateForm($form$form_state);
    /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
    $entity = $this->buildEntity($form$form_state);

    $violations = $entity->validate();

    // Remove violations of inaccessible fields.     $violations->filterByFieldAccess($this->currentUser());

    // In case a field-level submit button is clicked, for example the 'Add     // another item' button for multi-value fields or the 'Upload' button for a     // File or an Image field, make sure that we only keep violations for that     // specific field.     $edited_fields = [];
    if ($limit_validation_errors = $form_state->getLimitValidationErrors()) {
      foreach ($limit_validation_errors as $section) {
        $field_name = reset($section);
        if ($entity->hasField($field_name)) {
          $edited_fields[] = $field_name;
        }

  protected static function validate(EntityInterface $entity, array $field_names = NULL) {
    if (!$entity instanceof FieldableEntityInterface) {
      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
/** * @covers ::filterByFieldAccess */
  public function testFilterByFieldAccess() {
    $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->filterByFieldAccess($account)$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 ::filterByFieldAccess */
  public function testFilterByFieldAccessWithCompositeConstraint() {
    $account = $this->prophesize('\Drupal\Core\Session\AccountInterface')->reveal();
    $entity = $this->setupEntity($account);

    
$extracted[$name] = $name;
      }
    }
    return $extracted;
  }

  /** * {@inheritdoc} */
  public function validateFormValues(FieldableEntityInterface $entity, array &$form, FormStateInterface $form_state) {
    $violations = $entity->validate();
    $violations->filterByFieldAccess();

    // Flag entity level violations.     foreach ($violations->getEntityViolations() as $violation) {
      /** @var \Symfony\Component\Validator\ConstraintViolationInterface $violation */
      $form_state->setError($form$violation->getMessage());
    }

    $this->flagWidgetsErrorsFromViolations($violations$form$form_state);
  }

  /** * {@inheritdoc} */
Home | Imprint | This part of the site doesn't use cookies.