entity.inc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. /**
  3. * @file
  4. * Defines base for migration destinations implemented as Drupal entities.
  5. */
  6. /**
  7. * Abstract base class for entity-based destination handling. Holds common
  8. * Field API-related functions.
  9. */
  10. abstract class MigrateDestinationEntity extends MigrateDestination {
  11. /**
  12. * The entity type (node, user, taxonomy_term, etc.) of the destination.
  13. *
  14. * @var string
  15. */
  16. protected $entityType;
  17. public function getEntityType() {
  18. return $this->entityType;
  19. }
  20. /**
  21. * The bundle (node type, vocabulary, etc.) of the destination.
  22. *
  23. * @var string
  24. */
  25. protected $bundle;
  26. public function getBundle() {
  27. return $this->bundle;
  28. }
  29. /**
  30. * Default language for text fields in this destination.
  31. *
  32. * @var string
  33. */
  34. protected $language;
  35. public function getLanguage() {
  36. return $this->language;
  37. }
  38. /**
  39. * Default input format for text fields in this destination.
  40. *
  41. * @var int
  42. */
  43. protected $textFormat;
  44. public function getTextFormat() {
  45. return $this->textFormat;
  46. }
  47. /**
  48. * Simply save the key schema.
  49. *
  50. * @param array $key_schema
  51. */
  52. public function __construct($entity_type, $bundle, array $options = array()) {
  53. parent::__construct();
  54. $this->entityType = $entity_type;
  55. $this->bundle = $bundle;
  56. $this->language = isset($options['language']) ? $options['language'] : LANGUAGE_NONE;
  57. $this->textFormat = isset($options['text_format']) ? $options['text_format'] : filter_fallback_format();
  58. }
  59. public function __toString() {
  60. // TODO: Link to configuration page
  61. if ($this->entityType == $this->bundle) {
  62. $output = t('%type', array('%type' => $this->entityType));
  63. }
  64. else {
  65. $output = t('%type (%bundle)',
  66. array('%type' => $this->entityType, '%bundle' => $this->bundle));
  67. }
  68. // TODO: Non-default language, input format
  69. return $output;
  70. }
  71. /**
  72. * Give handlers a shot at cleaning up before an entity has been rolled back.
  73. *
  74. * @param $entity_id
  75. * ID of the entity about to be deleted..
  76. */
  77. public function prepareRollback($entity_id) {
  78. $migration = Migration::currentMigration();
  79. // Call any general entity handlers (in particular, the builtin field handler)
  80. migrate_handler_invoke_all('Entity', 'prepareRollback', $entity_id);
  81. // Then call any entity-specific handlers
  82. migrate_handler_invoke_all($this->entityType, 'prepareRollback', $entity_id);
  83. // Then call any prepare handler for this specific Migration.
  84. if (method_exists($migration, 'prepareRollback')) {
  85. $migration->prepareRollback($entity_id);
  86. }
  87. }
  88. /**
  89. * Give handlers a shot at cleaning up after an entity has been rolled back.
  90. *
  91. * @param $entity_id
  92. * ID of the entity which has been deleted.
  93. */
  94. public function completeRollback($entity_id) {
  95. $migration = Migration::currentMigration();
  96. // Call any general entity handlers (in particular, the builtin field handler)
  97. migrate_handler_invoke_all('Entity', 'completeRollback', $entity_id);
  98. // Then call any entity-specific handlers
  99. migrate_handler_invoke_all($this->entityType, 'completeRollback', $entity_id);
  100. // Then call any complete handler for this specific Migration.
  101. if (method_exists($migration, 'completeRollback')) {
  102. $migration->completeRollback($entity_id);
  103. }
  104. }
  105. /**
  106. * Give handlers a shot at modifying the object before saving it.
  107. *
  108. * @param $entity
  109. * Entity object to build. Prefilled with any fields mapped in the Migration.
  110. * @param $source_row
  111. * Raw source data object - passed through to prepare handlers.
  112. */
  113. public function prepare($entity, stdClass $source_row) {
  114. // Add source keys for debugging and identification of migrated data by hooks.
  115. /* TODO: Restore
  116. foreach ($migration->sourceKeyMap() as $field_name => $key_name) {
  117. $keys[$key_name] = $source_row->$field_name;
  118. }
  119. */
  120. $migration = Migration::currentMigration();
  121. $entity->migrate = array(
  122. // 'source_keys' => $keys,
  123. 'machineName' => $migration->getMachineName(),
  124. );
  125. // Call any general entity handlers (in particular, the builtin field handler)
  126. migrate_handler_invoke_all('Entity', 'prepare', $entity, $source_row);
  127. // Then call any entity-specific handlers
  128. migrate_handler_invoke_all($this->entityType, 'prepare', $entity, $source_row);
  129. // Then call any prepare handler for this specific Migration.
  130. if (method_exists($migration, 'prepare')) {
  131. $migration->prepare($entity, $source_row);
  132. }
  133. }
  134. /**
  135. * Give handlers a shot at modifying the object (or taking additional action)
  136. * after saving it.
  137. *
  138. * @param $object
  139. * Entity object to build. This is the complete object after saving.
  140. * @param $source_row
  141. * Raw source data object - passed through to complete handlers.
  142. */
  143. public function complete($entity, stdClass $source_row) {
  144. // Call any general entity handlers (in particular, the builtin field handler)
  145. migrate_handler_invoke_all('Entity', 'complete', $entity, $source_row);
  146. // Then call any entity-specific handlers
  147. migrate_handler_invoke_all($this->entityType, 'complete', $entity, $source_row);
  148. // Then call any complete handler for this specific Migration.
  149. $migration = Migration::currentMigration();
  150. if (method_exists($migration, 'complete')) {
  151. try {
  152. $migration->complete($entity, $source_row);
  153. }
  154. catch (Exception $e) {
  155. // If we catch any errors here, save the messages without letting
  156. // the exception prevent the saving of the entity being recorded.
  157. $migration->saveMessage($e->getMessage());
  158. }
  159. }
  160. }
  161. }