e16cf168b8293c4246bec565132e73a3b55b32be
[yaffs-website] / web / core / modules / content_translation / src / ContentTranslationManager.php
1 <?php
2
3 namespace Drupal\content_translation;
4
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\workflows\Entity\Workflow;
8
9 /**
10  * Provides common functionality for content translation.
11  */
12 class ContentTranslationManager implements ContentTranslationManagerInterface, BundleTranslationSettingsInterface {
13
14   /**
15    * The entity type manager.
16    *
17    * @var \Drupal\Core\Entity\EntityManagerInterface
18    */
19   protected $entityManager;
20
21   /**
22    * The updates manager.
23    *
24    * @var \Drupal\content_translation\ContentTranslationUpdatesManager
25    */
26   protected $updatesManager;
27
28   /**
29    * Constructs a ContentTranslationManageAccessCheck object.
30    *
31    * @param \Drupal\Core\Entity\EntityManagerInterface $manager
32    *   The entity type manager.
33    * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
34    *   The updates manager.
35    */
36   public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
37     $this->entityManager = $manager;
38     $this->updatesManager = $updates_manager;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getTranslationHandler($entity_type_id) {
45     return $this->entityManager->getHandler($entity_type_id, 'translation');
46   }
47
48   /**
49    * {@inheritdoc}
50    */
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()));
56   }
57
58   /**
59    * {@inheritdoc}
60    */
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'));
64   }
65
66   /**
67    * {@inheritdoc}
68    */
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;
74       }
75     }
76     return $supported_types;
77   }
78
79   /**
80    * {@inheritdoc}
81    */
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]);
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public function isEnabled($entity_type_id, $bundle = NULL) {
93     $enabled = FALSE;
94
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)) {
100           $enabled = TRUE;
101           break;
102         }
103       }
104     }
105
106     return $enabled;
107   }
108
109   /**
110    * {@inheritdoc}
111    */
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)
115       ->save();
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   public function getBundleTranslationSettings($entity_type_id, $bundle) {
122     $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
123     return $config->getThirdPartySetting('content_translation', 'bundle_settings', []);
124   }
125
126   /**
127    * Loads a content language config entity based on the entity type and bundle.
128    *
129    * @param string $entity_type_id
130    *   ID of the entity type.
131    * @param string $bundle
132    *   Bundle name.
133    *
134    * @return \Drupal\language\Entity\ContentLanguageSettings
135    *   The content language config entity if one exists. Otherwise, returns
136    *   default values.
137    */
138   protected function loadContentLanguageSettings($entity_type_id, $bundle) {
139     if ($entity_type_id == NULL || $bundle == NULL) {
140       return NULL;
141     }
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]);
145     }
146     return $config;
147   }
148
149   /**
150    * Checks whether support for pending revisions should be enabled.
151    *
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.
156    *
157    * @return bool
158    *   TRUE if pending revisions should be enabled, FALSE otherwise.
159    *
160    * @internal
161    *   There is ongoing discussion about how pending revisions should behave.
162    *   The logic enabling pending revision support is likely to change once a
163    *   decision is made.
164    *
165    * @see https://www.drupal.org/node/2940575
166    */
167   public static function isPendingRevisionSupportEnabled($entity_type_id, $bundle_id = NULL) {
168     if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) {
169       return FALSE;
170     }
171
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)) {
178           return TRUE;
179         }
180         else {
181           $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id));
182           if (isset($bundle_ids[$bundle_id])) {
183             return TRUE;
184           }
185         }
186       }
187     }
188
189     return FALSE;
190   }
191
192 }