addressfield.migrate.inc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @file
  4. * Base integration with the Migrate API class.
  5. */
  6. /**
  7. * Implements hook_migrate_api().
  8. */
  9. function addressfield_migrate_api() {
  10. $api = array(
  11. 'api' => 2,
  12. 'field handlers' => array('MigrateAddressFieldHandler'),
  13. );
  14. return $api;
  15. }
  16. /**
  17. * Primary value passed to this field must be the two letter ISO country code of
  18. * the address.
  19. *
  20. * Arguments are used to specify all the other values:
  21. * 'administrative_area' - The administrative area of this address. (i.e. State/Province)
  22. * 'sub_administrative_area' - The sub administrative area of this address.
  23. * 'locality' - The locality of this address. (i.e. City)
  24. * 'dependent_locality' - The dependent locality of this address.
  25. * 'postal_code' - The postal code of this address.
  26. * 'thoroughfare' - The thoroughfare of this address. (i.e. Street address)
  27. * 'premise' - The premise of this address. (i.e. Apartment / Suite number)
  28. * 'sub_premise' - The sub_premise of this address.
  29. * 'organisation_name' - Contents of a primary OrganisationName element in the xNL XML.
  30. * 'name_line' - Contents of a primary NameLine element in the xNL XML.
  31. * 'first_name' - Contents of the FirstName element of a primary PersonName element in the xNL XML.
  32. * 'last_name' - Contents of the LastName element of a primary PersonName element in the xNL XML.
  33. * 'data' - Additional data for this address.
  34. *
  35. * Add the source field mappings to the argument array then add null mappings to
  36. * avoid having fields flagged as as unmapped:
  37. * @code
  38. * // The country should be passed in as the primary value.
  39. * $this->addFieldMapping('field_address', 'profile_country');
  40. * $this->addFieldMapping('field_address:thoroughfare', 'profile_address');
  41. * $this->addFieldMapping('field_address:locality', 'profile_city');
  42. * $this->addFieldMapping('field_address:administrative_area', 'profile_state');
  43. * @endcode
  44. */
  45. class MigrateAddressFieldHandler extends MigrateFieldHandler {
  46. public function __construct() {
  47. $this->registerTypes(array('addressfield'));
  48. }
  49. /**
  50. * Provide subfields for the addressfield columns.
  51. */
  52. public function fields() {
  53. // Declare our arguments to also be available as subfields.
  54. $fields = array(
  55. 'administrative_area' => t('<a href="@doc">The administrative area of ' .
  56. 'this address (i.e. State/Province)</a>',
  57. array('@doc' => 'http://drupal.org/node/1996546#administrative_area')),
  58. 'sub_administrative_area' => t('<a href="@doc">The sub administrative ' .
  59. 'area of this address</a>',
  60. array('@doc' => 'http://drupal.org/node/1996546#sub_administrative_area')),
  61. 'locality' => t('<a href="@doc">The locality of this address (i.e. ' .
  62. 'City)</a>',
  63. array('@doc' => 'http://drupal.org/node/1996546#locality')),
  64. 'dependent_locality' => t('<a href="@doc">The dependent locality of ' .
  65. 'this address</a>',
  66. array('@doc' => 'http://drupal.org/node/1996546#dependent_locality')),
  67. 'postal_code' => t('<a href="@doc">The postal code of this address</a>',
  68. array('@doc' => 'http://drupal.org/node/1996546#postal_code')),
  69. 'thoroughfare' => t('<a href="@doc">The thoroughfare of this address ' .
  70. '(i.e. Street address)</a>',
  71. array('@doc' => 'http://drupal.org/node/1996546#thoroughfare')),
  72. 'premise' => t('<a href="@doc">The premise of this address (i.e. Apartment / Suite number)</a>',
  73. array('@doc' => 'http://drupal.org/node/1996546#premise')),
  74. 'sub_premise' => t('<a href="@doc">The sub_premise of this address</a>',
  75. array('@doc' => 'http://drupal.org/node/1996546#sub_premise')),
  76. 'organisation_name' => t('<a href="@doc">Contents of a primary ' .
  77. 'OrganisationName element in the xNL XML</a>',
  78. array('@doc' => 'http://drupal.org/node/1996546#organisation_name')),
  79. 'name_line' => t('<a href="@doc">Contents of a primary NameLine element ' .
  80. 'in the xNL XML</a>',
  81. array('@doc' => 'http://drupal.org/node/1996546#name_line')),
  82. 'first_name' => t('<a href="@doc">Contents of the FirstName element of ' .
  83. 'a primary PersonName element in the xNL XML</a>',
  84. array('@doc' => 'http://drupal.org/node/1996546#first_name')),
  85. 'last_name' => t('<a href="@doc">Contents of the LastName element of a ' .
  86. 'primary PersonName element in the xNL XML</a>',
  87. array('@doc' => 'http://drupal.org/node/1996546#last_name')),
  88. 'data' => t('<a href="@doc">Additional data for this address</a>',
  89. array('@doc' => 'http://drupal.org/node/1996546#data')),
  90. );
  91. return $fields;
  92. }
  93. /**
  94. * Implements MigrateFieldHandler::prepare().
  95. *
  96. * @param $entity
  97. * @param array $field_info
  98. * @param array $instance
  99. * @param array $values
  100. *
  101. * @return null
  102. */
  103. public function prepare($entity, array $field_info, array $instance,
  104. array $values) {
  105. $arguments = array();
  106. if (isset($values['arguments'])) {
  107. $arguments = array_filter($values['arguments']);
  108. unset($values['arguments']);
  109. }
  110. $language = $this->getFieldLanguage($entity, $field_info, $arguments);
  111. // Setup the standard Field API array for saving.
  112. $delta = 0;
  113. foreach ($values as $value) {
  114. $return[$language][$delta] = array('country' => $value)
  115. + $this->prepareArguments($arguments, $field_info, $delta);
  116. $delta++;
  117. }
  118. return isset($return) ? $return : NULL;
  119. }
  120. /**
  121. * Builds an array with additional data for the current $delta.
  122. *
  123. * @param array $arguments
  124. * @param array $field_info
  125. * @param $delta
  126. *
  127. * @return array
  128. */
  129. protected function prepareArguments(array $arguments, array $field_info, $delta) {
  130. $result = array();
  131. $data = array();
  132. foreach ($arguments as $column_key => $column_value) {
  133. $value = NULL;
  134. if (is_array($arguments[$column_key])) {
  135. if (!empty($arguments[$column_key][$delta])) {
  136. $value = $arguments[$column_key][$delta];
  137. }
  138. }
  139. else {
  140. $value = $arguments[$column_key];
  141. }
  142. if ($value) {
  143. if (isset($field_info['columns'][$column_key])) {
  144. // Store the data in a separate column.
  145. $result[$column_key] = $value;
  146. }
  147. else {
  148. // Add the data to the 'data' column.
  149. $data[$column_key] = $value;
  150. }
  151. }
  152. }
  153. // Store all the other data as a serialized array in the data field.
  154. if (!empty($data)) {
  155. $result['data'] = serialize($data);
  156. }
  157. return $result;
  158. }
  159. }