BlockCustomTranslation.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace Drupal\block_content\Plugin\migrate\source\d7;
  3. use Drupal\migrate\Row;
  4. use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;
  5. use Drupal\content_translation\Plugin\migrate\source\I18nQueryTrait;
  6. /**
  7. * Gets Drupal 7 custom block translation from database.
  8. *
  9. * @MigrateSource(
  10. * id = "d7_block_custom_translation",
  11. * source_module = "i18n_block"
  12. * )
  13. */
  14. class BlockCustomTranslation extends DrupalSqlBase {
  15. use I18nQueryTrait;
  16. /**
  17. * Drupal 7 table names.
  18. */
  19. const CUSTOM_BLOCK_TABLE = 'block_custom';
  20. const I18N_STRING_TABLE = 'i18n_string';
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function query() {
  25. // Build a query based on blockCustomTable table where each row has the
  26. // translation for only one property, either title or description. The
  27. // method prepareRow() is then used to obtain the translation for the
  28. // other property.
  29. $query = $this->select(static::CUSTOM_BLOCK_TABLE, 'b')
  30. ->fields('b', ['bid', 'format', 'body'])
  31. ->fields('i18n', ['property'])
  32. ->fields('lt', ['lid', 'translation', 'language'])
  33. ->orderBy('b.bid');
  34. // Use 'title' for the info field to match the property name in
  35. // i18nStringTable.
  36. $query->addField('b', 'info', 'title');
  37. // Add in the property, which is either title or body. Cast the bid to text
  38. // so PostgreSQL can make the join.
  39. $query->leftJoin(static::I18N_STRING_TABLE, 'i18n', 'i18n.objectid = CAST(b.bid AS CHAR(255))');
  40. $query->condition('i18n.type', 'block');
  41. // Add in the translation for the property.
  42. $query->innerJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
  43. return $query;
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. public function prepareRow(Row $row) {
  49. parent::prepareRow($row);
  50. // Set the i18n string table for use in I18nQueryTrait.
  51. $this->i18nStringTable = static::I18N_STRING_TABLE;
  52. // Save the translation for this property.
  53. $property_in_row = $row->getSourceProperty('property');
  54. // Get the translation for the property not already in the row and save it
  55. // in the row.
  56. $property_not_in_row = ($property_in_row === 'title') ? 'body' : 'title';
  57. return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'bid', $this->idMap);
  58. }
  59. /**
  60. * {@inheritdoc}
  61. */
  62. public function fields() {
  63. return [
  64. 'bid' => $this->t('The block numeric identifier.'),
  65. 'format' => $this->t('Input format of the custom block/box content.'),
  66. 'lid' => $this->t('i18n_string table id'),
  67. 'language' => $this->t('Language for this field.'),
  68. 'property' => $this->t('Block property'),
  69. 'translation' => $this->t('The translation of the value of "property".'),
  70. 'title' => $this->t('Block title.'),
  71. 'title_translated' => $this->t('Block title translation.'),
  72. 'body' => $this->t('Block body.'),
  73. 'body_translated' => $this->t('Block body translation.'),
  74. ];
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getIds() {
  80. $ids['bid']['type'] = 'integer';
  81. $ids['bid']['alias'] = 'b';
  82. $ids['language']['type'] = 'string';
  83. return $ids;
  84. }
  85. }