WorkflowAccessControlHandler.php in Drupal 10
File
core/modules/workflows/src/WorkflowAccessControlHandler.php
View source
<?php
namespace Drupal\workflows;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WorkflowAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $workflowTypeManager;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('plugin.manager.workflows.type'));
}
public function __construct(EntityTypeInterface $entity_type, PluginManagerInterface $workflow_type_manager) {
parent::__construct($entity_type);
$this->workflowTypeManager = $workflow_type_manager;
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$workflow_type = $entity
->getTypePlugin();
if (strpos($operation, 'delete-state') === 0) {
[
,
$state_id,
] = explode(':', $operation, 2);
return AccessResult::allowedIf(count($entity
->getTypePlugin()
->getStates()) > 1)
->andIf(parent::checkAccess($entity, 'edit', $account))
->andIf(AccessResult::allowedIf(!in_array($state_id, $workflow_type
->getRequiredStates(), TRUE)))
->addCacheableDependency($entity);
}
return parent::checkAccess($entity, $operation, $account);
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
$workflow_types_count = count($this->workflowTypeManager
->getDefinitions());
$admin_access = parent::checkCreateAccess($account, $context, $entity_bundle);
return $admin_access
->andIf(AccessResult::allowedIf($workflow_types_count > 0))
->addCacheTags([
'workflow_type_plugins',
]);
}
}