content_moderation_permissions-1.0.0/src/Permissions.php
src/Permissions.php
<?php
namespace Drupal\content_moderation_permissions;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\workflows\Entity\Workflow;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Defines a class for dynamic permissions based on transitions.
*
* @internal
*/
class Permissions implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs a ContentTranslationPermissions instance.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('entity_type.manager')
);
}
/**
* Returns an array of transition permissions.
*
* @return array
* The transition permissions.
*/
public function transitionPermissions() {
$permissions = [];
/** @var \Drupal\workflows\WorkflowInterface $workflow */
foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
foreach ($workflow->getTypePlugin()->getTransitions() as $transition) {
foreach ($this->entityTypeManager
->getStorage('node_type')
->loadMultiple() as $content_type) {
// Check if moderation is enabled for this bundle on this workflow.
$workflow_moderation_enabled = $workflow->getTypePlugin()->appliesToEntityTypeAndBundle(
$content_type->getEntityType()->getBundleOf(),
$content_type->id()
);
if ($workflow_moderation_enabled) {
$permissions['use ' . $workflow->id() . ' transition ' . $transition->id() . ' for ' . $content_type->id()] = [
'title' => $this->t('%workflow workflow: Use %transition transition for %content_type.', [
'%workflow' => $workflow->label(),
'%transition' => $transition->label(),
'%content_type' => $content_type->label(),
]),
];
}
}
}
}
return $permissions;
}
}
