Backup of db before drupal security update
[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
8 /**
9  * Provides common functionality for content translation.
10  */
11 class ContentTranslationManager implements ContentTranslationManagerInterface {
12
13   /**
14    * The entity type manager.
15    *
16    * @var \Drupal\Core\Entity\EntityManagerInterface
17    */
18   protected $entityManager;
19
20   /**
21    * The updates manager.
22    *
23    * @var \Drupal\content_translation\ContentTranslationUpdatesManager
24    */
25   protected $updatesManager;
26
27   /**
28    * Constructs a ContentTranslationManageAccessCheck object.
29    *
30    * @param \Drupal\Core\Entity\EntityManagerInterface $manager
31    *   The entity type manager.
32    * @param \Drupal\content_translation\ContentTranslationUpdatesManager $updates_manager
33    *   The updates manager.
34    */
35   public function __construct(EntityManagerInterface $manager, ContentTranslationUpdatesManager $updates_manager) {
36     $this->entityManager = $manager;
37     $this->updatesManager = $updates_manager;
38   }
39
40   /**
41    * {@inheritdoc}
42    */
43   public function getTranslationHandler($entity_type_id) {
44     return $this->entityManager->getHandler($entity_type_id, 'translation');
45   }
46
47   /**
48    * {@inheritdoc}
49    */
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()));
55   }
56
57   /**
58    * {@inheritdoc}
59    */
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'));
63   }
64
65   /**
66    * {@inheritdoc}
67    */
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;
73       }
74     }
75     return $supported_types;
76   }
77
78   /**
79    * {@inheritdoc}
80    */
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]);
86   }
87
88   /**
89    * {@inheritdoc}
90    */
91   public function isEnabled($entity_type_id, $bundle = NULL) {
92     $enabled = FALSE;
93
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)) {
99           $enabled = TRUE;
100           break;
101         }
102       }
103     }
104
105     return $enabled;
106   }
107
108   /**
109    * Loads a content language config entity based on the entity type and bundle.
110    *
111    * @param string $entity_type_id
112    *   ID of the entity type.
113    * @param string $bundle
114    *   Bundle name.
115    *
116    * @return \Drupal\language\Entity\ContentLanguageSettings
117    *   The content language config entity if one exists. Otherwise, returns
118    *   default values.
119    */
120   protected function loadContentLanguageSettings($entity_type_id, $bundle) {
121     if ($entity_type_id == NULL || $bundle == NULL) {
122       return NULL;
123     }
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]);
127     }
128     return $config;
129   }
130
131 }