class ValidEntityHierarchySectionValidator in Entity Reference Hierarchy 3.x
Same name and namespace in other branches
- 8.2 modules/entity_hierarchy_workbench_access/src/Plugin/Validation/Constraint/ValidEntityHierarchySectionValidator.php \Drupal\entity_hierarchy_workbench_access\Plugin\Validation\Constraint\ValidEntityHierarchySectionValidator
Defines a class for validating entity hierarchy selection with access.
Hierarchy
- class \Drupal\entity_hierarchy_workbench_access\Plugin\Validation\Constraint\ValidEntityHierarchySectionValidator extends \Symfony\Component\Validator\ConstraintValidator implements \Symfony\Component\Validator\ConstraintValidatorInterface, ContainerInjectionInterface
Expanded class hierarchy of ValidEntityHierarchySectionValidator
File
- modules/
entity_hierarchy_workbench_access/ src/ Plugin/ Validation/ Constraint/ ValidEntityHierarchySectionValidator.php, line 20
Namespace
Drupal\entity_hierarchy_workbench_access\Plugin\Validation\ConstraintView source
class ValidEntityHierarchySectionValidator extends ConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {
/**
* Config factory.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* Plugin manager.
*
* @var \Drupal\workbench_access\WorkbenchAccessManagerInterface
*/
protected $workbenchManager;
/**
* Entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a new ValidEntityHierarchySection.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory.
* @param \Drupal\Core\Session\AccountInterface $currentUser
* Current user.
* @param \Drupal\workbench_access\WorkbenchAccessManagerInterface $workbenchManager
* Workbench manager.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager.
*/
public function __construct(ConfigFactoryInterface $configFactory, AccountInterface $currentUser, WorkbenchAccessManagerInterface $workbenchManager, EntityTypeManagerInterface $entityTypeManager) {
$this->configFactory = $configFactory;
$this->currentUser = $currentUser;
$this->workbenchManager = $workbenchManager;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container
->get('config.factory'), $container
->get('current_user'), $container
->get('plugin.manager.workbench_access.scheme'), $container
->get('entity_type.manager'));
}
/**
* {@inheritdoc}
*/
public function validate($items, Constraint $constraint) {
/** @var \Drupal\Core\Field\FieldItemListInterface $items */
if ($this->currentUser
->hasPermission('bypass workbench access')) {
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $parent */
$parent = $items->entity;
if (!$parent) {
if ($this->configFactory
->get('workbench_access.settings')
->get('deny_on_empty')) {
$this->context
->addViolation($constraint->message);
}
return;
}
/** @var \Drupal\Core\Entity\ContentEntityInterface $saved */
if ($items
->getEntity()
->id() && ($saved = $this->entityTypeManager
->getStorage($parent
->getEntityTypeId())
->loadUnchanged($items
->getEntity()
->id()))) {
$field_name = $items
->getFieldDefinition()
->getFieldStorageDefinition()
->getName();
if ($saved
->hasField($field_name) && !$saved
->get($field_name)
->isEmpty() && $saved->{$field_name}->entity && $saved->{$field_name}->entity
->id() === $parent
->id()) {
// The user is not changing the field value.
return;
}
}
$result = array_reduce($this->entityTypeManager
->getStorage('access_scheme')
->loadMultiple(), function (AccessResult $carry, AccessSchemeInterface $scheme) use ($parent) {
$carry
->addCacheableDependency($scheme)
->cachePerPermissions()
->addCacheableDependency($parent);
return $carry
->orIf($scheme
->getAccessScheme()
->checkEntityAccess($scheme, $parent, 'update', $this->currentUser));
}, AccessResult::neutral());
if ($result
->isForbidden()) {
$this->context
->addViolation($constraint->message);
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ValidEntityHierarchySectionValidator:: |
protected | property | Config factory. | |
ValidEntityHierarchySectionValidator:: |
protected | property | Current user. | |
ValidEntityHierarchySectionValidator:: |
protected | property | Entity type manager. | |
ValidEntityHierarchySectionValidator:: |
protected | property | Plugin manager. | |
ValidEntityHierarchySectionValidator:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
|
ValidEntityHierarchySectionValidator:: |
public | function | ||
ValidEntityHierarchySectionValidator:: |
public | function | Constructs a new ValidEntityHierarchySection. |