BlockCustomTranslation.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = "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. ->isNotNull('lt.lid');
  35. // Use 'title' for the info field to match the property name in
  36. // i18nStringTable.
  37. $query->addField('b', 'info', 'title');
  38. // Add in the property, which is either title or body. Cast the bid to text
  39. // so PostgreSQL can make the join.
  40. $query->leftJoin(static::I18N_STRING_TABLE, 'i18n', 'i18n.objectid = CAST(b.bid as CHAR(255))');
  41. $query->condition('i18n.type', 'block');
  42. // Add in the translation for the property.
  43. $query->leftJoin('locales_target', 'lt', 'lt.lid = i18n.lid');
  44. return $query;
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function prepareRow(Row $row) {
  50. parent::prepareRow($row);
  51. // Set the i18n string table for use in I18nQueryTrait.
  52. $this->i18nStringTable = static::I18N_STRING_TABLE;
  53. // Save the translation for this property.
  54. $property_in_row = $row->getSourceProperty('property');
  55. // Get the translation for the property not already in the row and save it
  56. // in the row.
  57. $property_not_in_row = ($property_in_row === 'title') ? 'body' : 'title';
  58. return $this->getPropertyNotInRowTranslation($row, $property_not_in_row, 'bid', $this->idMap);
  59. }
  60. /**
  61. * {@inheritdoc}
  62. */
  63. public function fields() {
  64. return [
  65. 'bid' => $this->t('The block numeric identifier.'),
  66. 'format' => $this->t('Input format of the custom block/box content.'),
  67. 'lid' => $this->t('i18n_string table id'),
  68. 'language' => $this->t('Language for this field.'),
  69. 'property' => $this->t('Block property'),
  70. 'translation' => $this->t('The translation of the value of "property".'),
  71. 'title' => $this->t('Block title.'),
  72. 'title_translated' => $this->t('Block title translation.'),
  73. 'body' => $this->t('Block body.'),
  74. 'body_translated' => $this->t('Block body translation.'),
  75. ];
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getIds() {
  81. $ids['bid']['type'] = 'integer';
  82. $ids['bid']['alias'] = 'b';
  83. $ids['language']['type'] = 'string';
  84. return $ids;
  85. }
  86. }