1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Base integration with the Migrate API class.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Implements hook_migrate_api().
|
10
|
*/
|
11
|
function addressfield_migrate_api() {
|
12
|
$api = array(
|
13
|
'api' => 2,
|
14
|
'field handlers' => array('MigrateAddressFieldHandler'),
|
15
|
);
|
16
|
return $api;
|
17
|
}
|
18
|
|
19
|
/**
|
20
|
* Primary value passed to this field must be the two letter ISO country code of
|
21
|
* the address.
|
22
|
*
|
23
|
* Arguments are used to specify all the other values:
|
24
|
* 'administrative_area' - The administrative area of this address. (i.e. State/Province)
|
25
|
* 'sub_administrative_area' - The sub administrative area of this address.
|
26
|
* 'locality' - The locality of this address. (i.e. City)
|
27
|
* 'dependent_locality' - The dependent locality of this address.
|
28
|
* 'postal_code' - The postal code of this address.
|
29
|
* 'thoroughfare' - The thoroughfare of this address. (i.e. Street address)
|
30
|
* 'premise' - The premise of this address. (i.e. Apartment / Suite number)
|
31
|
* 'sub_premise' - The sub_premise of this address.
|
32
|
* 'organisation_name' - Contents of a primary OrganisationName element in the xNL XML.
|
33
|
* 'name_line' - Contents of a primary NameLine element in the xNL XML.
|
34
|
* 'first_name' - Contents of the FirstName element of a primary PersonName element in the xNL XML.
|
35
|
* 'last_name' - Contents of the LastName element of a primary PersonName element in the xNL XML.
|
36
|
* 'data' - Additional data for this address.
|
37
|
*
|
38
|
* Add the source field mappings to the argument array then add null mappings to
|
39
|
* avoid having fields flagged as as unmapped:
|
40
|
* @code
|
41
|
* // The country should be passed in as the primary value.
|
42
|
* $this->addFieldMapping('field_address', 'profile_country');
|
43
|
* $this->addFieldMapping('field_address:thoroughfare', 'profile_address');
|
44
|
* $this->addFieldMapping('field_address:locality', 'profile_city');
|
45
|
* $this->addFieldMapping('field_address:administrative_area', 'profile_state');
|
46
|
* @endcode
|
47
|
*/
|
48
|
class MigrateAddressFieldHandler extends MigrateFieldHandler {
|
49
|
public function __construct() {
|
50
|
$this->registerTypes(array('addressfield'));
|
51
|
}
|
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
|
/**
|
99
|
* Implements MigrateFieldHandler::prepare().
|
100
|
*
|
101
|
* @param $entity
|
102
|
* @param array $field_info
|
103
|
* @param array $instance
|
104
|
* @param array $values
|
105
|
*
|
106
|
* @return null
|
107
|
*/
|
108
|
public function prepare($entity, array $field_info, array $instance,
|
109
|
array $values) {
|
110
|
$arguments = array();
|
111
|
if (isset($values['arguments'])) {
|
112
|
$arguments = array_filter($values['arguments']);
|
113
|
unset($values['arguments']);
|
114
|
}
|
115
|
$language = $this->getFieldLanguage($entity, $field_info, $arguments);
|
116
|
|
117
|
// Setup the standard Field API array for saving.
|
118
|
$delta = 0;
|
119
|
foreach ($values as $value) {
|
120
|
$return[$language][$delta] = array('country' => $value)
|
121
|
+ $this->prepareArguments($arguments, $field_info, $delta);
|
122
|
$delta++;
|
123
|
}
|
124
|
|
125
|
return isset($return) ? $return : NULL;
|
126
|
}
|
127
|
|
128
|
/**
|
129
|
* Builds an array with additional data for the current $delta.
|
130
|
*
|
131
|
* @param array $arguments
|
132
|
* @param array $field_info
|
133
|
* @param $delta
|
134
|
*
|
135
|
* @return array
|
136
|
*/
|
137
|
protected function prepareArguments(array $arguments, array $field_info, $delta) {
|
138
|
$result = array();
|
139
|
$data = array();
|
140
|
|
141
|
foreach ($arguments as $column_key => $column_value) {
|
142
|
$value = NULL;
|
143
|
|
144
|
if (is_array($arguments[$column_key])) {
|
145
|
if (!empty($arguments[$column_key][$delta])) {
|
146
|
$value = $arguments[$column_key][$delta];
|
147
|
}
|
148
|
}
|
149
|
else {
|
150
|
$value = $arguments[$column_key];
|
151
|
}
|
152
|
|
153
|
if ($value) {
|
154
|
if (isset($field_info['columns'][$column_key])) {
|
155
|
// Store the data in a separate column.
|
156
|
$result[$column_key] = $value;
|
157
|
}
|
158
|
else {
|
159
|
// Add the data to the 'data' column.
|
160
|
$data[$column_key] = $value;
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
|
165
|
// Store all the other data as a serialized array in the data field.
|
166
|
if (!empty($data)) {
|
167
|
$result['data'] = serialize($data);
|
168
|
}
|
169
|
|
170
|
return $result;
|
171
|
}
|
172
|
}
|