3 namespace Drupal\content_translation;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\workflows\Entity\Workflow;
10 * Provides common functionality for content translation.
12 class ContentTranslationManager implements ContentTranslationManagerInterface, BundleTranslationSettingsInterface {
15 * The entity type manager.
17 * @var \Drupal\Core\Entity\EntityManagerInterface
19 protected $entityManager;
22 * The updates manager.
24 * @var \Drupal\content_translation\ContentTranslationUpdatesManager
26 protected $updatesManager;
29 * Constructs a ContentTranslationManageAccessCheck object.
31 * @param \Drupal\Core\Entity\EntityManagerInterface $manager
32 * The entity type manager.
33 * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
34 * The updates manager.
36 public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
37 $this->entityManager = $manager;
38 $this->updatesManager = $updates_manager;
44 public function getTranslationHandler($entity_type_id) {
45 return $this->entityManager->getHandler($entity_type_id, 'translation');
51 public function getTranslationMetadata(EntityInterface $translation) {
52 // We need a new instance of the metadata handler wrapping each translation.
53 $entity_type = $translation->getEntityType();
54 $class = $entity_type->get('content_translation_metadata');
55 return new $class($translation, $this->getTranslationHandler($entity_type->id()));
61 public function isSupported($entity_type_id) {
62 $entity_type = $this->entityManager->getDefinition($entity_type_id);
63 return $entity_type->isTranslatable() && ($entity_type->hasLinkTemplate('drupal:content-translation-overview') || $entity_type->get('content_translation_ui_skip'));
69 public function getSupportedEntityTypes() {
70 $supported_types = [];
71 foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
72 if ($this->isSupported($entity_type_id)) {
73 $supported_types[$entity_type_id] = $entity_type;
76 return $supported_types;
82 public function setEnabled($entity_type_id, $bundle, $value) {
83 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
84 $config->setThirdPartySetting('content_translation', 'enabled', $value)->save();
85 $entity_type = $this->entityManager->getDefinition($entity_type_id);
86 $this->updatesManager->updateDefinitions([$entity_type_id => $entity_type]);
92 public function isEnabled($entity_type_id, $bundle = NULL) {
95 if ($this->isSupported($entity_type_id)) {
96 $bundles = !empty($bundle) ? [$bundle] : array_keys($this->entityManager->getBundleInfo($entity_type_id));
97 foreach ($bundles as $bundle) {
98 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
99 if ($config->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
112 public function setBundleTranslationSettings($entity_type_id, $bundle, array $settings) {
113 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
114 $config->setThirdPartySetting('content_translation', 'bundle_settings', $settings)
121 public function getBundleTranslationSettings($entity_type_id, $bundle) {
122 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
123 return $config->getThirdPartySetting('content_translation', 'bundle_settings', []);
127 * Loads a content language config entity based on the entity type and bundle.
129 * @param string $entity_type_id
130 * ID of the entity type.
131 * @param string $bundle
134 * @return \Drupal\language\Entity\ContentLanguageSettings
135 * The content language config entity if one exists. Otherwise, returns
138 protected function loadContentLanguageSettings($entity_type_id, $bundle) {
139 if ($entity_type_id == NULL || $bundle == NULL) {
142 $config = $this->entityManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
143 if ($config == NULL) {
144 $config = $this->entityManager->getStorage('language_content_settings')->create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
150 * Checks whether support for pending revisions should be enabled.
152 * @param string $entity_type_id
153 * The ID of the entity type to be checked.
154 * @param string $bundle_id
155 * (optional) The ID of the bundle to be checked. Defaults to none.
158 * TRUE if pending revisions should be enabled, FALSE otherwise.
161 * There is ongoing discussion about how pending revisions should behave.
162 * The logic enabling pending revision support is likely to change once a
165 * @see https://www.drupal.org/node/2940575
167 public static function isPendingRevisionSupportEnabled($entity_type_id, $bundle_id = NULL) {
168 if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) {
172 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
173 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
174 $plugin = $workflow->getTypePlugin();
175 $entity_type_ids = array_flip($plugin->getEntityTypes());
176 if (isset($entity_type_ids[$entity_type_id])) {
177 if (!isset($bundle_id)) {
181 $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id));
182 if (isset($bundle_ids[$bundle_id])) {