You are here

class MigrateLocationFieldHandler in Location 7.3

Hierarchy

Expanded class hierarchy of MigrateLocationFieldHandler

1 string reference to 'MigrateLocationFieldHandler'
location_migrate_api in ./location.migrate.inc
Implements hook_migrate_api().

File

./location.migrate.inc, line 44
Migrate integration.

View source
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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MigrateFieldHandler::getFieldLanguage function Determine the language of the field.
MigrateHandler::$dependencies protected property List of other handler classes which should be invoked before the current one.
MigrateHandler::$typesHandled protected property List of "types" handled by this handler. Depending on the kind of handler, these may be destination types, field types, etc.
MigrateHandler::getDependencies public function
MigrateHandler::getTypesHandled public function
MigrateHandler::handlesType public function Does this handler handle the given type? 1
MigrateHandler::registerTypes protected function Register a list of types handled by this class
MigrateLocationFieldHandler::fields public function Implementation of MigrateFieldHandler::fields().
MigrateLocationFieldHandler::prepare public function
MigrateLocationFieldHandler::__construct public function Overrides MigrateHandler::__construct