date_migrate_example.migrate.inc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @file
  4. * Examples and test folder for migration into date fields.
  5. */
  6. /**
  7. * Migration class to test import of various date fields.
  8. */
  9. class DateExampleMigration extends XMLMigration {
  10. /**
  11. * Sets up the migration.
  12. */
  13. public function __construct() {
  14. parent::__construct();
  15. $this->description = t('Example migration into date fields');
  16. $this->map = new MigrateSQLMap($this->machineName,
  17. array(
  18. 'id' => array(
  19. 'type' => 'int',
  20. 'unsigned' => TRUE,
  21. 'not null' => TRUE,
  22. 'description' => 'Date ID',
  23. ),
  24. ),
  25. MigrateDestinationNode::getKeySchema()
  26. );
  27. // Source fields available in the XML file.
  28. $fields = array(
  29. 'id' => t('Source id'),
  30. 'title' => t('Title'),
  31. 'body' => t('Description'),
  32. 'date' => t('A simple date'),
  33. 'date_range_from' => t('Start value for a date range'),
  34. 'datestamp' => t('Simple datestamp'),
  35. 'datestamp_range_from' => t('Start value for a datestamp range'),
  36. 'datetime' => t('Simple datetime'),
  37. 'datetime_range_from' => t('Start value for a datetime range'),
  38. 'date_repeat' => t('Sample of a repeating date field'),
  39. );
  40. // Our test data is in an XML file.
  41. $xml_folder = drupal_get_path('module', 'date_migrate_example');
  42. $items_url = $xml_folder . '/date_migrate_example.xml';
  43. $item_xpath = '/source_data/item';
  44. $item_id_xpath = 'id';
  45. $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_id_xpath);
  46. $this->source = new MigrateSourceMultiItems($items_class, $fields);
  47. $this->destination = new MigrateDestinationNode('date_migrate_example');
  48. // Basic fields.
  49. $this->addFieldMapping('title', 'title')
  50. ->xpath('title');
  51. $this->addFieldMapping('uid')
  52. ->defaultValue(1);
  53. $this->addFieldMapping('body', 'body')
  54. ->xpath('body');
  55. // For simple date fields, we just need the xpath.
  56. $this->addFieldMapping('field_date', 'date')
  57. ->xpath('date');
  58. // For date ranges, we add the "end" value in prepareRow() below.
  59. $this->addFieldMapping('field_date_range', 'date_range_from');
  60. $this->addFieldMapping('field_date_range:to', 'date_range_to');
  61. // RRULEs on repeat fields are also done in prepareRow().
  62. $this->addFieldMapping('field_date_repeat', 'date_repeat');
  63. $this->addFieldMapping('field_date_repeat:rrule', 'date_repeat_rrule');
  64. $this->addFieldMapping('field_datestamp', 'datestamp')
  65. ->xpath('datestamp');
  66. $this->addFieldMapping('field_datestamp_range', 'datestamp_range_from');
  67. $this->addFieldMapping('field_datestamp_range:to', 'datestamp_range_to');
  68. // You can specify a timezone to be applied to all values going into the
  69. // field (Tokyo is UTC+9, no DST).
  70. $this->addFieldMapping('field_datetime', 'datetime')
  71. ->xpath('datetime');
  72. $this->addFieldMapping('field_datetime:timezone')
  73. ->defaultValue('Asia/Tokyo');
  74. // You can also get the timezone from the source data - it can be different
  75. // for each instance of the field. Like To and RRULE values, it is added
  76. // in prepareRow().
  77. $this->addFieldMapping('field_datetime_range', 'datetime_range_from');
  78. $this->addFieldMapping('field_datetime_range:to', 'datetime_range_to');
  79. $this->addFieldMapping('field_datetime_range:timezone', 'datetime_range_timezone');
  80. // Unmapped destination fields.
  81. $this->addUnmigratedDestinations(array('is_new', 'status', 'promote',
  82. 'revision', 'language', 'sticky', 'created', 'changed', 'revision_uid'));
  83. }
  84. /**
  85. * Transforms the raw migration data into the expected date formats.
  86. *
  87. * An advanced feature of the date field handler is that in addition to the
  88. * basic (Start) date itself, we can add additional properties like timezone,
  89. * encapsulating them as JSON.
  90. */
  91. public function prepareRow($current_row) {
  92. // The date range field can have multiple values.
  93. $current_row->date_range_from = array();
  94. foreach ($current_row->xml->date_range as $range) {
  95. $current_row->date_range_from[] = (string) $range->from[0];
  96. $current_row->date_range_to[] = (string) $range->to[0];
  97. }
  98. $current_row->datestamp_range_from
  99. = (string) $current_row->xml->datestamp_range->from[0];
  100. $current_row->datestamp_range_to
  101. = (string) $current_row->xml->datestamp_range->to[0];
  102. $current_row->datetime_range_from
  103. = (string) $current_row->xml->datetime_range->from[0];
  104. $current_row->datetime_range_to
  105. = (string) $current_row->xml->datetime_range->to[0];
  106. $current_row->datetime_range_timezone
  107. = (string) $current_row->xml->datetime_range->timezone[0];
  108. $current_row->date_repeat
  109. = (string) $current_row->xml->date_repeat->date[0];
  110. $current_row->date_repeat_rrule
  111. = (string) $current_row->xml->date_repeat->rule[0];
  112. }
  113. }