MigrateDestinationParagraphsItem.inc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. /**
  3. * @file
  4. * Provides basic class for importing Paragraphs via Migrate API.
  5. */
  6. /**
  7. * Destination class implementing migration into field_collection.
  8. */
  9. class MigrateDestinationParagraphsItem extends MigrateDestinationEntity {
  10. /**
  11. * The bundle (node type, vocabulary, etc.) of the destination.
  12. *
  13. * @var string
  14. */
  15. protected $field_name;
  16. public function getFieldName() {
  17. return $this->field_name;
  18. }
  19. static public function getKeySchema() {
  20. return array(
  21. 'item_id' => array(
  22. 'type' => 'int',
  23. 'unsigned' => TRUE,
  24. 'not null' => TRUE,
  25. 'description' => 'ID of field collection item',
  26. ),
  27. );
  28. }
  29. /**
  30. * Basic initialization.
  31. *
  32. * @param array $options
  33. * (optional) Options applied to collections.
  34. */
  35. public function __construct($bundle, array $options = array()) {
  36. parent::__construct('paragraphs_item', $bundle, $options);
  37. $this->field_name = isset($options['field_name']) ? $options['field_name'] : '';
  38. }
  39. /**
  40. * Return an options array for node destinations.
  41. *
  42. * @param string $language
  43. * Default language for nodes created via this destination class.
  44. * @param string $text_format
  45. * Default text format for nodes created via this destination class.
  46. */
  47. static public function options($language, $text_format) {
  48. return compact('language', 'text_format');
  49. }
  50. /**
  51. * Returns a list of fields available to be mapped for the node type (bundle)
  52. *
  53. * @param Migration $migration
  54. * Optionally, the migration containing this destination.
  55. * @return array
  56. * Keys: machine names of the fields (to be passed to addFieldMapping)
  57. * Values: Human-friendly descriptions of the fields.
  58. */
  59. public function fields($migration = NULL) {
  60. $fields = array();
  61. $fields['field_name'] = t('Field name');
  62. $fields['archived'] = t('Archived status of the paragraph item');
  63. $fields += migrate_handler_invoke_all('Entity', 'fields', $this->entityType, $this->bundle, $migration);
  64. $fields += migrate_handler_invoke_all('ParagraphsItem', 'fields', $this->entityType, $this->bundle, $migration);
  65. return $fields;
  66. }
  67. /**
  68. * Delete a batch of nodes at once.
  69. *
  70. * @param $nids
  71. * Array of node IDs to be deleted.
  72. */
  73. public function bulkRollback(array $item_ids) {
  74. migrate_instrument_start('paragraphs_item_delete_multiple');
  75. $this->prepareRollback($item_ids);
  76. entity_delete_multiple('paragraphs_item', $item_ids);
  77. $this->completeRollback($item_ids);
  78. migrate_instrument_stop('paragraphs_item_delete_multiple');
  79. }
  80. /**
  81. * Import a single node.
  82. *
  83. * @param $node
  84. * Node object to build. Prefilled with any fields mapped in the Migration.
  85. * @param $row
  86. * Raw source data object - passed through to prepare/complete handlers.
  87. * @return array
  88. * Array of key fields (nid only in this case) of the node that was saved if
  89. * successful. FALSE on failure.
  90. */
  91. public function import(stdClass $paragraphs_item, stdClass $row) {
  92. $migration = Migration::currentMigration();
  93. // Updating previously-migrated content?
  94. if (isset($row->migrate_map_destid1)) {
  95. if (isset($paragraphs_item->item_id)) {
  96. if ($paragraphs_item->item_id != $row->migrate_map_destid1) {
  97. throw new MigrateException(t("Incoming item_id !item_id and map destination item_id !destid1 don't match",
  98. array('!item_id' => $paragraphs_item->item_id, '!destid1' => $row->migrate_map_destid1)));
  99. }
  100. }
  101. else {
  102. $paragraphs_item->item_id = $row->migrate_map_destid1;
  103. $paragraphs_item->is_new = FALSE;
  104. $paragraphs_item->is_new_revision = FALSE;
  105. // Get the existing revision_id so updates don't generate notices.
  106. $values = db_select('paragraphs_item', 'p')
  107. ->fields('p', array('revision_id'))
  108. ->condition('item_id', $paragraphs_item->item_id)
  109. ->execute()
  110. ->fetchAssoc();
  111. if (empty($values)) {
  112. throw new MigrateException(t("Incoming paragraphs ID !item_id no longer exists",
  113. array('!item_id' => $paragraphs_item->item_id)));
  114. }
  115. $paragraphs_item->revision_id = $values['revision_id'];
  116. }
  117. }
  118. // When updating, we make sure that id exists.
  119. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  120. if (!isset($paragraphs_item->item_id)) {
  121. throw new MigrateException(t('System-of-record is DESTINATION, but no destination item_id provided'));
  122. }
  123. // Hold raw original values for later.
  124. $raw_paragraph = $paragraphs_item;
  125. // This entity will be the one, we party on.
  126. $entity = paragraphs_item_load($paragraphs_item->item_id);
  127. if (empty($entity)) {
  128. throw new MigrateException(t('System-of-record is DESTINATION, but paragraphs item !item_id does not exist',
  129. array('!item_id' => $paragraphs_item->item_id)));
  130. }
  131. }
  132. // Provide defaults for SOURCE d
  133. else {
  134. // Set some default properties.
  135. $defaults = array(
  136. 'language' => $this->language,
  137. 'bundle' => $this->bundle,
  138. 'field_name' => $this->field_name,
  139. 'archived' => 0,
  140. );
  141. foreach ($defaults as $field => $value) {
  142. if (!isset($paragraphs_item->$field)) {
  143. $paragraphs_item->$field = $value;
  144. }
  145. }
  146. }
  147. $this->prepare($paragraphs_item, $row);
  148. if ($migration->getSystemOfRecord() == Migration::DESTINATION) {
  149. foreach ($raw_paragraph as $field => $value) {
  150. $entity->$field = $paragraphs_item->$field;
  151. }
  152. }
  153. else {
  154. // This will be the entity we party on.
  155. $entity = entity_create('paragraphs_item', (array) $paragraphs_item);
  156. }
  157. if (isset($entity->item_id) && $entity->item_id) {
  158. $updating = TRUE;
  159. }
  160. else {
  161. $updating = FALSE;
  162. }
  163. migrate_instrument_start('paragraphs_item_save');
  164. $entity->save(TRUE);
  165. migrate_instrument_stop('paragraphs_item_save');
  166. $this->complete($entity, $row);
  167. if (isset($entity->item_id) && $entity->item_id > 0) {
  168. $return = array($entity->item_id);
  169. if ($updating) {
  170. $this->numUpdated++;
  171. }
  172. else {
  173. $this->numCreated++;
  174. }
  175. }
  176. else {
  177. $return = FALSE;
  178. }
  179. return $return;
  180. }
  181. }