1

I'm trying to convert a d6 location field into a a d8 geolocation field, but when I run it the field does not get updated, and I don't get any messages either.

All of the other node data has been imported already so I am running the migration with the command: drush migrate-import ttf_location_migration --update

I am including title because without it I get an mySQL error message.

Here's my migration YML file:

id: ttf_location_migration

label: Migrate old location data to geolocation field

source:
  plugin: ttf_location
  key: ttf_migrate

process:
  nid: nid
  title: title
  type: type
  'field_location/bundle': type
  'field_location/deleted':
    plugin: default_value
    default_value: 0
  'field_location/entity_id': nid
  'field_location/revision_id': nid
  'field_location/langcode':
    plugin: default_value
    default_value: 'en'
  'field_location/delta': 
    plugin: default_value
    default_value: 0
  'field_location/field_location_lat': latitude
  'field_location/field_location_lng': longitude


destination:
  plugin: entity:node
  overwrite_properties:
    - 'field_location/bundle'
    - 'field_location/deleted'
    - 'field_location/entity_id'
    - 'field_location/revision_id'
    - 'field_location/langcode'
    - 'field_location/delta' 
    - 'field_location/field_location_lat'
    - 'field_location/field_location_lng'

migration_dependencies: {}

dependencies:
  enforced:
    module:
      - ttf_migrate

and my source plugin:

<?php

namespace Drupal\ttf_migrate\Plugin\migrate\source;

use Drupal\migrate\Plugin\migrate\source\SqlBase;
use Drupal\migrate\Row;

/**
 * Source plugin for location.
 *
 * @MigrateSource(
 *   id = "ttf_location"
 * )
 */
class TtfLocation extends SqlBase {

  /**
   * {@inheritdoc}
   */
  public function query() {


    $query = $this->select('location', 'l')
      ->fields('l', ['lid', 'latitude', 'longitude']);
    $query->join('location_instance','li','l.lid = li.lid');
    $query->fields('li', ['nid']);
    $query->join('node', 'n', 'li.nid = n.nid');
    $query->fields('n', ['type', 'title']);

    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'lid' => $this->t('Location ID'),
      'latitude' => $this->t('Latitude'),
      'longitude' => $this->t('longitude'),
      'nid' => $this->t('Content ID'),
      'type' => $this->t('Type'),
      'title'=>$this->t('Title')
    ];

    return $fields;
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    return [
      'nid' => [
        'type' => 'integer',
        'alias' => 'nid',
      ],
    ];
  }

}

Any help much appreciated

Thank you

1
+50

Do not repeat the field name for sub columns: Instead of e.g. field_location/field_location_lat use only field_location/lat, in the process: section.

There also is no need to manually set interal keys like entity_id or revision_id: Only set the destination plugin and the field values, and Migrate module will fill all other internal columns for you.

destination:
  plugin: entity:node

process:
  title: title
  type:
    plugin: default_value
    value: NODE_BUNDLE_NAME
  langcode:
    plugin: default_value
    value: 'en'
  field_location/lat: latitude
  field_location/lng: longitude
  • Great thanks @Hudri - that worked beautifully. – gillarf Dec 20 '19 at 15:08
0

From a code snippet I posted a while ago, you can make up the following structure for a geolocation field:

$node = Node::create(
  'type' => 'location',
  'title' => 'My location',
  'geolocation_field' => ['lat'=> 41.264, 'lng' => 45.33]
);
$node->save();

Not familiar with migrate module, but this can help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for?Browse other questions tagged or ask your own question.