annotate core/modules/content_translation/src/ContentTranslationManager.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\content_translation;
Chris@0 4
Chris@18 5 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
Chris@0 6 use Drupal\Core\Entity\EntityInterface;
Chris@14 7 use Drupal\workflows\Entity\Workflow;
Chris@18 8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
Chris@18 9 use Drupal\Core\Entity\EntityTypeManagerInterface;
Chris@0 10
Chris@0 11 /**
Chris@0 12 * Provides common functionality for content translation.
Chris@0 13 */
Chris@14 14 class ContentTranslationManager implements ContentTranslationManagerInterface, BundleTranslationSettingsInterface {
Chris@18 15 use DeprecatedServicePropertyTrait;
Chris@18 16
Chris@18 17 /**
Chris@18 18 * {@inheritdoc}
Chris@18 19 */
Chris@18 20 protected $deprecatedProperties = [
Chris@18 21 'entityManager' => 'entity.manager',
Chris@18 22 'updatesManager' => 'content_translation.updates_manager',
Chris@18 23 ];
Chris@18 24
Chris@18 25 /**
Chris@18 26 * The entity type bundle info provider.
Chris@18 27 *
Chris@18 28 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
Chris@18 29 */
Chris@18 30 protected $entityTypeBundleInfo;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The entity type manager.
Chris@0 34 *
Chris@18 35 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 36 */
Chris@18 37 protected $entityTypeManager;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Constructs a ContentTranslationManageAccessCheck object.
Chris@0 41 *
Chris@18 42 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 43 * The entity type manager.
Chris@18 44 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
Chris@18 45 * The entity type bundle info provider.
Chris@0 46 */
Chris@18 47 public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_bundle_info) {
Chris@18 48 $this->entityTypeManager = $entity_type_manager;
Chris@18 49
Chris@18 50 if (!($entity_type_bundle_info instanceof EntityTypeBundleInfoInterface)) {
Chris@18 51 @trigger_error('The entity_type.bundle.info service should be passed to ContentTranslationManager::__construct() instead of the content_translation.updates_manager service since 8.7.0. This will be required in Drupal 9.0.0. See https://www.drupal.org/node/2549139 and https://www.drupal.org/node/2973222.', E_USER_DEPRECATED);
Chris@18 52 $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
Chris@18 53 }
Chris@18 54 $this->entityTypeBundleInfo = $entity_type_bundle_info;
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * {@inheritdoc}
Chris@0 59 */
Chris@0 60 public function getTranslationHandler($entity_type_id) {
Chris@18 61 return $this->entityTypeManager->getHandler($entity_type_id, 'translation');
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * {@inheritdoc}
Chris@0 66 */
Chris@0 67 public function getTranslationMetadata(EntityInterface $translation) {
Chris@0 68 // We need a new instance of the metadata handler wrapping each translation.
Chris@0 69 $entity_type = $translation->getEntityType();
Chris@0 70 $class = $entity_type->get('content_translation_metadata');
Chris@0 71 return new $class($translation, $this->getTranslationHandler($entity_type->id()));
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function isSupported($entity_type_id) {
Chris@18 78 $entity_type = $this->entityTypeManager->getDefinition($entity_type_id);
Chris@0 79 return $entity_type->isTranslatable() && ($entity_type->hasLinkTemplate('drupal:content-translation-overview') || $entity_type->get('content_translation_ui_skip'));
Chris@0 80 }
Chris@0 81
Chris@0 82 /**
Chris@0 83 * {@inheritdoc}
Chris@0 84 */
Chris@0 85 public function getSupportedEntityTypes() {
Chris@0 86 $supported_types = [];
Chris@18 87 foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) {
Chris@0 88 if ($this->isSupported($entity_type_id)) {
Chris@0 89 $supported_types[$entity_type_id] = $entity_type;
Chris@0 90 }
Chris@0 91 }
Chris@0 92 return $supported_types;
Chris@0 93 }
Chris@0 94
Chris@0 95 /**
Chris@0 96 * {@inheritdoc}
Chris@0 97 */
Chris@0 98 public function setEnabled($entity_type_id, $bundle, $value) {
Chris@0 99 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@0 100 $config->setThirdPartySetting('content_translation', 'enabled', $value)->save();
Chris@0 101 }
Chris@0 102
Chris@0 103 /**
Chris@0 104 * {@inheritdoc}
Chris@0 105 */
Chris@0 106 public function isEnabled($entity_type_id, $bundle = NULL) {
Chris@0 107 $enabled = FALSE;
Chris@0 108
Chris@0 109 if ($this->isSupported($entity_type_id)) {
Chris@18 110 $bundles = !empty($bundle) ? [$bundle] : array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id));
Chris@0 111 foreach ($bundles as $bundle) {
Chris@0 112 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@0 113 if ($config->getThirdPartySetting('content_translation', 'enabled', FALSE)) {
Chris@0 114 $enabled = TRUE;
Chris@0 115 break;
Chris@0 116 }
Chris@0 117 }
Chris@0 118 }
Chris@0 119
Chris@0 120 return $enabled;
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@14 124 * {@inheritdoc}
Chris@14 125 */
Chris@14 126 public function setBundleTranslationSettings($entity_type_id, $bundle, array $settings) {
Chris@14 127 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@14 128 $config->setThirdPartySetting('content_translation', 'bundle_settings', $settings)
Chris@14 129 ->save();
Chris@14 130 }
Chris@14 131
Chris@14 132 /**
Chris@14 133 * {@inheritdoc}
Chris@14 134 */
Chris@14 135 public function getBundleTranslationSettings($entity_type_id, $bundle) {
Chris@14 136 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@14 137 return $config->getThirdPartySetting('content_translation', 'bundle_settings', []);
Chris@14 138 }
Chris@14 139
Chris@14 140 /**
Chris@0 141 * Loads a content language config entity based on the entity type and bundle.
Chris@0 142 *
Chris@0 143 * @param string $entity_type_id
Chris@0 144 * ID of the entity type.
Chris@0 145 * @param string $bundle
Chris@0 146 * Bundle name.
Chris@0 147 *
Chris@0 148 * @return \Drupal\language\Entity\ContentLanguageSettings
Chris@0 149 * The content language config entity if one exists. Otherwise, returns
Chris@0 150 * default values.
Chris@0 151 */
Chris@0 152 protected function loadContentLanguageSettings($entity_type_id, $bundle) {
Chris@0 153 if ($entity_type_id == NULL || $bundle == NULL) {
Chris@0 154 return NULL;
Chris@0 155 }
Chris@18 156 $config = $this->entityTypeManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
Chris@0 157 if ($config == NULL) {
Chris@18 158 $config = $this->entityTypeManager->getStorage('language_content_settings')->create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]);
Chris@0 159 }
Chris@0 160 return $config;
Chris@0 161 }
Chris@0 162
Chris@14 163 /**
Chris@14 164 * Checks whether support for pending revisions should be enabled.
Chris@14 165 *
Chris@14 166 * @param string $entity_type_id
Chris@14 167 * The ID of the entity type to be checked.
Chris@14 168 * @param string $bundle_id
Chris@14 169 * (optional) The ID of the bundle to be checked. Defaults to none.
Chris@14 170 *
Chris@14 171 * @return bool
Chris@14 172 * TRUE if pending revisions should be enabled, FALSE otherwise.
Chris@14 173 *
Chris@14 174 * @internal
Chris@14 175 * There is ongoing discussion about how pending revisions should behave.
Chris@14 176 * The logic enabling pending revision support is likely to change once a
Chris@14 177 * decision is made.
Chris@14 178 *
Chris@14 179 * @see https://www.drupal.org/node/2940575
Chris@14 180 */
Chris@14 181 public static function isPendingRevisionSupportEnabled($entity_type_id, $bundle_id = NULL) {
Chris@14 182 if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) {
Chris@14 183 return FALSE;
Chris@14 184 }
Chris@14 185
Chris@14 186 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
Chris@14 187 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
Chris@14 188 $plugin = $workflow->getTypePlugin();
Chris@14 189 $entity_type_ids = array_flip($plugin->getEntityTypes());
Chris@14 190 if (isset($entity_type_ids[$entity_type_id])) {
Chris@14 191 if (!isset($bundle_id)) {
Chris@14 192 return TRUE;
Chris@14 193 }
Chris@14 194 else {
Chris@14 195 $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id));
Chris@14 196 if (isset($bundle_ids[$bundle_id])) {
Chris@14 197 return TRUE;
Chris@14 198 }
Chris@14 199 }
Chris@14 200 }
Chris@14 201 }
Chris@14 202
Chris@14 203 return FALSE;
Chris@14 204 }
Chris@14 205
Chris@0 206 }