View source
<?php
namespace Drupal\block_content\Plugin\migrate\source\d7;
use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
use Drupal\content_translation\Plugin\migrate\source\I18nQueryTrait;
class BlockCustomTranslation extends DrupalSqlBase {
use I18nQueryTrait;
const CUSTOM_BLOCK_TABLE = 'block_custom';
const I18N_STRING_TABLE = 'i18n_string';
public function query() {
$query = $this
->select(static::CUSTOM_BLOCK_TABLE, 'b')
->fields('b', [
'bid',
'format',
'body',
])
->fields('i18n', [
'property',
])
->fields('lt', [
'lid',
'translation',
'language',
])
->orderBy('b.bid');
$query
->addField('b', 'info', 'title');
$query
->leftJoin(static::I18N_STRING_TABLE, 'i18n', '[i18n].[objectid] = CAST([b].[bid] AS CHAR(255))');
$query
->condition('i18n.type', 'block');
$query
->innerJoin('locales_target', 'lt', '[lt].[lid] = [i18n].[lid]');
return $query;
}
public function prepareRow(Row $row) {
if (!parent::prepareRow($row)) {
return FALSE;
}
$this->i18nStringTable = static::I18N_STRING_TABLE;
$property_in_row = $row
->getSourceProperty('property');
$property_not_in_row = $property_in_row === 'title' ? 'body' : 'title';
return $this
->getPropertyNotInRowTranslation($row, $property_not_in_row, 'bid', $this->idMap);
}
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.'),
];
}
public function getIds() {
$ids['bid']['type'] = 'integer';
$ids['bid']['alias'] = 'b';
$ids['language']['type'] = 'string';
return $ids;
}
}