View source
<?php
declare (strict_types=1);
namespace Drupal\scheduled_transitions;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ScheduledTransitionsPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
public const ENTITY_OPERATION_VIEW_TRANSITIONS = 'view scheduled transition';
public const ENTITY_OPERATION_ADD_TRANSITION = 'add scheduled transition';
public const ENTITY_OPERATION_RESCHEDULE_TRANSITIONS = 'reschedule scheduled transitions';
protected $entityTypeManager;
protected $bundleInfo;
protected $scheduledTransitionsUtility;
public function __construct(EntityTypeManagerInterface $entityTypeManager, EntityTypeBundleInfoInterface $bundleInfo, TranslationInterface $stringTranslation, ScheduledTransitionsUtilityInterface $scheduledTransitionsUtility) {
$this->entityTypeManager = $entityTypeManager;
$this->bundleInfo = $bundleInfo;
$this->stringTranslation = $stringTranslation;
$this->scheduledTransitionsUtility = $scheduledTransitionsUtility;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('entity_type.bundle.info'), $container
->get('string_translation'), $container
->get('scheduled_transitions.utility'));
}
public function permissions() : array {
$permissions = [];
$bundleInfo = $this->scheduledTransitionsUtility
->getBundles();
foreach ($bundleInfo as $entityTypeId => $bundles) {
$entityBundleInfo = $this->bundleInfo
->getBundleInfo($entityTypeId);
foreach ($bundles as $bundleId) {
$tArgs = [
'@entity_type' => $this->entityTypeManager
->getDefinition($entityTypeId)
->getLabel(),
'@bundle' => $entityBundleInfo[$bundleId]['label'] ?? '',
];
$viewPermission = static::viewScheduledTransitionsPermission($entityTypeId, $bundleId);
$permissions[$viewPermission] = [
'title' => $this
->t('View scheduled transitions for @entity_type:@bundle entities', $tArgs),
];
$addPermission = static::addScheduledTransitionsPermission($entityTypeId, $bundleId);
$permissions[$addPermission] = [
'title' => $this
->t('Add scheduled transitions for @entity_type:@bundle entities', $tArgs),
];
$reschedulePermission = static::rescheduleScheduledTransitionsPermission($entityTypeId, $bundleId);
$permissions[$reschedulePermission] = [
'title' => $this
->t('Reschedule scheduled transitions for @entity_type:@bundle entities', $tArgs),
];
}
}
return $permissions;
}
public static function viewScheduledTransitionsPermission(string $entityTypeId, string $bundle) : string {
return sprintf('view scheduled transitions %s %s', $entityTypeId, $bundle);
}
public static function addScheduledTransitionsPermission(string $entityTypeId, string $bundle) : string {
return sprintf('add scheduled transitions %s %s', $entityTypeId, $bundle);
}
public static function rescheduleScheduledTransitionsPermission(string $entityTypeId, string $bundle) : string {
return sprintf('reschedule scheduled transitions %s %s', $entityTypeId, $bundle);
}
}