class EntityReferenceSupportedNewEntitiesConstraintValidator

Same name in other branches
  1. 8.9.x core/modules/workspaces/src/Plugin/Validation/Constraint/EntityReferenceSupportedNewEntitiesConstraintValidator.php \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator
  2. 10 core/modules/workspaces/src/Plugin/Validation/Constraint/EntityReferenceSupportedNewEntitiesConstraintValidator.php \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator
  3. 11.x core/modules/workspaces/src/Plugin/Validation/Constraint/EntityReferenceSupportedNewEntitiesConstraintValidator.php \Drupal\workspaces\Plugin\Validation\Constraint\EntityReferenceSupportedNewEntitiesConstraintValidator

Checks if new entities created for entity reference fields are supported.

Hierarchy

Expanded class hierarchy of EntityReferenceSupportedNewEntitiesConstraintValidator

File

core/modules/workspaces/src/Plugin/Validation/Constraint/EntityReferenceSupportedNewEntitiesConstraintValidator.php, line 15

Namespace

Drupal\workspaces\Plugin\Validation\Constraint
View source
class EntityReferenceSupportedNewEntitiesConstraintValidator extends ConstraintValidator implements ContainerInjectionInterface {
    
    /**
     * The workspace manager.
     *
     * @var \Drupal\workspaces\WorkspaceManagerInterface
     */
    protected $workspaceManager;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * Creates a new EntityReferenceSupportedNewEntitiesConstraintValidator instance.
     */
    public function __construct(WorkspaceManagerInterface $workspaceManager, EntityTypeManagerInterface $entityTypeManager) {
        $this->workspaceManager = $workspaceManager;
        $this->entityTypeManager = $entityTypeManager;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('workspaces.manager'), $container->get('entity_type.manager'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function validate($value, Constraint $constraint) {
        // The validator should run only if we are in a active workspace context.
        if (!$this->workspaceManager
            ->hasActiveWorkspace()) {
            return;
        }
        $target_entity_type_id = $value->getFieldDefinition()
            ->getFieldStorageDefinition()
            ->getSetting('target_type');
        $target_entity_type = $this->entityTypeManager
            ->getDefinition($target_entity_type_id);
        if ($value->hasNewEntity() && !$this->workspaceManager
            ->isEntityTypeSupported($target_entity_type)) {
            $this->context
                ->addViolation($constraint->message, [
                '%collection_label' => $target_entity_type->getCollectionLabel(),
            ]);
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
EntityReferenceSupportedNewEntitiesConstraintValidator::$entityTypeManager protected property The entity type manager.
EntityReferenceSupportedNewEntitiesConstraintValidator::$workspaceManager protected property The workspace manager.
EntityReferenceSupportedNewEntitiesConstraintValidator::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
EntityReferenceSupportedNewEntitiesConstraintValidator::validate public function
EntityReferenceSupportedNewEntitiesConstraintValidator::__construct public function Creates a new EntityReferenceSupportedNewEntitiesConstraintValidator instance.

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