annotate core/modules/content_translation/src/ContentTranslationManager.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents c75dbcec494b
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\content_translation;
Chris@0 4
Chris@5 5 use Drupal\Core\DependencyInjection\DeprecatedServicePropertyTrait;
Chris@0 6 use Drupal\Core\Entity\EntityInterface;
Chris@0 7 use Drupal\workflows\Entity\Workflow;
Chris@5 8 use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
Chris@5 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@0 14 class ContentTranslationManager implements ContentTranslationManagerInterface, BundleTranslationSettingsInterface {
Chris@5 15 use DeprecatedServicePropertyTrait;
Chris@5 16
Chris@5 17 /**
Chris@5 18 * {@inheritdoc}
Chris@5 19 */
Chris@5 20 protected $deprecatedProperties = [
Chris@5 21 'entityManager' => 'entity.manager',
Chris@5 22 'updatesManager' => 'content_translation.updates_manager',
Chris@5 23 ];
Chris@5 24
Chris@5 25 /**
Chris@5 26 * The entity type bundle info provider.
Chris@5 27 *
Chris@5 28 * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
Chris@5 29 */
Chris@5 30 protected $entityTypeBundleInfo;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * The entity type manager.
Chris@0 34 *
Chris@5 35 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
Chris@0 36 */
Chris@5 37 protected $entityTypeManager;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * Constructs a ContentTranslationManageAccessCheck object.
Chris@0 41 *
Chris@5 42 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
Chris@0 43 * The entity type manager.
Chris@5 44 * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info
Chris@5 45 * The entity type bundle info provider.
Chris@0 46 */
Chris@5 47 public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_bundle_info) {
Chris@5 48 $this->entityTypeManager = $entity_type_manager;
Chris@5 49
Chris@5 50 if (!($entity_type_bundle_info instanceof EntityTypeBundleInfoInterface)) {
Chris@5 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@5 52 $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info');
Chris@5 53 }
Chris@5 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@5 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@5 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@5 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@5 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@0 124 * {@inheritdoc}
Chris@0 125 */
Chris@0 126 public function setBundleTranslationSettings($entity_type_id, $bundle, array $settings) {
Chris@0 127 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@0 128 $config->setThirdPartySetting('content_translation', 'bundle_settings', $settings)
Chris@0 129 ->save();
Chris@0 130 }
Chris@0 131
Chris@0 132 /**
Chris@0 133 * {@inheritdoc}
Chris@0 134 */
Chris@0 135 public function getBundleTranslationSettings($entity_type_id, $bundle) {
Chris@0 136 $config = $this->loadContentLanguageSettings($entity_type_id, $bundle);
Chris@0 137 return $config->getThirdPartySetting('content_translation', 'bundle_settings', []);
Chris@0 138 }
Chris@0 139
Chris@0 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@5 156 $config = $this->entityTypeManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle);
Chris@0 157 if ($config == NULL) {
Chris@5 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@0 163 /**
Chris@0 164 * Checks whether support for pending revisions should be enabled.
Chris@0 165 *
Chris@0 166 * @param string $entity_type_id
Chris@0 167 * The ID of the entity type to be checked.
Chris@0 168 * @param string $bundle_id
Chris@0 169 * (optional) The ID of the bundle to be checked. Defaults to none.
Chris@0 170 *
Chris@0 171 * @return bool
Chris@0 172 * TRUE if pending revisions should be enabled, FALSE otherwise.
Chris@0 173 *
Chris@0 174 * @internal
Chris@0 175 * There is ongoing discussion about how pending revisions should behave.
Chris@0 176 * The logic enabling pending revision support is likely to change once a
Chris@0 177 * decision is made.
Chris@0 178 *
Chris@0 179 * @see https://www.drupal.org/node/2940575
Chris@0 180 */
Chris@0 181 public static function isPendingRevisionSupportEnabled($entity_type_id, $bundle_id = NULL) {
Chris@0 182 if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) {
Chris@0 183 return FALSE;
Chris@0 184 }
Chris@0 185
Chris@0 186 foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) {
Chris@0 187 /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */
Chris@0 188 $plugin = $workflow->getTypePlugin();
Chris@0 189 $entity_type_ids = array_flip($plugin->getEntityTypes());
Chris@0 190 if (isset($entity_type_ids[$entity_type_id])) {
Chris@0 191 if (!isset($bundle_id)) {
Chris@0 192 return TRUE;
Chris@0 193 }
Chris@0 194 else {
Chris@0 195 $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id));
Chris@0 196 if (isset($bundle_ids[$bundle_id])) {
Chris@0 197 return TRUE;
Chris@0 198 }
Chris@0 199 }
Chris@0 200 }
Chris@0 201 }
Chris@0 202
Chris@0 203 return FALSE;
Chris@0 204 }
Chris@0 205
Chris@0 206 }