Commit be4d3c53 authored by catch's avatar catch

Issue #2073467 by maxocub, Jo Fitzgerald, pobster, masipila, plach, heddn,...

Issue #2073467 by maxocub, Jo Fitzgerald, pobster, masipila, plach, heddn, phenaproxima: Migrate Drupal 7 Entity Translation settings to Drupal 8
parent 522631b0
id: d7_entity_translation_settings
label: Drupal 7 Entity Translation settings
migration_tags:
- Drupal 7
- Configuration
source:
plugin: d7_entity_translation_settings
process:
id: id
target_entity_type_id: target_entity_type_id
target_bundle: target_bundle
default_langcode:
plugin: static_map
source: default_langcode
bypass: true
map:
xx-et-default: site_default
xx-et-current: current_interface
xx-et-author: authors_default
language_alterable: language_alterable
third_party_settings/content_translation/enabled:
plugin: default_value
default_value: true
third_party_settings/content_translation/bundle_settings/untranslatable_fields_hide: untranslatable_fields_hide
destination:
plugin: entity:language_content_settings
content_translation_update_definitions:
- comment
- node
- taxonomy_term
- user
migration_dependencies:
optional:
- d7_comment_type
- d7_node_type
- d7_taxonomy_vocabulary
<?php
namespace Drupal\content_translation\Plugin\migrate\source\d7;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Drupal 7 Entity Translation settings from variables.
*
* @MigrateSource(
* id = "d7_entity_translation_settings",
* source_module = "entity_translation"
* )
*/
class EntityTranslationSettings extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
// Query all meaningful variables for entity translation.
$query = $this->select('variable', 'v')
->fields('v', ['name', 'value']);
$condition = $query->orConditionGroup()
// The 'entity_translation_entity_types' variable tells us which entity
// type uses entity translation.
->condition('name', 'entity_translation_entity_types')
// The 'entity_translation_taxonomy' variable tells us which taxonomy
// vocabulary uses entity_translation.
->condition('name', 'entity_translation_taxonomy')
// The 'entity_translation_settings_%' variables give us the entity
// translation settings for each entity type and each bundle.
->condition('name', 'entity_translation_settings_%', 'LIKE')
// The 'language_content_type_%' variables tells us which node type and
// which comment type uses entity translation.
->condition('name', 'language_content_type_%', 'LIKE');
$query->condition($condition);
return $query;
}
/**
* {@inheritdoc}
*/
protected function initializeIterator() {
$results = array_map('unserialize', $this->prepareQuery()->execute()->fetchAllKeyed());
$rows = [];
// Find out which entity type uses entity translation by looking at the
// 'entity_translation_entity_types' variable.
$entity_types = array_filter($results['entity_translation_entity_types']);
// If no entity type uses entity translation, there's nothing to do.
if (empty($entity_types)) {
return new \ArrayIterator($rows);
}
// Find out which node type uses entity translation by looking at the
// 'language_content_type_%' variables.
$node_types = [];
foreach ($results as $name => $value) {
if (preg_match('/^language_content_type_(.+)$/', $name, $matches) && (int) $value === 4) {
$node_types[] = $matches[1];
}
}
// Find out which vocabulary uses entity translation by looking at the
// 'entity_translation_taxonomy' variable.
$vocabularies = array_keys(array_filter($results['entity_translation_taxonomy']));
if (in_array('node', $entity_types, TRUE) && !empty($node_types)) {
// For each node type that uses entity translation, check if a
// settings variable exists for that node type, otherwise use default
// values.
foreach ($node_types as $node_type) {
$settings = isset($results['entity_translation_settings_node__' . $node_type]) ? $results['entity_translation_settings_node__' . $node_type] : [];
$rows[] = [
'id' => 'node.' . $node_type,
'target_entity_type_id' => 'node',
'target_bundle' => $node_type,
'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'und',
// The Drupal 7 'hide_language_selector' configuration has become
// 'language_alterable' in Drupal 8 so we need to negate the value we
// receive from the source. The Drupal 7 'hide_language_selector'
// default value for the node entity type was FALSE so in Drupal 8 it
// should be set to TRUE, unlike the other entity types for which
// it's the opposite.
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : TRUE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('comment', $entity_types, TRUE) && !empty($node_types)) {
// A comment type uses entity translation if the associated node type
// uses it. So, for each node type that uses entity translation, check
// if a settings variable exists for that comment type, otherwise use
// default values.
foreach ($node_types as $node_type) {
$settings = isset($results['entity_translation_settings_comment__comment_node_' . $node_type]) ? $results['entity_translation_settings_comment__comment_node_' . $node_type] : [];
// Forum uses a hardcoded comment type name, so make sure we use it
// when we're dealing with forum comment type.
$bundle = $node_type == 'forum' ? 'comment_forum' : 'comment_node_' . $node_type;
$rows[] = [
'id' => 'comment.' . $bundle,
'target_entity_type_id' => 'comment',
'target_bundle' => $bundle,
'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-current',
// The Drupal 7 'hide_language_selector' configuration has become
// 'language_alterable' in Drupal 8 so we need to negate the value we
// receive from the source. The Drupal 7 'hide_language_selector'
// default value for the comment entity type was TRUE so in Drupal 8
// it should be set to FALSE.
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('taxonomy_term', $entity_types, TRUE) && !empty($vocabularies)) {
// For each vocabulary that uses entity translation, check if a
// settings variable exists for that vocabulary, otherwise use default
// values.
foreach ($vocabularies as $vocabulary) {
$settings = isset($results['entity_translation_settings_taxonomy_term__' . $vocabulary]) ? $results['entity_translation_settings_taxonomy_term__' . $vocabulary] : [];
$rows[] = [
'id' => 'taxonomy_term.' . $vocabulary,
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => $vocabulary,
'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-default',
// The Drupal 7 'hide_language_selector' configuration has become
// 'language_alterable' in Drupal 8 so we need to negate the value we
// receive from the source. The Drupal 7 'hide_language_selector'
// default value for the taxonomy_term entity type was TRUE so in
// Drupal 8 it should be set to FALSE.
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
}
if (in_array('user', $entity_types, TRUE)) {
// User entity type is not bundleable. Check if a settings variable
// exists, otherwise use default values.
$settings = isset($results['entity_translation_settings_user__user']) ? $results['entity_translation_settings_user__user'] : [];
$rows[] = [
'id' => 'user.user',
'target_entity_type_id' => 'user',
'target_bundle' => 'user',
'default_langcode' => isset($settings['default_language']) ? $settings['default_language'] : 'xx-et-default',
// The Drupal 7 'hide_language_selector' configuration has become
// 'language_alterable' in Drupal 8 so we need to negate the value we
// receive from the source. The Drupal 7 'hide_language_selector'
// default value for the user entity type was TRUE so in Drupal 8 it
// should be set to FALSE.
'language_alterable' => isset($settings['hide_language_selector']) ? (bool) !$settings['hide_language_selector'] : FALSE,
'untranslatable_fields_hide' => isset($settings['shared_fields_original_only']) ? (bool) $settings['shared_fields_original_only'] : FALSE,
];
}
return new \ArrayIterator($rows);
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'id' => $this->t('The configuration ID'),
'target_entity_type_id' => $this->t('The target entity type ID'),
'target_bundle' => $this->t('The target bundle'),
'default_langcode' => $this->t('The default language'),
'language_alterable' => $this->t('Whether to show language selector on create and edit pages'),
'untranslatable_fields_hide' => $this->t('Whether to hide non translatable fields on translation forms'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['id']['type'] = 'string';
return $ids;
}
/**
* {@inheritdoc}
*/
public function count($refresh = FALSE) {
// Since the number of variables we fetch with query() does not match the
// actual number of rows generated by initializeIterator(), we need to
// override count() to return the correct count.
return (int) $this->initializeIterator()->count();
}
}
<?php
namespace Drupal\Tests\content_translation\Kernel\Migrate\d7;
use Drupal\Core\Language\LanguageInterface;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests the migration of entity translation settings.
*
* @group migrate_drupal_7
*/
class MigrateEntityTranslationSettingsTest extends MigrateDrupal7TestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'comment',
'content_translation',
'language',
'menu_ui',
'node',
'taxonomy',
'text',
'user',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig([
'comment',
'content_translation',
'node',
'taxonomy',
'user',
]);
$this->installEntitySchema('comment');
$this->installEntitySchema('node');
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('user');
$this->executeMigrations([
'd7_comment_type',
'd7_node_type',
'd7_taxonomy_vocabulary',
'd7_entity_translation_settings',
]);
}
/**
* Tests entity translation settings migration.
*/
public function testEntityTranslationSettingsMigration() {
// Tests 'comment_node_test_content_type' entity translation settings.
$config = $this->config('language.content_settings.comment.comment_node_test_content_type');
$this->assertSame($config->get('target_entity_type_id'), 'comment');
$this->assertSame($config->get('target_bundle'), 'comment_node_test_content_type');
$this->assertSame($config->get('default_langcode'), 'current_interface');
$this->assertFalse((bool) $config->get('language_alterable'));
$this->assertTrue((bool) $config->get('third_party_settings.content_translation.enabled'));
$this->assertFalse((bool) $config->get('third_party_settings.content_translation.bundle_settings.untranslatable_fields_hide'));
// Tests 'test_content_type' entity translation settings.
$config = $this->config('language.content_settings.node.test_content_type');
$this->assertSame($config->get('target_entity_type_id'), 'node');
$this->assertSame($config->get('target_bundle'), 'test_content_type');
$this->assertSame($config->get('default_langcode'), LanguageInterface::LANGCODE_NOT_SPECIFIED);
$this->assertTrue((bool) $config->get('language_alterable'));
$this->assertTrue((bool) $config->get('third_party_settings.content_translation.enabled'));
$this->assertFalse((bool) $config->get('third_party_settings.content_translation.bundle_settings.untranslatable_fields_hide'));
// Tests 'test_vocabulary' entity translation settings.
$config = $this->config('language.content_settings.taxonomy_term.test_vocabulary');
$this->assertSame($config->get('target_entity_type_id'), 'taxonomy_term');
$this->assertSame($config->get('target_bundle'), 'test_vocabulary');
$this->assertSame($config->get('default_langcode'), LanguageInterface::LANGCODE_SITE_DEFAULT);
$this->assertFalse((bool) $config->get('language_alterable'));
$this->assertTrue((bool) $config->get('third_party_settings.content_translation.enabled'));
$this->assertFalse((bool) $config->get('third_party_settings.content_translation.bundle_settings.untranslatable_fields_hide'));
// Tests 'user' entity translation settings.
$config = $this->config('language.content_settings.user.user');
$this->assertSame($config->get('target_entity_type_id'), 'user');
$this->assertSame($config->get('target_bundle'), 'user');
$this->assertSame($config->get('default_langcode'), LanguageInterface::LANGCODE_SITE_DEFAULT);
$this->assertFalse((bool) $config->get('language_alterable'));
$this->assertTrue((bool) $config->get('third_party_settings.content_translation.enabled'));
$this->assertFalse((bool) $config->get('third_party_settings.content_translation.bundle_settings.untranslatable_fields_hide'));
}
}
<?php
namespace Drupal\Tests\content_translation\Kernel\Plugin\migrate\source\d7;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests entity translation settings source plugin.
*
* @covers \Drupal\content_translation\Plugin\migrate\source\d7\EntityTranslationSettings
*
* @group content_translation
*/
class EntityTranslationSettingsTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'content_translation',
'language',
'migrate_drupal',
];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// Source data when there's no entity type that uses entity translation.
$tests[0]['source_data']['variable'] = [
[
'name' => 'entity_translation_entity_types',
'value' => 'a:4:{s:7:"comment";i:0;s:4:"node";i:0;s:13:"taxonomy_term";i:0;s:4:"user";i:0;}',
],
];
// Source data when there's no bundle settings variables.
$tests[1]['source_data']['variable'] = [
[
'name' => 'entity_translation_entity_types',
'value' => 'a:4:{s:7:"comment";s:7:"comment";s:4:"node";s:4:"node";s:13:"taxonomy_term";s:13:"taxonomy_term";s:4:"user";s:4:"user";}',
],
[
'name' => 'entity_translation_taxonomy',
'value' => 'a:3:{s:6:"forums";b:1;s:4:"tags";b:1;s:4:"test";b:0;}',
],
[
'name' => 'language_content_type_article',
'value' => 's:1:"2";',
],
[
'name' => 'language_content_type_forum',
'value' => 's:1:"4";',
],
[
'name' => 'language_content_type_page',
'value' => 's:1:"4";',
],
];
// Source data when there's bundle settings variables.
$tests[2]['source_data']['variable'] = [
[
'name' => 'entity_translation_entity_types',
'value' => 'a:4:{s:7:"comment";s:7:"comment";s:4:"node";s:4:"node";s:13:"taxonomy_term";s:13:"taxonomy_term";s:4:"user";s:4:"user";}',
],
[
'name' => 'entity_translation_settings_comment__comment_node_forum',
'value' => 'a:5:{s:16:"default_language";s:12:"xx-et-author";s:22:"hide_language_selector";i:1;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:0;}',
],
[
'name' => 'entity_translation_settings_comment__comment_node_page',
'value' => 'a:5:{s:16:"default_language";s:12:"xx-et-author";s:22:"hide_language_selector";i:0;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:1;}',
],
[
'name' => 'entity_translation_settings_node__forum',
'value' => 'a:5:{s:16:"default_language";s:12:"xx-et-author";s:22:"hide_language_selector";i:0;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:0;}',
],
[
'name' => 'entity_translation_settings_node__page',
'value' => 'a:5:{s:16:"default_language";s:13:"xx-et-default";s:22:"hide_language_selector";i:1;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:1;}',
],
[
'name' => 'entity_translation_settings_taxonomy_term__forums',
'value' => 'a:5:{s:16:"default_language";s:13:"xx-et-current";s:22:"hide_language_selector";i:0;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:1;}',
],
[
'name' => 'entity_translation_settings_taxonomy_term__tags',
'value' => 'a:5:{s:16:"default_language";s:13:"xx-et-current";s:22:"hide_language_selector";i:1;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:0;}',
],
[
'name' => 'entity_translation_settings_user__user',
'value' => 'a:5:{s:16:"default_language";s:12:"xx-et-author";s:22:"hide_language_selector";i:1;s:21:"exclude_language_none";i:0;s:13:"lock_language";i:0;s:27:"shared_fields_original_only";i:1;}',
],
[
'name' => 'entity_translation_taxonomy',
'value' => 'a:3:{s:6:"forums";b:1;s:4:"tags";b:1;s:4:"test";b:0;}',
],
[
'name' => 'language_content_type_article',
'value' => 's:1:"2";',
],
[
'name' => 'language_content_type_forum',
'value' => 's:1:"4";',
],
[
'name' => 'language_content_type_page',
'value' => 's:1:"4";',
],
];
// Expected data when there's no entity type that uses entity translation.
$tests[0]['expected_data'] = [];
// Expected data when there's no bundle settings variables.
$tests[1]['expected_data'] = [
[
'id' => 'node.forum',
'target_entity_type_id' => 'node',
'target_bundle' => 'forum',
'default_langcode' => 'und',
'language_alterable' => TRUE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'node.page',
'target_entity_type_id' => 'node',
'target_bundle' => 'page',
'default_langcode' => 'und',
'language_alterable' => TRUE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'comment.comment_forum',
'target_entity_type_id' => 'comment',
'target_bundle' => 'comment_forum',
'default_langcode' => 'xx-et-current',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'comment.comment_node_page',
'target_entity_type_id' => 'comment',
'target_bundle' => 'comment_node_page',
'default_langcode' => 'xx-et-current',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'taxonomy_term.forums',
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => 'forums',
'default_langcode' => 'xx-et-default',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'taxonomy_term.tags',
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => 'tags',
'default_langcode' => 'xx-et-default',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'user.user',
'target_entity_type_id' => 'user',
'target_bundle' => 'user',
'default_langcode' => 'xx-et-default',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
];
// Expected data when there's bundle settings variables.
$tests[2]['expected_data'] = [
[
'id' => 'node.forum',
'target_entity_type_id' => 'node',
'target_bundle' => 'forum',
'default_langcode' => 'xx-et-author',
'language_alterable' => TRUE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'node.page',
'target_entity_type_id' => 'node',
'target_bundle' => 'page',
'default_langcode' => 'xx-et-default',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => TRUE,
],
[
'id' => 'comment.comment_forum',
'target_entity_type_id' => 'comment',
'target_bundle' => 'comment_forum',
'default_langcode' => 'xx-et-author',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'comment.comment_node_page',
'target_entity_type_id' => 'comment',
'target_bundle' => 'comment_node_page',
'default_langcode' => 'xx-et-author',
'language_alterable' => TRUE,
'untranslatable_fields_hide' => TRUE,
],
[
'id' => 'taxonomy_term.forums',
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => 'forums',
'default_langcode' => 'xx-et-current',
'language_alterable' => TRUE,
'untranslatable_fields_hide' => TRUE,
],
[
'id' => 'taxonomy_term.tags',
'target_entity_type_id' => 'taxonomy_term',
'target_bundle' => 'tags',
'default_langcode' => 'xx-et-current',
'language_alterable' => FALSE,
'untranslatable_fields_hide' => FALSE,
],
[
'id' => 'user.user',
'target_entity_type_id' => 'user',
'target_bundle' => 'user',
'default_langcode' => 'xx-et-author',