ValidEntityHierarchySectionValidator.php in Entity Reference Hierarchy 3.x
File
modules/entity_hierarchy_workbench_access/src/Plugin/Validation/Constraint/ValidEntityHierarchySectionValidator.php
View source
<?php
namespace Drupal\entity_hierarchy_workbench_access\Plugin\Validation\Constraint;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\ConstraintValidatorInterface;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\workbench_access\Entity\AccessSchemeInterface;
use Drupal\workbench_access\WorkbenchAccessManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Validator\Constraint;
class ValidEntityHierarchySectionValidator extends ConstraintValidator implements ConstraintValidatorInterface, ContainerInjectionInterface {
protected $configFactory;
protected $currentUser;
protected $workbenchManager;
protected $entityTypeManager;
public function __construct(ConfigFactoryInterface $configFactory, AccountInterface $currentUser, WorkbenchAccessManagerInterface $workbenchManager, EntityTypeManagerInterface $entityTypeManager) {
$this->configFactory = $configFactory;
$this->currentUser = $currentUser;
$this->workbenchManager = $workbenchManager;
$this->entityTypeManager = $entityTypeManager;
}
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'));
}
public function validate($items, Constraint $constraint) {
if ($this->currentUser
->hasPermission('bypass workbench access')) {
return;
}
$parent = $items->entity;
if (!$parent) {
if ($this->configFactory
->get('workbench_access.settings')
->get('deny_on_empty')) {
$this->context
->addViolation($constraint->message);
}
return;
}
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()) {
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);
}
}
}