diff --git a/core/lib/Drupal/Core/Config/Testing/ConfigSchemaChecker.php b/core/lib/Drupal/Core/Config/Testing/ConfigSchemaChecker.php index cf5f113..db80944 100644 --- a/core/lib/Drupal/Core/Config/Testing/ConfigSchemaChecker.php +++ b/core/lib/Drupal/Core/Config/Testing/ConfigSchemaChecker.php @@ -66,9 +66,7 @@ public function onConfigSave(ConfigCrudEvent $event) { $name = $saved_config->getName(); $data = $saved_config->get(); $checksum = crc32(serialize($data)); - // Content translation settings cannot be provided schema yet, see - // https://www.drupal.org/node/2363155 - if ($name != 'content_translation.settings' && !isset($this->checked[$name . ':' . $checksum])) { + if (!isset($this->checked[$name . ':' . $checksum])) { $this->checked[$name . ':' . $checksum] = TRUE; $errors = $this->checkConfigSchema($this->typedManager, $name, $data); if ($errors === FALSE) { diff --git a/core/modules/comment/src/Tests/CommentTranslationUITest.php b/core/modules/comment/src/Tests/CommentTranslationUITest.php index a11ef3f..945e532 100644 --- a/core/modules/comment/src/Tests/CommentTranslationUITest.php +++ b/core/modules/comment/src/Tests/CommentTranslationUITest.php @@ -53,7 +53,7 @@ function setupBundle() { // translatable. $this->container->get('comment.manager')->addDefaultField('node', 'page', 'comment'); // Mark this bundle as translatable. - content_translation_set_config('comment', 'comment_article', 'enabled', TRUE); + $this->container->get('content_translation.manager')->setEnabled('comment', 'comment_article', TRUE); } /** diff --git a/core/modules/content_translation/config/schema/content_translation.schema.yml b/core/modules/content_translation/config/schema/content_translation.schema.yml index 6e4167e..1e724d8 100644 --- a/core/modules/content_translation/config/schema/content_translation.schema.yml +++ b/core/modules/content_translation/config/schema/content_translation.schema.yml @@ -10,3 +10,11 @@ field_config.third_party.content_translation: sequence: - type: string label: 'Field column for which to synchronize translations' + +language.content_settings.third_party.content_translation: + type: mapping + label: 'Content translation content settings' + mapping: + enabled: + type: boolean + label: 'Content translation enabled' diff --git a/core/modules/content_translation/content_translation.admin.inc b/core/modules/content_translation/content_translation.admin.inc index 6b87a15..d812652 100644 --- a/core/modules/content_translation/content_translation.admin.inc +++ b/core/modules/content_translation/content_translation.admin.inc @@ -72,9 +72,10 @@ function _content_translation_form_language_content_settings_form_alter(array &$ return; } + $content_translation_manager = \Drupal::service('content_translation.manager'); $default = $form['entity_types']['#default_value']; foreach ($default as $entity_type_id => $enabled) { - $default[$entity_type_id] = $enabled || content_translation_enabled($entity_type_id) ? $entity_type_id : FALSE; + $default[$entity_type_id] = $enabled || $content_translation_manager->isEnabled($entity_type_id) ? $entity_type_id : FALSE; } $form['entity_types']['#default_value'] = $default; @@ -118,7 +119,7 @@ function _content_translation_form_language_content_settings_form_alter(array &$ // entity might have fields and if there are fields to translate. $form['settings'][$entity_type_id][$bundle]['translatable'] = array( '#type' => 'checkbox', - '#default_value' => content_translation_enabled($entity_type_id, $bundle), + '#default_value' => $content_translation_manager->isEnabled($entity_type_id, $bundle), ); } } @@ -313,7 +314,7 @@ function content_translation_form_language_content_settings_submit(array $form, } if (isset($bundle_settings['translatable'])) { // Store whether a bundle has translation enabled or not. - content_translation_set_config($entity_type_id, $bundle, 'enabled', $bundle_settings['translatable']); + \Drupal::service('content_translation.manager')->setEnabled($entity_type_id, $bundle, $bundle_settings['translatable']); // Save translation_sync settings. if (!empty($bundle_settings['columns'])) { diff --git a/core/modules/content_translation/content_translation.module b/core/modules/content_translation/content_translation.module index df1e077..da196a5 100644 --- a/core/modules/content_translation/content_translation.module +++ b/core/modules/content_translation/content_translation.module @@ -12,6 +12,7 @@ use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Routing\RouteMatchInterface; +use Drupal\language\Entity\ContentLanguageSettings; use Drupal\node\NodeInterface; /** @@ -134,8 +135,7 @@ function content_translation_entity_type_alter(array &$entity_types) { function content_translation_entity_bundle_info_alter(&$bundles) { foreach ($bundles as $entity_type => &$info) { foreach ($info as $bundle => &$bundle_info) { - $enabled = content_translation_get_config($entity_type, $bundle, 'enabled'); - $bundle_info['translatable'] = !empty($enabled); + $bundle_info['translatable'] = \Drupal::service('content_translation.manager')->isEnabled($entity_type, $bundle); } } } @@ -231,97 +231,6 @@ function content_translation_translate_access(EntityInterface $entity) { } /** - * Returns the key name used to store the configuration setting. - * - * Based on the entity type and bundle, the keys used to store configuration - * will have a common root name. - * - * @param string $entity_type - * The type of the entity the setting refers to. - * @param string $bundle - * The bundle of the entity the setting refers to. - * @param string $setting - * The name of the setting. - * - * @return string - * The key name of the configuration setting. - * - * @todo Generalize this logic so that it is available to any module needing - * per-bundle configuration. - */ -function content_translation_get_config_key($entity_type, $bundle, $setting) { - $entity_type = preg_replace('/[^0-9a-zA-Z_]/', "_", $entity_type); - $bundle = preg_replace('/[^0-9a-zA-Z_]/', "_", $bundle); - return $entity_type . '.' . $bundle . '.content_translation.' . $setting; -} - -/** - * Retrieves the value for the specified setting. - * - * @param string $entity_type - * The type of the entity the setting refer to. - * @param string $bundle - * The bundle of the entity the setting refer to. - * @param string $setting - * The name of the setting. - * - * @returns mixed - * The stored value for the given setting. - */ -function content_translation_get_config($entity_type, $bundle, $setting) { - $key = content_translation_get_config_key($entity_type, $bundle, $setting); - return \Drupal::config('content_translation.settings')->get($key); -} - -/** - * Stores the given value for the specified setting. - * - * @param string $entity_type - * The type of the entity the setting refer to. - * @param string $bundle - * The bundle of the entity the setting refer to. - * @param string $setting - * The name of the setting. - * @param $value - * The value to be stored for the given setting. - */ -function content_translation_set_config($entity_type, $bundle, $setting, $value) { - $key = content_translation_get_config_key($entity_type, $bundle, $setting); - return \Drupal::config('content_translation.settings')->set($key, $value)->save(); -} - -/** - * Determines whether the given entity type is translatable. - * - * @param string $entity_type - * The type of the entity. - * @param string $bundle - * (optional) The bundle of the entity. If no bundle is provided, all the - * available bundles are checked. - * - * @returns - * TRUE if the specified bundle is translatable. If no bundle is provided - * returns TRUE if at least one of the entity bundles is translatable. - * - * @todo Move to \Drupal\content_translation\ContentTranslationManager. - */ -function content_translation_enabled($entity_type, $bundle = NULL) { - $enabled = FALSE; - - if (\Drupal::service('content_translation.manager')->isSupported($entity_type)) { - $bundles = !empty($bundle) ? array($bundle) : array_keys(entity_get_bundles($entity_type)); - foreach ($bundles as $bundle) { - if (content_translation_get_config($entity_type, $bundle, 'enabled')) { - $enabled = TRUE; - break; - } - } - } - - return $enabled; -} - -/** * Content translation controller factory. * * @param string $entity_type_id @@ -419,7 +328,7 @@ function content_translation_language_fallback_candidates_entity_view_alter(&$ca function content_translation_entity_storage_load(array $entities, $entity_type) { $enabled_entities = array(); - if (content_translation_enabled($entity_type)) { + if (\Drupal::service('content_translation.manager')->isEnabled($entity_type)) { foreach ($entities as $entity) { if ($entity instanceof ContentEntityInterface && $entity->isTranslatable()) { $enabled_entities[$entity->id()] = $entity; @@ -536,7 +445,7 @@ function content_translation_entity_extra_field_info() { foreach (\Drupal::entityManager()->getDefinitions() as $entity_type => $info) { foreach (entity_get_bundles($entity_type) as $bundle => $bundle_info) { - if (content_translation_enabled($entity_type, $bundle)) { + if (\Drupal::service('content_translation.manager')->isEnabled($entity_type, $bundle)) { $extra[$entity_type][$bundle]['form']['translation'] = array( 'label' => t('Translation'), 'description' => t('Translation settings'), @@ -554,7 +463,7 @@ function content_translation_entity_extra_field_info() { */ function content_translation_form_field_ui_field_edit_form_alter(array &$form, FormStateInterface $form_state) { $field = $form_state->get('field'); - $bundle_is_translatable = content_translation_enabled($field->entity_type, $field->bundle); + $bundle_is_translatable = \Drupal::service('content_translation.manager')->isEnabled($field->entity_type, $field->bundle); $form['field']['translatable'] = array( '#type' => 'checkbox', @@ -653,7 +562,7 @@ function content_translation_language_configuration_element_process(array $eleme '#title' => t('Enable translation'), // For new bundle, we don't know the bundle name yet, // default to no translatability. - '#default_value' => $context['bundle'] ? content_translation_enabled($context['entity_type'], $context['bundle']) : FALSE, + '#default_value' => $context['bundle'] ? \Drupal::service('content_translation.manager')->isEnabled($context['entity_type'], $context['bundle']) : FALSE, '#element_validate' => array('content_translation_language_configuration_element_validate'), '#prefix' => '', ); @@ -698,8 +607,8 @@ function content_translation_language_configuration_element_submit(array $form, $context = $form_state->get(['language', $key]); $enabled = $form_state->getValue(array($key, 'content_translation')); - if (content_translation_enabled($context['entity_type'], $context['bundle']) != $enabled) { - content_translation_set_config($context['entity_type'], $context['bundle'], 'enabled', $enabled); + if (\Drupal::service('content_translation.manager')->isEnabled($context['entity_type'], $context['bundle']) != $enabled) { + \Drupal::service('content_translation.manager')->setEnabled($context['entity_type'], $context['bundle'], $enabled); \Drupal::entityManager()->clearCachedDefinitions(); \Drupal::service('router.builder_indicator')->setRebuildNeeded(); } diff --git a/core/modules/content_translation/src/ContentTranslationManager.php b/core/modules/content_translation/src/ContentTranslationManager.php index 817d3f8..7c258a8 100644 --- a/core/modules/content_translation/src/ContentTranslationManager.php +++ b/core/modules/content_translation/src/ContentTranslationManager.php @@ -8,6 +8,7 @@ namespace Drupal\content_translation; use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\language\Entity\ContentLanguageSettings; /** * Provides common functionality for content translation. @@ -52,4 +53,55 @@ public function getSupportedEntityTypes() { return $supported_types; } + /** + * {@inheritdoc} + */ + public function setEnabled($entity_type_id, $bundle, $value) { + $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); + $config->setThirdPartySetting('content_translation', 'enabled', $value)->save(); + } + + /** + * {@inheritdoc} + */ + public function isEnabled($entity_type_id, $bundle = NULL) { + $enabled = FALSE; + + if ($this->isSupported($entity_type_id)) { + $bundles = !empty($bundle) ? array($bundle) : array_keys($this->entityManager->getBundleInfo($entity_type_id)); + foreach ($bundles as $bundle) { + $config = $this->loadContentLanguageSettings($entity_type_id, $bundle); + if ($config->getThirdPartySetting('content_translation', 'enabled', FALSE)) { + $enabled = TRUE; + break; + } + } + } + + return $enabled; + } + + /** + * Loads a content language config entity based on the entity type and bundle. + * + * @param string $entity_type_id + * ID of the entity type. + * @param string $bundle + * Bundle name. + * + * @return $this + * The content language config entity if one exists. Otherwise, returns + * default values. + */ + protected function loadContentLanguageSettings($entity_type_id, $bundle) { + if ($entity_type_id == NULL || $bundle == NULL) { + return NULL; + } + $config = $this->entityManager->getStorage('language_content_settings')->load($entity_type_id . '.' . $bundle); + if ($config == NULL) { + $config = $this->entityManager->getStorage('language_content_settings')->create(['target_entity_type_id' => $entity_type_id, 'target_bundle' => $bundle]); + } + return $config; + } + } diff --git a/core/modules/content_translation/src/ContentTranslationManagerInterface.php b/core/modules/content_translation/src/ContentTranslationManagerInterface.php index 8591138..e3ae29f 100644 --- a/core/modules/content_translation/src/ContentTranslationManagerInterface.php +++ b/core/modules/content_translation/src/ContentTranslationManagerInterface.php @@ -31,4 +31,32 @@ public function getSupportedEntityTypes(); */ public function isSupported($entity_type_id); + /** + * Sets the value for translatability of the given entity type bundle. + * + * @param string $entity_type_id + * The entity type. + * @param string $bundle + * The bundle of the entity. + * @param bool $value + * The boolean value we need to save. + */ + public function setEnabled($entity_type_id, $bundle, $value); + + /** + * Determines whether the given entity type is translatable. + * + * @param string $entity_type_id + * The type of the entity. + * @param string $bundle + * (optional) The bundle of the entity. If no bundle is provided, all the + * available bundles are checked. + * + * @returns bool + * TRUE if the specified bundle is translatable. If no bundle is provided + * returns TRUE if at least one of the entity bundles is translatable. + * + */ + public function isEnabled($entity_type_id, $bundle = NULL); + } diff --git a/core/modules/content_translation/src/ContentTranslationPermissions.php b/core/modules/content_translation/src/ContentTranslationPermissions.php index 35a664a..b1ff68a 100644 --- a/core/modules/content_translation/src/ContentTranslationPermissions.php +++ b/core/modules/content_translation/src/ContentTranslationPermissions.php @@ -27,20 +27,33 @@ class ContentTranslationPermissions implements ContainerInjectionInterface { protected $entityManager; /** + * The content translation manager. + * + * @var \Drupal\content_translation\ContentTranslationManagerInterface + */ + protected $contentTranslationManager; + + /** * Constructs a ContentTranslationPermissions instance. * * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager * The entity manager. + * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager + * The content translation manager. */ - public function __construct(EntityManagerInterface $entity_manager) { + public function __construct(EntityManagerInterface $entity_manager, ContentTranslationManagerInterface $content_translation_manager) { $this->entityManager = $entity_manager; + $this->contentTranslationManager = $content_translation_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { - return new static($container->get('entity.manager')); + return new static( + $container->get('entity.manager'), + $container->get('content_translation.manager') + ); } /** @@ -59,7 +72,7 @@ public function contentPermissions() { switch ($permission_granularity) { case 'bundle': foreach ($this->entityManager->getBundleInfo($entity_type_id) as $bundle => $bundle_info) { - if (content_translation_enabled($entity_type_id, $bundle)) { + if ($this->contentTranslationManager->isEnabled($entity_type_id, $bundle)) { $t_args['%bundle_label'] = isset($bundle_info['label']) ? $bundle_info['label'] : $bundle; $permission["translate $bundle $entity_type_id"] = [ 'title' => $this->t('Translate %bundle_label @entity_label', $t_args), @@ -69,7 +82,7 @@ public function contentPermissions() { break; case 'entity_type': - if (content_translation_enabled($entity_type_id)) { + if ($this->contentTranslationManager->isEnabled($entity_type_id)) { $permission["translate $entity_type_id"] = [ 'title' => $this->t('Translate @entity_label', $t_args), ]; diff --git a/core/modules/content_translation/src/Tests/ContentTranslationContextualLinksTest.php b/core/modules/content_translation/src/Tests/ContentTranslationContextualLinksTest.php index 3172827..1dfec07 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationContextualLinksTest.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationContextualLinksTest.php @@ -74,7 +74,7 @@ protected function setUp() { // Enable translation for the current entity type and ensure the change is // picked up. - content_translation_set_config('node', $this->bundle, 'enabled', TRUE); + \Drupal::service('content_translation.manager')->setEnabled('node', $this->bundle, TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedBundles(); \Drupal::service('router.builder')->rebuild(); diff --git a/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php b/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php index a2620ec..0b2102d 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationSettingsTest.php @@ -227,7 +227,7 @@ protected function assertSettings($entity_type, $bundle, $enabled, $edit) { $args = array('@entity_type' => $entity_type, '@bundle' => $bundle, '@enabled' => $enabled ? 'enabled' : 'disabled'); $message = format_string('Translation for entity @entity_type (@bundle) is @enabled.', $args); \Drupal::entityManager()->clearCachedDefinitions(); - return $this->assertEqual(content_translation_enabled($entity_type, $bundle), $enabled, $message); + return $this->assertEqual(\Drupal::service('content_translation.manager')->isEnabled($entity_type, $bundle), $enabled, $message); } /** diff --git a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php index e52036b..dda1ff2 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationTestBase.php @@ -163,7 +163,7 @@ protected function setupBundle() { protected function enableTranslation() { // Enable translation for the current entity type and ensure the change is // picked up. - content_translation_set_config($this->entityTypeId, $this->bundle, 'enabled', TRUE); + \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedDefinitions(); \Drupal::service('router.builder')->rebuild(); diff --git a/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php index 2c31c1e..8480073 100644 --- a/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php +++ b/core/modules/entity_reference/src/Tests/EntityReferenceFieldTranslatedReferenceViewTest.php @@ -166,8 +166,8 @@ protected function createContent() { protected function enableTranslation() { // Enable translation for the entity types and ensure the change is picked // up. - content_translation_set_config($this->testEntityTypeName, $this->referrerType->id(), 'enabled', TRUE); - content_translation_set_config($this->testEntityTypeName, $this->referencedType->id(), 'enabled', TRUE); + \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referrerType->id(), TRUE); + \Drupal::service('content_translation.manager')->setEnabled($this->testEntityTypeName, $this->referencedType->id(), TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedDefinitions(); \Drupal::service('router.builder')->rebuild(); diff --git a/core/modules/language/config/schema/language.schema.yml b/core/modules/language/config/schema/language.schema.yml index 24025f2..d894eb6 100644 --- a/core/modules/language/config/schema/language.schema.yml +++ b/core/modules/language/config/schema/language.schema.yml @@ -124,7 +124,7 @@ language.content_settings.*.*: type: sequence label: 'Third party settings' sequence: - - type: content_settings.third_party.[%key] + - type: language.content_settings.third_party.[%key] condition.plugin.language: type: condition.plugin diff --git a/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php index f9feddf..fc3267c 100644 --- a/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php +++ b/core/modules/taxonomy/src/Tests/TermTranslationFieldViewTest.php @@ -139,8 +139,8 @@ protected function setUpNode() { protected function enableTranslation() { // Enable translation for the current entity type and ensure the change is // picked up. - content_translation_set_config('node', 'article', 'enabled', TRUE); - content_translation_set_config('taxonomy', $this->vocabulary->id(), 'enabled', TRUE); + \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE); + \Drupal::service('content_translation.manager')->setEnabled('taxonomy_vocabulary', $this->vocabulary->id(), TRUE); drupal_static_reset(); \Drupal::entityManager()->clearCachedDefinitions(); \Drupal::service('router.builder')->rebuild(); diff --git a/core/modules/taxonomy/src/Tests/Views/TaxonomyTermViewTest.php b/core/modules/taxonomy/src/Tests/Views/TaxonomyTermViewTest.php index fb7805a..5899705 100644 --- a/core/modules/taxonomy/src/Tests/Views/TaxonomyTermViewTest.php +++ b/core/modules/taxonomy/src/Tests/Views/TaxonomyTermViewTest.php @@ -96,7 +96,7 @@ public function testTaxonomyTermView() { $language->save(); // Enable translation for the article content type and ensure the change is // picked up. - content_translation_set_config('node', 'article', 'enabled', TRUE); + \Drupal::service('content_translation.manager')->setEnabled('node', 'article', TRUE); $roles = $this->admin_user->getRoles(TRUE); Role::load(reset($roles)) ->grantPermission('create content translations')