Backup of db before drupal security update
[yaffs-website] / web / core / modules / content_translation / src / ContentTranslationPermissions.php
1 <?php
2
3 namespace Drupal\content_translation;
4
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\StringTranslation\StringTranslationTrait;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides dynamic permissions for the content_translation module.
12  */
13 class ContentTranslationPermissions implements ContainerInjectionInterface {
14
15   use StringTranslationTrait;
16
17   /**
18    * The entity manager.
19    *
20    * @var \Drupal\Core\Entity\EntityManagerInterface
21    */
22   protected $entityManager;
23
24   /**
25    * The content translation manager.
26    *
27    * @var \Drupal\content_translation\ContentTranslationManagerInterface
28    */
29   protected $contentTranslationManager;
30
31   /**
32    * Constructs a ContentTranslationPermissions instance.
33    *
34    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
35    *   The entity manager.
36    * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
37    *   The content translation manager.
38    */
39   public function __construct(EntityManagerInterface $entity_manager, ContentTranslationManagerInterface $content_translation_manager) {
40     $this->entityManager = $entity_manager;
41     $this->contentTranslationManager = $content_translation_manager;
42   }
43
44   /**
45    * {@inheritdoc}
46    */
47   public static function create(ContainerInterface $container) {
48     return new static(
49       $container->get('entity.manager'),
50       $container->get('content_translation.manager')
51     );
52   }
53
54   /**
55    * Returns an array of content translation permissions.
56    *
57    * @return array
58    */
59   public function contentPermissions() {
60     $permission = [];
61     // Create a translate permission for each enabled entity type and (optionally)
62     // bundle.
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()];
66
67         switch ($permission_granularity) {
68           case 'bundle':
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),
74                 ];
75               }
76             }
77             break;
78
79           case 'entity_type':
80             if ($this->contentTranslationManager->isEnabled($entity_type_id)) {
81               $permission["translate $entity_type_id"] = [
82                 'title' => $this->t('Translate @entity_label', $t_args),
83               ];
84             }
85             break;
86         }
87       }
88     }
89
90     return $permission;
91   }
92
93 }