addressfield.migrate.inc 6.6 KB

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