Projet

Général

Profil

Paste
Télécharger (6,58 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / addressfield / addressfield.migrate.inc @ d51f9c7d

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