3 namespace Drupal\content_translation;
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
11 * Provides dynamic permissions for the content_translation module.
13 class ContentTranslationPermissions implements ContainerInjectionInterface {
15 use StringTranslationTrait;
20 * @var \Drupal\Core\Entity\EntityManagerInterface
22 protected $entityManager;
25 * The content translation manager.
27 * @var \Drupal\content_translation\ContentTranslationManagerInterface
29 protected $contentTranslationManager;
32 * Constructs a ContentTranslationPermissions instance.
34 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
36 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
37 * The content translation manager.
39 public function __construct(EntityManagerInterface $entity_manager, ContentTranslationManagerInterface $content_translation_manager) {
40 $this->entityManager = $entity_manager;
41 $this->contentTranslationManager = $content_translation_manager;
47 public static function create(ContainerInterface $container) {
49 $container->get('entity.manager'),
50 $container->get('content_translation.manager')
55 * Returns an array of content translation permissions.
59 public function contentPermissions() {
61 // Create a translate permission for each enabled entity type and (optionally)
63 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
64 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
65 $t_args = ['@entity_label' => $entity_type->getLowercaseLabel()];
67 switch ($permission_granularity) {
69 foreach ($this->entityManager->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
70 if ($this->contentTranslationManager->isEnabled($entity_type_id, $bundle)) {
71 $t_args['%bundle_label'] = isset($bundle_info['label']) ? $bundle_info['label'] : $bundle;
72 $permission["translate $bundle $entity_type_id"] = [
73 'title' => $this->t('Translate %bundle_label @entity_label', $t_args),
80 if ($this->contentTranslationManager->isEnabled($entity_type_id)) {
81 $permission["translate $entity_type_id"] = [
82 'title' => $this->t('Translate @entity_label', $t_args),