ContentTranslationPermissions.php in Drupal 10
File
core/modules/content_translation/src/ContentTranslationPermissions.php
View source
<?php
namespace Drupal\content_translation;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ContentTranslationPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
protected $entityTypeManager;
protected $entityTypeBundleInfo;
protected $contentTranslationManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentTranslationManagerInterface $content_translation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info) {
$this->entityTypeManager = $entity_type_manager;
$this->contentTranslationManager = $content_translation_manager;
$this->entityTypeBundleInfo = $entity_type_bundle_info;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity_type.manager'), $container
->get('content_translation.manager'), $container
->get('entity_type.bundle.info'));
}
public function contentPermissions() {
$permissions = [];
foreach ($this->entityTypeManager
->getDefinitions() as $entity_type_id => $entity_type) {
if ($permission_granularity = $entity_type
->getPermissionGranularity()) {
switch ($permission_granularity) {
case 'bundle':
foreach ($this->entityTypeBundleInfo
->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
if ($this->contentTranslationManager
->isEnabled($entity_type_id, $bundle)) {
$permissions["translate {$bundle} {$entity_type_id}"] = $this
->buildBundlePermission($entity_type, $bundle, $bundle_info);
}
}
break;
case 'entity_type':
if ($this->contentTranslationManager
->isEnabled($entity_type_id)) {
$permissions["translate {$entity_type_id}"] = [
'title' => $this
->t('Translate @entity_label', [
'@entity_label' => $entity_type
->getSingularLabel(),
]),
'dependencies' => [
'module' => [
$entity_type
->getProvider(),
],
],
];
}
break;
}
}
}
return $permissions;
}
private function buildBundlePermission(EntityTypeInterface $entity_type, string $bundle, array $bundle_info) {
$permission = [
'title' => $this
->t('Translate %bundle_label @entity_label', [
'@entity_label' => $entity_type
->getSingularLabel(),
'%bundle_label' => $bundle_info['label'] ?? $bundle,
]),
];
$bundle_entity_type = $entity_type
->getBundleEntityType();
if ($bundle_entity_type && ($bundle_entity = $this->entityTypeManager
->getStorage($bundle_entity_type)
->load($bundle))) {
$permission['dependencies'][$bundle_entity
->getConfigDependencyKey()][] = $bundle_entity
->getConfigDependencyName();
}
else {
$permission['dependencies']['module'][] = $entity_type
->getProvider();
}
return $permission;
}
}