Commit a79df573 authored by Gábor Hojtsy's avatar Gábor Hojtsy

Issue #2909444 by quietone, Jo Fitzgerald, rakesh.gectcr, maxocub,...

Issue #2909444 by quietone, Jo Fitzgerald, rakesh.gectcr, maxocub, phenaproxima, masipila, Gábor Hojtsy, catch: Migrate D6 i18n custom blocks (boxes)
parent 314b28c5
<?php
namespace Drupal\block_content\Plugin\migrate\source\d6;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
/**
* Gets Drupal 6 i18n custom block translations from database.
*
* @MigrateSource(
* id = "d6_box_translation",
* source_module = "i18nblocks"
* )
*/
class BoxTranslation extends DrupalSqlBase {
/**
* {@inheritdoc}
*/
public function query() {
// Build a query based on i18n_strings table where each row has the
// translation for only one property, either title or description. The
// method prepareRow() is then used to obtain the translation for the
// other property.
$query = $this->select('boxes', 'b')
->fields('b', ['bid', 'format', 'body'])
->fields('i18n', ['property'])
->fields('lt', ['lid', 'translation', 'language'])
->orderBy('b.bid')
->isNotNull('lt.lid');
// Use 'title' for the info field to match the property name in the
// i18n_strings table.
$query->addField('b', 'info', 'title');
// Add in the property, which is either title or body. Cast the bid to text
// so PostgreSQL can make the join.
$query->leftJoin('i18n_strings', 'i18n', 'i18n.objectid = CAST(b.bid as CHAR(255))');
$query->condition('i18n.type', 'block');
// Add in the translation for the property.
$query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
return $query;
}
/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
$language = $row->getSourceProperty('language');
$bid = $row->getSourceProperty('bid');
// If this row has been migrated it is a duplicate then skip it.
if ($this->idMap->lookupDestinationIds(['bid' => $bid, 'language' => $language])) {
return FALSE;
}
// Save the translation for this property.
$property = $row->getSourceProperty('property');
$row->setSourceProperty($property . '_translated', $row->getSourceProperty('translation'));
// Get the translation for the property not already in the row.
$translation = ($property === 'title') ? 'body' : 'title';
$query = $this->select('i18n_strings', 'i18n')
->fields('i18n', ['lid'])
->condition('i18n.property', $translation)
->condition('i18n.objectid', $bid);
$query->leftJoin('locales_target', 'lt', 'i18n.lid = lt.lid');
$query->condition('lt.language', $language)
->addField('lt', 'translation');
$results = $query->execute()->fetchAssoc();
if (!$results) {
$row->setSourceProperty($translation . '_translated', NULL);
}
else {
$row->setSourceProperty($translation . '_translated', $results['translation']);
}
}
/**
* {@inheritdoc}
*/
public function fields() {
return [
'bid' => $this->t('The block numeric identifier.'),
'format' => $this->t('Input format of the custom block/box content.'),
'lid' => $this->t('i18n_string table id'),
'language' => $this->t('Language for this field.'),
'property' => $this->t('Block property'),
'translation' => $this->t('The translation of the value of "property".'),
'title' => $this->t('Block title.'),
'title_translated' => $this->t('Block title translation.'),
'body' => $this->t('Block body.'),
'body_translated' => $this->t('Block body translation.'),
];
}
/**
* {@inheritdoc}
*/
public function getIds() {
$ids['bid']['type'] = 'integer';
$ids['bid']['alias'] = 'b';
$ids['language']['type'] = 'string';
return $ids;
}
}
<?php
namespace Drupal\Tests\block_content\Kernel\Migrate\d6;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* Tests migration of i18n custom block strings.
*
* @group migrate_drupal_6
*/
class MigrateCustomBlockContentTranslationTest extends MigrateDrupal6TestBase {
/**
* {@inheritdoc}
*/
public static $modules = [
'block_content',
'content_translation',
'language',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installConfig(['block_content']);
$this->installEntitySchema('block_content');
$this->executeMigrations([
'language',
'd6_filter_format',
'block_content_type',
'block_content_body_field',
'd6_custom_block',
'd6_custom_block_translation',
]);
}
/**
* Tests the Drupal 6 i18n custom block strings to Drupal 8 migration.
*/
public function testCustomBlockContentTranslation() {
/** @var \Drupal\block_content\Entity\BlockContent $block */
$block = BlockContent::load(1)->getTranslation('fr');
$this->assertSame('fr - Static Block', $block->label());
$this->assertGreaterThanOrEqual(REQUEST_TIME, $block->getChangedTime());
$this->assertLessThanOrEqual(time(), $block->getChangedTime());
$this->assertSame('fr', $block->language()->getId());
$this->assertSame('<h3>fr - My first custom block body</h3>', $block->body->value);
$this->assertSame('full_html', $block->body->format);
$block = $block->getTranslation('zu');
$this->assertSame('My block 1', $block->label());
$this->assertGreaterThanOrEqual(REQUEST_TIME, $block->getChangedTime());
$this->assertLessThanOrEqual(time(), $block->getChangedTime());
$this->assertSame('zu', $block->language()->getId());
$this->assertSame('<h3>zu - My first custom block body</h3>', $block->body->value);
$this->assertSame('full_html', $block->body->format);
$block = BlockContent::load(2)->getTranslation('fr');
$this->assertSame('Encore un bloc statique', $block->label());
$this->assertGreaterThanOrEqual(REQUEST_TIME, $block->getChangedTime());
$this->assertLessThanOrEqual(time(), $block->getChangedTime());
$this->assertSame('fr', $block->language()->getId());
$this->assertSame('Nom de vocabulaire beaucoup plus long que trente-deux caractères', $block->body->value);
$this->assertSame('full_html', $block->body->format);
}
}
<?php
namespace Drupal\Tests\block_content\Kernel\Plugin\migrate\source\d6;
use Drupal\Tests\migrate\Kernel\MigrateSqlSourceTestBase;
/**
* Tests i18n custom block translations source plugin.
*
* @covers \Drupal\block_content\Plugin\migrate\source\d6\BoxTranslation
*
* @group content_translation
*/
class BoxTranslationTest extends MigrateSqlSourceTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['block_content', 'migrate_drupal'];
/**
* {@inheritdoc}
*/
public function providerSource() {
$tests = [];
// The source data.
$tests[0]['database']['boxes'] = [
[
'bid' => 1,
'body' => 'box 1 body',
'info' => 'box 1 title',
'format' => '2',
],
[
'bid' => 2,
'body' => 'box 2 body',
'info' => 'box 2 title',
'format' => '2',
],
];
$tests[0]['database']['i18n_strings'] = [
[
'lid' => 1,
'objectid' => 1,
'type' => 'block',
'property' => 'title',
'objectindex' => 1,
'format' => 0,
],
[
'lid' => 2,
'objectid' => 1,
'type' => 'block',
'property' => 'body',
'objectindex' => 1,
'format' => 0,
],
[
'lid' => 3,
'objectid' => 2,
'type' => 'block',
'property' => 'body',
'objectindex' => 2,
'format' => 2,
],
];
$tests[0]['database']['locales_target'] = [
[
'lid' => 1,
'language' => 'fr',
'translation' => 'fr - title translation',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
[
'lid' => 2,
'language' => 'fr',
'translation' => 'fr - body translation',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
[
'lid' => 3,
'language' => 'zu',
'translation' => 'zu - body translation',
'plid' => 0,
'plural' => 0,
'i18n_status' => 0,
],
];
$tests[0]['expected_results'] = [
[
'lid' => '1',
'property' => 'title',
'language' => 'fr',
'translation' => 'fr - title translation',
'bid' => '1',
'format' => '2',
'title_translated' => 'fr - title translation',
'body_translated' => 'fr - body translation',
'title' => 'box 1 title',
'body' => 'box 1 body',
],
[
'lid' => '2',
'property' => 'body',
'language' => 'fr',
'translation' => 'fr - body translation',
'bid' => '1',
'format' => '2',
'title_translated' => 'fr - title translation',
'body_translated' => 'fr - body translation',
'title' => 'box 1 title',
'body' => 'box 1 body',
],
[
'lid' => '3',
'property' => 'body',
'language' => 'zu',
'translation' => 'zu - body translation',
'bid' => '2',
'format' => '2',
'title_translated' => NULL,
'body_translated' => 'zu - body translation',
'title' => 'box 2 title',
'body' => 'box 2 body',
],
];
return $tests;
}
}
id: d6_custom_block_translation
label: Custom block translations
migration_tags:
- Drupal 6
- Content
source:
plugin: d6_box_translation
process:
id:
plugin: migration_lookup
migration: d6_custom_block
source:
- bid
langcode: language
info:
-
plugin: callback
source:
- title_translated
- title
callable: array_filter
-
plugin: callback
callable: current
'body/value':
-
plugin: callback
source:
- body_translated
- body
callable: array_filter
-
plugin: callback
callable: current
'body/format':
plugin: migration_lookup
migration: d6_filter_format
source: format
destination:
plugin: entity:block_content
no_stub: true
translations: true
destination_module: content_translation
migration_dependencies:
required:
- d6_filter_format
- block_content_body_field
- d6_custom_block
- language
......@@ -132,6 +132,7 @@ protected function getAvailablePaths() {
'filefield',
'filter',
'forum',
'i18nblocks',
'i18nmenu',
'i18nblocks',
'i18ntaxonomy',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment