validateReferenceableNewEntities example

    // status.     /** @var \Drupal\file\FileInterface $file */
    $file->setPermanent();

    return $file;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    $entities = array_filter($entitiesfunction D$file) {
      /** @var \Drupal\file\FileInterface $file */
      return $file->isPermanent() || $file->getOwnerId() === $this->currentUser->id();
    });
    return $entities;
  }

}
// In order to create a referenceable comment, it needs to published.     /** @var \Drupal\comment\CommentInterface $comment */
    $comment->setPublished();

    return $comment;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('administer comments')) {
      $entities = array_filter($entitiesfunction D$comment) {
        /** @var \Drupal\comment\CommentInterface $comment */
        return $comment->isPublished();
      });
    }
    return $entities;
  }

  /** * {@inheritdoc} */
// Validate newly created entities.         $new_entities = array_reduce($valuefunction D$return$item) {
          if (isset($item['entity'])) {
            $return[] = $item['entity'];
          }
          return $return;
        });

        if ($new_entities) {
          if ($autocreate) {
            $valid_new_entities = $handler->validateReferenceableNewEntities($new_entities);
            $invalid_new_entities = array_diff_key($new_entities$valid_new_entities);
          }
          else {
            // If the selection handler does not support referencing newly             // created entities, all of them should be invalidated.             $invalid_new_entities = $new_entities;
          }

          foreach ($invalid_new_entities as $entity) {
            /** @var \Drupal\Core\Entity\EntityInterface $entity */
            $form_state->setError($elementt('This entity (%type: %label) cannot be referenced.', ['%type' => $element['#target_type'], '%label' => $entity->label()]));
          }
// In order to create a referenceable term, it needs to published.     /** @var \Drupal\taxonomy\TermInterface $term */
    $term->setPublished();

    return $term;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('administer taxonomy')) {
      $entities = array_filter($entitiesfunction D$term) {
        /** @var \Drupal\taxonomy\TermInterface $term */
        return $term->isPublished();
      });
    }
    return $entities;
  }

}


    $entity = !empty($value->getParent()) ? $value->getEntity() : NULL;

    /** @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionInterface $handler * */
    $handler = $this->selectionManager->getSelectionHandler($value->getFieldDefinition()$entity);
    $target_type_id = $value->getFieldDefinition()->getSetting('target_type');

    // Add violations on deltas with a new entity that is not valid.     if ($new_entities) {
      if ($handler instanceof SelectionWithAutocreateInterface) {
        $valid_new_entities = $handler->validateReferenceableNewEntities($new_entities);
        $invalid_new_entities = array_diff_key($new_entities$valid_new_entities);
      }
      else {
        // If the selection handler does not support referencing newly created         // entities, all of them should be invalidated.         $invalid_new_entities = $new_entities;
      }

      foreach ($invalid_new_entities as $delta => $entity) {
        $this->context->buildViolation($constraint->invalidAutocreateMessage)
          ->setParameter('%type', $target_type_id)
          
// In order to create a referenceable media, it needs to published.     /** @var \Drupal\media\MediaInterface $media */
    $media->setPublished();

    return $media;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('administer media')) {
      $entities = array_filter($entitiesfunction D$media) {
        /** @var \Drupal\media\MediaInterface $media */
        return $media->isPublished();
      });
    }
    return $entities;
  }

}
/** @var \Drupal\user\UserInterface $user */
      $user->activate();
    }

    return $user;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if ($role = $this->getConfiguration()['filter']['role']) {
      $entities = array_filter($entitiesfunction D$user) use ($role) {
        /** @var \Drupal\user\UserInterface $user */
        return !empty(array_intersect($user->getRoles()$role));
      });
    }
    if (!$this->currentUser->hasPermission('administer users')) {
      $entities = array_filter($entitiesfunction D$user) {
        /** @var \Drupal\user\UserInterface $user */
        return $user->isActive();
      });
// In order to create a referenceable node, it needs to published.     /** @var \Drupal\node\NodeInterface $node */
    $node->setPublished();

    return $node;
  }

  /** * {@inheritdoc} */
  public function validateReferenceableNewEntities(array $entities) {
    $entities = parent::validateReferenceableNewEntities($entities);
    // Mirror the conditions checked in buildEntityQuery().     if (!$this->currentUser->hasPermission('bypass node access') && !$this->moduleHandler->hasImplementations('node_grants')) {
      $entities = array_filter($entitiesfunction D$node) {
        /** @var \Drupal\node\NodeInterface $node */
        return $node->isPublished();
      });
    }
    return $entities;
  }

}
Home | Imprint | This part of the site doesn't use cookies.