summaryrefslogtreecommitdiffstats
path: root/location.migrate.inc
blob: 05f5db76cbabb9f5bcb158e9deb6b8e2c2894b4f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
 * @file
 * Migrate integration.
 */

/**
 * Implements hook_migrate_api().
 */
function location_migrate_api() {
  $api = array(
    'api' => 2,
    'field handlers' => array(
      'MigrateLocationFieldHandler',
    ),
  );

  return $api;
}

/*
*
* Primary value passed to this field is the is_primary value (boolean)
* but can be overwritten with subfields.
*
* Subfields are used to specify all the other values:
*  'street'
*  'additional',
*  'city',
*  'province',
*  'postal_code',
*  'country',
*  'is_primary',
*  'latitude',
*  'longitude'
*
* @code
*   $this->addFieldMapping('field_location_dest')->defaultValue('TRUE');
*   $this->addFieldMapping('field_location_dest:street', 'field_source_address_1');
*   $this->addFieldMapping('field_location_dest:city', 'field_source_city');
* @endcode
*/

class MigrateLocationFieldHandler extends MigrateFieldHandler {

  /**
   * {@inheritdoc}
   */
  public function __construct() {
    $this->registerTypes(array('location'));
  }

  /**
   * Implementation of MigrateFieldHandler::fields().
   *
   * @param string $type
   *   The file field type - 'file' or 'image'
   *
   * @param string $parent_field
   *   Name of the parent field.
   *
   * @param Migration $migration
   *   The migration context for the parent field. We can look at the mappings
   *   and determine which subfields are relevant.
   *
   * @return array
   *   Array of fields.
   */
  public function fields($type, $parent_field, $migration = NULL) {
    $dummy = array();
    $all_fields = location_invoke_locationapi($dummy, 'fields');
    unset($all_fields['locpick']);
    foreach ($all_fields as $field => $label) {
      $fields[$field] = t('Subfield: @label', array('@label' => $label));
    }
    $fields['latitude'] = t('Subfield: Latitude');
    $fields['longitude'] = t('Subfield: Longitude');
    $fields['source'] = t('Subfield: Source value');
    $fields['is_primary'] = t('Subfield: Is primary');

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function prepare($entity, array $field_info, array $instance, array $values) {
    $migration = Migration::currentMigration();
    $destination = $migration->getDestination();

    $arguments = array();
    if (isset($values['arguments'])) {
      $arguments = $values['arguments'];
      unset($values['arguments']);
    }

    $language = $this->getFieldLanguage($entity, $field_info, $arguments);
    $return = array($language => array());

    foreach ($values as $delta => $value) {
      // Handle potentially multiple arguments.
      $instance_arguments = array();

      foreach ($arguments as $key => $argument) {
        // For a scalar argument, pass it directly.
        if (!is_array($argument)) {
          if ($delta == 0) {
            $instance_arguments[$key] = $argument;
          }
        }
        elseif (isset($argument[$delta])) {
          $instance_arguments[$key] = $argument[$delta];
        }
      }

      if (isset($instance_arguments['country'])) {
        $instance_arguments['country'] = strtolower($instance_arguments['country']);
      }
      // Ensure we have at least one location field besides 'is_primary' before saving.
      if (!empty($instance_arguments)) {
        $instance_arguments['is_primary'] = ($delta == 0);
        $instance_arguments['inhibit_geocode'] = TRUE;

        // Only save the location if saved successfully.
        if ($lid = location_save($instance_arguments)) {
          $return[$language][$delta] = array_merge(array('lid' => $lid),
            $instance_arguments);
        }
      }
    }

    return $return;
  }
}