3 namespace Drupal\content_translation;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
9 * Provides common functionality for content translation.
11 class ContentTranslationManager implements ContentTranslationManagerInterface {
14 * The entity type manager.
16 * @var \Drupal\Core\Entity\EntityManagerInterface
18 protected $entityManager;
21 * The updates manager.
23 * @var \Drupal\content_translation\ContentTranslationUpdatesManager
25 protected $updatesManager;
28 * Constructs a ContentTranslationManageAccessCheck object.
30 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
31 * The entity type manager.
32 * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
33 * The updates manager.
35 public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
36 $this->entityManager = $manager;
37 $this->updatesManager = $updates_manager;
43 public function getTranslationHandler($entity_type_id) {
44 return $this->entityManager->getHandler($entity_type_id, 'translation');
50 public function getTranslationMetadata(EntityInterface $translation) {
51 // We need a new instance of the metadata handler wrapping each translation.
52 $entity_type = $translation->getEntityType();
53 $class = $entity_type->get('content_translation_metadata');
54 return new $class($translation, $this->getTranslationHandler($entity_type->id()));
60 public function isSupported($entity_type_id) {
61 $entity_type = $this->entityManager->getDefinition($entity_type_id);
62 return $entity_type->isTranslatable() && ($entity_type->hasLinkTemplate('drupal:content-translation-overview') || $entity_type->get('content_translation_ui_skip'));
68 public function getSupportedEntityTypes() {
69 $supported_types = [];
70 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
71 if ($this->isSupported($entity_type_id)) {
72 $supported_types[$entity_type_id] = $entity_type;
75 return $supported_types;
81 public function setEnabled($entity_type_id, $bundle, $value) {
82 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
83 $config->setThirdPartySetting('content_translation', 'enabled', $value)->save();
84 $entity_type = $this->entityManager->getDefinition($entity_type_id);
85 $this->updatesManager->updateDefinitions([$entity_type_id => $entity_type]);
91 public function isEnabled($entity_type_id, $bundle = NULL) {
94 if ($this->isSupported($entity_type_id)) {
95 $bundles = !empty($bundle) ? [$bundle] : array_keys($this->entityManager->getBundleInfo($entity_type_id));
96 foreach ($bundles as $bundle) {
97 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
98 if ($config->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
109 * Loads a content language config entity based on the entity type and bundle.
111 * @param string $entity_type_id
112 * ID of the entity type.
113 * @param string $bundle
116 * @return \Drupal\language\Entity\ContentLanguageSettings
117 * The content language config entity if one exists. Otherwise, returns
120 protected function loadContentLanguageSettings($entity_type_id, $bundle) {
121 if ($entity_type_id == NULL || $bundle == NULL) {
124 $config = $this->entityManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
125 if ($config == NULL) {
126 $config = $this->entityManager->getStorage('language_content_settings')->create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);