migrate_extras_media.migrate.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. <?php
  2. /**
  3. * @file
  4. * Examples and test fodder for migration of Media entities and fields.
  5. */
  6. /**
  7. * Migration class for media images.
  8. */
  9. class MigrateExampleMediaImageMigration extends XMLMigration {
  10. public function __construct() {
  11. parent::__construct();
  12. $this->description = t('Example migration of media images');
  13. $this->map = new MigrateSQLMap($this->machineName,
  14. array(
  15. 'filename' => array(
  16. 'type' => 'varchar',
  17. 'length' => 255,
  18. 'not null' => TRUE,
  19. 'description' => 'Image filename',
  20. )
  21. ),
  22. MigrateDestinationMedia::getKeySchema()
  23. );
  24. // Source fields available in the XML file.
  25. $fields = array(
  26. 'filename' => t('Image filename, relative to the source directory'),
  27. 'description' => t('Description of the image'),
  28. );
  29. // Our test data is in an XML file
  30. $xml_folder = drupal_get_path('module', 'migrate_extras_media');
  31. $items_url = $xml_folder . '/migrate_extras_media.xml';
  32. $item_xpath = '/source_data/item/image';
  33. $item_ID_xpath = 'filename';
  34. $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);
  35. $this->source = new MigrateSourceMultiItems($items_class, $fields);
  36. // In the simplest case, just pass the media type.
  37. $this->destination = new MigrateDestinationMedia('image');
  38. // The source images are in a local directory - specify the parent.
  39. $this->addFieldMapping('source_dir')
  40. ->defaultValue(drupal_get_path('module', 'migrate_extras_media') . '/source_files');
  41. // The 'value' of the media destination is mapped to the source field
  42. // representing the media itself - in this case, a filename relative to
  43. // source_dir.
  44. $this->addFieldMapping('value', 'filename')
  45. ->xpath('filename');
  46. // Fields on the entity can be mapped in the usual way.
  47. $this->addFieldMapping('field_image_description', 'description')
  48. ->xpath('description');
  49. $this->addFieldMapping('uid')
  50. ->defaultValue(1);
  51. $this->addUnmigratedDestinations(array('field_image_description:format',
  52. 'field_image_description:language', 'destination_dir', 'destination_file',
  53. 'file_replace', 'preserve_files', 'timestamp'));
  54. if (module_exists('path')) {
  55. $this->addUnmigratedDestinations(array('path'));
  56. }
  57. }
  58. }
  59. /**
  60. * Migration class for media_youtube entities.
  61. */
  62. class MigrateExampleMediaVideoMigration extends XMLMigration {
  63. public function __construct() {
  64. parent::__construct();
  65. $this->description = t('Example migration of Youtube videos');
  66. $this->map = new MigrateSQLMap($this->machineName,
  67. array(
  68. 'uri' => array(
  69. 'type' => 'varchar',
  70. 'length' => 255,
  71. 'not null' => TRUE,
  72. 'description' => 'YouTube URI',
  73. )
  74. ),
  75. MigrateDestinationMedia::getKeySchema()
  76. );
  77. // Source fields available in the XML file.
  78. $fields = array(
  79. 'uri' => t('URI of a YouTube video'),
  80. 'description' => t('Description of the video'),
  81. );
  82. // Our test data is in an XML file
  83. $xml_folder = drupal_get_path('module', 'migrate_extras_media');
  84. $items_url = $xml_folder . '/migrate_extras_media.xml';
  85. $item_xpath = '/source_data/item/video';
  86. $item_ID_xpath = 'uri';
  87. $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);
  88. $this->source = new MigrateSourceMultiItems($items_class, $fields);
  89. // In this case, we need to specify the file_class in the second paramter -
  90. // this class understands how to translate a http://www.youtube.com/ URI
  91. // into Drupal's Youtube file scheme (youtube://).
  92. $this->destination = new MigrateDestinationMedia('video',
  93. 'MigrateExtrasFileYoutube');
  94. // We just need to map the 'value' in the media destination to the Youtube
  95. // URI.
  96. $this->addFieldMapping('value', 'uri')
  97. ->xpath('uri');
  98. $this->addFieldMapping('field_video_description', 'description')
  99. ->xpath('description');
  100. $this->addFieldMapping('uid')
  101. ->defaultValue(1);
  102. $this->addUnmigratedDestinations(array('field_video_description:format',
  103. 'field_video_description:language', 'timestamp'));
  104. if (module_exists('path')) {
  105. $this->addUnmigratedDestinations(array('path'));
  106. }
  107. }
  108. }
  109. /**
  110. * Migration class for nodes with media fields.
  111. */
  112. class MigrateExampleMediaNodeMigration extends XMLMigration {
  113. public function __construct() {
  114. parent::__construct();
  115. $this->description = t('Example migration into the Media module');
  116. $this->dependencies =
  117. array('MigrateExampleMediaImage', 'MigrateExampleMediaVideo');
  118. $this->map = new MigrateSQLMap($this->machineName,
  119. array(
  120. 'id' => array(
  121. 'type' => 'int',
  122. 'unsigned' => TRUE,
  123. 'not null' => TRUE,
  124. 'description' => 'Media ID',
  125. )
  126. ),
  127. MigrateDestinationNode::getKeySchema()
  128. );
  129. // Source fields available in the XML file.
  130. $fields = array(
  131. 'id' => t('Source id'),
  132. 'title' => t('Title'),
  133. 'body' => t('Description'),
  134. 'video_uri' => t('A YouTube URI'),
  135. 'video_description' => t('Description for a YouTube video'),
  136. 'image_filename' => t('Image filename'),
  137. 'image_description' => t('Image description'),
  138. 'document_filename' => t('Document filename'),
  139. );
  140. // Our test data is in an XML file
  141. $xml_folder = drupal_get_path('module', 'migrate_extras_media');
  142. $items_url = $xml_folder . '/migrate_extras_media.xml';
  143. $item_xpath = '/source_data/item';
  144. $item_ID_xpath = 'id';
  145. $items_class = new MigrateItemsXML($items_url, $item_xpath, $item_ID_xpath);
  146. $this->source = new MigrateSourceMultiItems($items_class, $fields);
  147. $this->destination = new MigrateDestinationNode('migrate_extras_media_example');
  148. // Basic fields
  149. $this->addFieldMapping('title', 'title')
  150. ->xpath('title');
  151. $this->addFieldMapping('uid')
  152. ->defaultValue(1);
  153. $this->addFieldMapping('body', 'body')
  154. ->xpath('body');
  155. $this->addFieldMapping('body:format')
  156. ->defaultValue('filtered_html');
  157. // The image and Youtube media entities are imported via their own
  158. // migrations above, we just need to link the fields to them. We do this
  159. // by mapping the primary keys of those migrations (URL and filename) to
  160. // the primary field values, and specifying a file_class of MigrateFileFid.
  161. $this->addFieldMapping('field_youtube_video', 'video_uri')
  162. ->xpath('video/uri')
  163. ->sourceMigration('MigrateExampleMediaVideo');
  164. $this->addFieldMapping('field_youtube_video:file_class')
  165. ->defaultValue('MigrateFileFid');
  166. $this->addFieldMapping('field_media_image', 'image_filename')
  167. ->xpath('image/filename')
  168. ->sourceMigration('MigrateExampleMediaImage');
  169. $this->addFieldMapping('field_media_image:file_class')
  170. ->defaultValue('MigrateFileFid');
  171. // We have not created a separate migration for documents, we're using the
  172. // file field handler to get those. This works just like it does for regular
  173. // file fields.
  174. $this->addFieldMapping('field_document', 'document_filename')
  175. ->xpath('document/filename');
  176. // This isn't technically necessary - MigrateFileUri is the default
  177. $this->addFieldMapping('field_document:file_class')
  178. ->defaultValue('MigrateFileUri');
  179. $this->addFieldMapping('field_document:source_dir')
  180. ->defaultValue(drupal_get_path('module', 'migrate_extras_media') . '/source_files');
  181. $this->addFieldMapping('field_document:destination_file', 'document_filename')
  182. ->xpath('document/filename');
  183. // Unmapped destination fields
  184. $this->addUnmigratedDestinations(array('is_new', 'status', 'promote',
  185. 'revision', 'language', 'sticky', 'created', 'changed', 'revision_uid',
  186. 'log', 'tnid', 'body:summary', 'body:language',
  187. 'comment'));
  188. $this->addUnmigratedDestinations(array('field_media_image:language',
  189. 'field_media_image:display', 'field_media_image:description',
  190. 'field_youtube_video:language', 'field_youtube_video:description',
  191. 'field_youtube_video:display', 'field_document:language', 'field_document:destination_dir',
  192. 'field_document:file_replace', 'field_document:preserve_files',
  193. 'field_document:description', 'field_document:display'));
  194. if (module_exists('path')) {
  195. $this->addUnmigratedDestinations(array('path'));
  196. if (module_exists('pathauto')) {
  197. $this->addUnmigratedDestinations(array('pathauto'));
  198. }
  199. }
  200. $this->addUnmigratedSources(array('image_description', 'video_description'));
  201. }
  202. /**
  203. * Implementation of Migration::prepare().
  204. */
  205. public function prepare($node, $row) {
  206. // This will replace any <img> tags in the body with the media module's
  207. // JSON references.
  208. MigrateDestinationMedia::rewriteImgTags($node);
  209. }
  210. }
  211. /*
  212. * Implementation of hook_migrate_api().
  213. */
  214. function migrate_extras_media_migrate_api() {
  215. $api = array(
  216. 'api' => 2,
  217. );
  218. return $api;
  219. }