Chris@0: 'entity.manager']; Chris@18: Chris@18: /** Chris@18: * The entity type manager. Chris@0: * Chris@0: * @var \Drupal\Core\Entity\EntityManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@18: Chris@18: /** Chris@18: * The entity bundle info. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface Chris@18: */ Chris@18: protected $entityTypeBundleInfo; Chris@0: Chris@0: /** Chris@0: * The content translation manager. Chris@0: * Chris@0: * @var \Drupal\content_translation\ContentTranslationManagerInterface Chris@0: */ Chris@0: protected $contentTranslationManager; Chris@0: Chris@0: /** Chris@0: * Constructs a ContentTranslationPermissions instance. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@18: * The entity type manager. Chris@0: * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager Chris@0: * The content translation manager. Chris@18: * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info Chris@18: * The entity type bundle info. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentTranslationManagerInterface $content_translation_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info = NULL) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@0: $this->contentTranslationManager = $content_translation_manager; Chris@18: if (!$entity_type_bundle_info) { Chris@18: @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: $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info'); Chris@18: } Chris@18: $this->entityTypeBundleInfo = $entity_type_bundle_info; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public static function create(ContainerInterface $container) { Chris@0: return new static( Chris@18: $container->get('entity_type.manager'), Chris@18: $container->get('content_translation.manager'), Chris@18: $container->get('entity_type.bundle.info') Chris@0: ); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of content translation permissions. Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: public function contentPermissions() { Chris@0: $permission = []; Chris@0: // Create a translate permission for each enabled entity type and (optionally) Chris@0: // bundle. Chris@18: foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { Chris@0: if ($permission_granularity = $entity_type->getPermissionGranularity()) { Chris@0: $t_args = ['@entity_label' => $entity_type->getLowercaseLabel()]; Chris@0: Chris@0: switch ($permission_granularity) { Chris@0: case 'bundle': Chris@18: foreach ($this->entityTypeBundleInfo->getBundleInfo($entity_type_id) as $bundle => $bundle_info) { Chris@0: if ($this->contentTranslationManager->isEnabled($entity_type_id, $bundle)) { Chris@0: $t_args['%bundle_label'] = isset($bundle_info['label']) ? $bundle_info['label'] : $bundle; Chris@0: $permission["translate $bundle $entity_type_id"] = [ Chris@0: 'title' => $this->t('Translate %bundle_label @entity_label', $t_args), Chris@0: ]; Chris@0: } Chris@0: } Chris@0: break; Chris@0: Chris@0: case 'entity_type': Chris@0: if ($this->contentTranslationManager->isEnabled($entity_type_id)) { Chris@0: $permission["translate $entity_type_id"] = [ Chris@0: 'title' => $this->t('Translate @entity_label', $t_args), Chris@0: ]; Chris@0: } Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $permission; Chris@0: } Chris@0: Chris@0: }