Chris@0: 'entity.manager', Chris@18: 'updatesManager' => 'content_translation.updates_manager', Chris@18: ]; Chris@18: Chris@18: /** Chris@18: * The entity type bundle info provider. Chris@18: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface Chris@18: */ Chris@18: protected $entityTypeBundleInfo; Chris@0: Chris@0: /** Chris@0: * The entity type manager. Chris@0: * Chris@18: * @var \Drupal\Core\Entity\EntityTypeManagerInterface Chris@0: */ Chris@18: protected $entityTypeManager; Chris@0: Chris@0: /** Chris@0: * Constructs a ContentTranslationManageAccessCheck object. Chris@0: * Chris@18: * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager Chris@0: * The entity type manager. Chris@18: * @param \Drupal\Core\Entity\EntityTypeBundleInfoInterface $entity_type_bundle_info Chris@18: * The entity type bundle info provider. Chris@0: */ Chris@18: public function __construct(EntityTypeManagerInterface $entity_type_manager, $entity_type_bundle_info) { Chris@18: $this->entityTypeManager = $entity_type_manager; Chris@18: Chris@18: if (!($entity_type_bundle_info instanceof EntityTypeBundleInfoInterface)) { Chris@18: @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: $entity_type_bundle_info = \Drupal::service('entity_type.bundle.info'); Chris@18: } Chris@18: $this->entityTypeBundleInfo = $entity_type_bundle_info; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTranslationHandler($entity_type_id) { Chris@18: return $this->entityTypeManager->getHandler($entity_type_id, 'translation'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getTranslationMetadata(EntityInterface $translation) { Chris@0: // We need a new instance of the metadata handler wrapping each translation. Chris@0: $entity_type = $translation->getEntityType(); Chris@0: $class = $entity_type->get('content_translation_metadata'); Chris@0: return new $class($translation, $this->getTranslationHandler($entity_type->id())); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isSupported($entity_type_id) { Chris@18: $entity_type = $this->entityTypeManager->getDefinition($entity_type_id); Chris@0: return $entity_type->isTranslatable() && ($entity_type->hasLinkTemplate('drupal:content-translation-overview') || $entity_type->get('content_translation_ui_skip')); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getSupportedEntityTypes() { Chris@0: $supported_types = []; Chris@18: foreach ($this->entityTypeManager->getDefinitions() as $entity_type_id => $entity_type) { Chris@0: if ($this->isSupported($entity_type_id)) { Chris@0: $supported_types[$entity_type_id] = $entity_type; Chris@0: } Chris@0: } Chris@0: return $supported_types; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function setEnabled($entity_type_id, $bundle, $value) { Chris@0: $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); Chris@0: $config->setThirdPartySetting('content_translation', 'enabled', $value)->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function isEnabled($entity_type_id, $bundle = NULL) { Chris@0: $enabled = FALSE; Chris@0: Chris@0: if ($this->isSupported($entity_type_id)) { Chris@18: $bundles = !empty($bundle) ? [$bundle] : array_keys($this->entityTypeBundleInfo->getBundleInfo($entity_type_id)); Chris@0: foreach ($bundles as $bundle) { Chris@0: $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); Chris@0: if ($config->getThirdPartySetting('content_translation', 'enabled', FALSE)) { Chris@0: $enabled = TRUE; Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $enabled; Chris@0: } Chris@0: Chris@0: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function setBundleTranslationSettings($entity_type_id, $bundle, array $settings) { Chris@14: $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); Chris@14: $config->setThirdPartySetting('content_translation', 'bundle_settings', $settings) Chris@14: ->save(); Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@14: public function getBundleTranslationSettings($entity_type_id, $bundle) { Chris@14: $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); Chris@14: return $config->getThirdPartySetting('content_translation', 'bundle_settings', []); Chris@14: } Chris@14: Chris@14: /** Chris@0: * Loads a content language config entity based on the entity type and bundle. Chris@0: * Chris@0: * @param string $entity_type_id Chris@0: * ID of the entity type. Chris@0: * @param string $bundle Chris@0: * Bundle name. Chris@0: * Chris@0: * @return \Drupal\language\Entity\ContentLanguageSettings Chris@0: * The content language config entity if one exists. Otherwise, returns Chris@0: * default values. Chris@0: */ Chris@0: protected function loadContentLanguageSettings($entity_type_id, $bundle) { Chris@0: if ($entity_type_id == NULL || $bundle == NULL) { Chris@0: return NULL; Chris@0: } Chris@18: $config = $this->entityTypeManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle); Chris@0: if ($config == NULL) { Chris@18: $config = $this->entityTypeManager->getStorage('language_content_settings')->create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]); Chris@0: } Chris@0: return $config; Chris@0: } Chris@0: Chris@14: /** Chris@14: * Checks whether support for pending revisions should be enabled. Chris@14: * Chris@14: * @param string $entity_type_id Chris@14: * The ID of the entity type to be checked. Chris@14: * @param string $bundle_id Chris@14: * (optional) The ID of the bundle to be checked. Defaults to none. Chris@14: * Chris@14: * @return bool Chris@14: * TRUE if pending revisions should be enabled, FALSE otherwise. Chris@14: * Chris@14: * @internal Chris@14: * There is ongoing discussion about how pending revisions should behave. Chris@14: * The logic enabling pending revision support is likely to change once a Chris@14: * decision is made. Chris@14: * Chris@14: * @see https://www.drupal.org/node/2940575 Chris@14: */ Chris@14: public static function isPendingRevisionSupportEnabled($entity_type_id, $bundle_id = NULL) { Chris@14: if (!\Drupal::moduleHandler()->moduleExists('content_moderation')) { Chris@14: return FALSE; Chris@14: } Chris@14: Chris@14: foreach (Workflow::loadMultipleByType('content_moderation') as $workflow) { Chris@14: /** @var \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration $plugin */ Chris@14: $plugin = $workflow->getTypePlugin(); Chris@14: $entity_type_ids = array_flip($plugin->getEntityTypes()); Chris@14: if (isset($entity_type_ids[$entity_type_id])) { Chris@14: if (!isset($bundle_id)) { Chris@14: return TRUE; Chris@14: } Chris@14: else { Chris@14: $bundle_ids = array_flip($plugin->getBundlesForEntityType($entity_type_id)); Chris@14: if (isset($bundle_ids[$bundle_id])) { Chris@14: return TRUE; Chris@14: } Chris@14: } Chris@14: } Chris@14: } Chris@14: Chris@14: return FALSE; Chris@14: } Chris@14: Chris@0: }