ParagraphsMigrateParagraphsFieldHandler.inc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * Class ParagraphsMigrateParagraphsFieldHandler
  4. *
  5. * Provides migrate field handler for paragraphs field.
  6. */
  7. class ParagraphsMigrateParagraphsFieldHandler extends MigrateSimpleFieldHandler {
  8. /**
  9. * @inheritdoc
  10. */
  11. public function __construct() {
  12. parent::__construct(array(
  13. 'value_key' => 'value',
  14. 'skip_empty' => TRUE,
  15. ));
  16. $this->registerTypes(array('paragraphs'));
  17. }
  18. /**
  19. * Provide additional fields for migration.
  20. *
  21. * @param $type
  22. * @param $instance
  23. * @param null $migration
  24. *
  25. * @return array
  26. */
  27. public function fields($type, $instance, $migration = NULL) {
  28. return array(
  29. 'revision_id' => t('Option: Provide optional revision id.'),
  30. );
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. protected function notNull($value) {
  36. return !is_null($value) && $value !== FALSE;
  37. }
  38. /**
  39. * @inheritdoc
  40. */
  41. public function prepare($entity, array $field_info, array $instance, array $values) {
  42. $arguments = array();
  43. if (isset($values['arguments'])) {
  44. $arguments = $values['arguments'];
  45. unset($values['arguments']);
  46. }
  47. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  48. // Let the derived class skip empty values.
  49. if ($this->skipEmpty) {
  50. $values = array_filter($values, array($this, 'notNull'));
  51. }
  52. // Do not proceed if we got no values.
  53. if (empty($values)) {
  54. return NULL;
  55. }
  56. $revision_ids = $this->getRevisionIds($values, $arguments);
  57. // Setup the Field API array for saving.
  58. $delta = 0;
  59. foreach ($values as $value) {
  60. if (is_array($language)) {
  61. $current_language = $language[$delta];
  62. }
  63. else {
  64. $current_language = $language;
  65. }
  66. $return[$current_language][] = array(
  67. $this->fieldValueKey => $value,
  68. 'revision_id' => $revision_ids[$delta],
  69. );
  70. $delta++;
  71. }
  72. return isset($return) ? $return : NULL;
  73. }
  74. /**
  75. * Helper to get set of revision ids for import.
  76. *
  77. * @param $values
  78. * @param $arguments
  79. */
  80. protected function getRevisionIds($values, $arguments) {
  81. $return = array();
  82. if (!isset($arguments['revision_id'])) {
  83. $arguments['revision_id'] = array();
  84. }
  85. elseif (!is_array($arguments['revision_id'])) {
  86. $arguments['revision_id'] = array($arguments['revision_id']);
  87. }
  88. $revision_ids = db_select('paragraphs_item', 'p')
  89. ->fields('p', array('item_id', 'revision_id'))
  90. ->condition('item_id', $values)
  91. ->execute()
  92. ->fetchAllKeyed();
  93. foreach ($values as $delta => $item_id) {
  94. // Get revision ID provided by the migration.
  95. if (!empty($arguments['revision_id'][$delta])) {
  96. $return[$delta] = $arguments['revision_id'][$delta];
  97. }
  98. // Provide latest revision id.
  99. else {
  100. $return[$delta] = $revision_ids[$item_id];
  101. }
  102. }
  103. return $return;
  104. }
  105. }