Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\content_translation;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@18
|
6 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
|
Chris@18
|
7 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
|
Chris@0
|
10 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Provides dynamic permissions for the content_translation module.
|
Chris@0
|
14 */
|
Chris@0
|
15 class ContentTranslationPermissions implements ContainerInjectionInterface {
|
Chris@0
|
16
|
Chris@0
|
17 use StringTranslationTrait;
|
Chris@18
|
18 use DeprecatedServicePropertyTrait;
|
Chris@0
|
19
|
Chris@0
|
20 /**
|
Chris@18
|
21 * {@inheritdoc}
|
Chris@18
|
22 */
|
Chris@18
|
23 protected $deprecatedProperties = ['entityManager' => 'entity.manager'];
|
Chris@18
|
24
|
Chris@18
|
25 /**
|
Chris@18
|
26 * The entity type manager.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var \Drupal\Core\Entity\EntityManagerInterface
|
Chris@0
|
29 */
|
Chris@18
|
30 protected $entityTypeManager;
|
Chris@18
|
31
|
Chris@18
|
32 /**
|
Chris@18
|
33 * The entity bundle info.
|
Chris@18
|
34 *
|
Chris@18
|
35 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
|
Chris@18
|
36 */
|
Chris@18
|
37 protected $entityTypeBundleInfo;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * The content translation manager.
|
Chris@0
|
41 *
|
Chris@0
|
42 * @var \Drupal\content_translation\ContentTranslationManagerInterface
|
Chris@0
|
43 */
|
Chris@0
|
44 protected $contentTranslationManager;
|
Chris@0
|
45
|
Chris@0
|
46 /**
|
Chris@0
|
47 * Constructs a ContentTranslationPermissions instance.
|
Chris@0
|
48 *
|
Chris@18
|
49 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
50 * The entity type manager.
|
Chris@0
|
51 * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager
|
Chris@0
|
52 * The content translation manager.
|
Chris@18
|
53 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
|
Chris@18
|
54 * The entity type bundle info.
|
Chris@0
|
55 */
|
Chris@18
|
56 public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentTranslationManagerInterface $content_translation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) {
|
Chris@18
|
57 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
58 $this->contentTranslationManager = $content_translation_manager;
|
Chris@18
|
59 if (!$entity_type_bundle_info) {
|
Chris@18
|
60 @trigger_error('Calling ContentTranslationPermissions::__construct() with the $entity_type_bundle_info argument is supported in drupal:8.7.0 and will be required before drupal:9.0.0. See https://www.drupal.org/node/2549139.', E_USER_DEPRECATED);
|
Chris@18
|
61 $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
|
Chris@18
|
62 }
|
Chris@18
|
63 $this->entityTypeBundleInfo = $entity_type_bundle_info;
|
Chris@0
|
64 }
|
Chris@0
|
65
|
Chris@0
|
66 /**
|
Chris@0
|
67 * {@inheritdoc}
|
Chris@0
|
68 */
|
Chris@0
|
69 public static function create(ContainerInterface $container) {
|
Chris@0
|
70 return new static(
|
Chris@18
|
71 $container->get('entity_type.manager'),
|
Chris@18
|
72 $container->get('content_translation.manager'),
|
Chris@18
|
73 $container->get('entity_type.bundle.info')
|
Chris@0
|
74 );
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Returns an array of content translation permissions.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @return array
|
Chris@0
|
81 */
|
Chris@0
|
82 public function contentPermissions() {
|
Chris@0
|
83 $permission = [];
|
Chris@0
|
84 // Create a translate permission for each enabled entity type and (optionally)
|
Chris@0
|
85 // bundle.
|
Chris@18
|
86 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
|
Chris@0
|
87 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
|
Chris@0
|
88 $t_args = ['@entity_label' => $entity_type->getLowercaseLabel()];
|
Chris@0
|
89
|
Chris@0
|
90 switch ($permission_granularity) {
|
Chris@0
|
91 case 'bundle':
|
Chris@18
|
92 foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
|
Chris@0
|
93 if ($this->contentTranslationManager->isEnabled($entity_type_id, $bundle)) {
|
Chris@0
|
94 $t_args['%bundle_label'] = isset($bundle_info['label']) ? $bundle_info['label'] : $bundle;
|
Chris@0
|
95 $permission["translate $bundle $entity_type_id"] = [
|
Chris@0
|
96 'title' => $this->t('Translate %bundle_label @entity_label', $t_args),
|
Chris@0
|
97 ];
|
Chris@0
|
98 }
|
Chris@0
|
99 }
|
Chris@0
|
100 break;
|
Chris@0
|
101
|
Chris@0
|
102 case 'entity_type':
|
Chris@0
|
103 if ($this->contentTranslationManager->isEnabled($entity_type_id)) {
|
Chris@0
|
104 $permission["translate $entity_type_id"] = [
|
Chris@0
|
105 'title' => $this->t('Translate @entity_label', $t_args),
|
Chris@0
|
106 ];
|
Chris@0
|
107 }
|
Chris@0
|
108 break;
|
Chris@0
|
109 }
|
Chris@0
|
110 }
|
Chris@0
|
111 }
|
Chris@0
|
112
|
Chris@0
|
113 return $permission;
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 }
|