0

I am trying to import content into Drupal 8 from an external source (not previous Drupal version). I have crawled the original site and placed the content in a database. I am then writing a custom module to bring it into Druapl. I have the default language as English so when I run the migration for the English content there has no issue. It all imports perfectly. When I then run the translation migration, with French content, I get this error:

 [error]  SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null: INSERT INTO {key_value} (name, collection, value) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array
(
    [:db_insert_placeholder_0] =>
    [:db_insert_placeholder_1] => node_translation_redirect
    [:db_insert_placeholder_2] => a:2:{i:0;N;i:1;N;}
)

Despite the error the content is imported, but as separate nodes instead of as translations to the original migration. I am using drush to run the migrations. Even though the French is imported, incorrectly, drush reports that the migration failed. I try to rollback both and the French does not get removed. I have to remove it manually.

I searched around but have not been able to find anyone having issues with SQL migrations.

Any help is appreciated. Thanks.

Here are my files:

mymigrate_content.yml

id: mymigrate_content
label: my content migration
langcode: en
source:
  plugin: mymigrate_content
  default_lang: true
  key: canada
  ids:
    pageid:
      type: integer
process:
  id: pageid
  title: title
  'body/value': body
  'body/format':
    plugin: default_value
    default_value: full_html
  'moderation_state/target_id':
    plugin: default_value
    default_value: published
destination:
  plugin: 'entity:node'
  default_bundle: page
  langcode: en
  no_stub: true

mymigrate_content_translation.yml

id: mymigrate_content_translation
label: my content migration
langcode: fr
source:
  plugin: mymigrate_content_translation
  translations: true
  default_lang: false
  key: canada
  ids:
    pageid:
      type: integer
process:
  id: 
    plugin: migration_lookup
    migration: mymigrate_content
    source: pageid
  title: title
  'body/value': body
  'body/format':
    plugin: default_value
    default_value: full_html
  'moderation_state/target_id':
    plugin: default_value
    default_value: published
  content_translation_source:
    plugin: default_value
    default_value: "en"
destination:
  plugin: 'entity:node'
  default_bundle: page
  langcode: fr
  no_stub: true
  translations: true

MyMigrate.php

<?php
namespace Drupal\ca_migrate\Plugin\migrate\source;

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

/**
 * Migration
 * 
 * @MigrateSource(
 *  id = "mymigrate_content",
 * source_module = "mymigrate",
 * )
 */
class MyMigrate extends SqlBase {
  /**
   * @var integer $rowCount
   */
  public $rowCount = -1;

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('pagecontent', 'pc')
      ->condition('pc.language', 'en', '=')
      ->fields('pc', [
        'pageid',
        'language',
        'body',
        'originalurlpath',
        'title',
      ]);

    return $query;
  }


  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'pageid' => $this->t('pageid'),
      'language' => $this->t('language'),
      'body' => $this->t('body'),
      'originalurlpath' => $this->t('originalurlpath'),
      'title' => $this->t('title'),
    ];

    return $fields;
  }

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

MyMigrateFr.php

<?php
namespace Drupal\ca_migrate\Plugin\migrate\source;

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

/**
 * Migration
 * 
 * @MigrateSource(
 *  id = "mymigrate_content_translation",
 * source_module = "mymigrate",
 * )
 */
class MyMigrateFr extends SqlBase {
  /**
   * @var integer $rowCount
   */
  public $rowCount = -1;

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('pagecontent', 'pc')
      ->condition('pc.language', 'fr', '=')
      ->fields('pc', [
        'pageid',
        'language',
        'body',
        'originalurlpath',
        'title',
      ]);

    return $query;
  }


  /**
   * {@inheritdoc}
   */
  public function fields() {
    $fields = [
      'pageid' => $this->t('pageid'),
      'language' => $this->t('language'),
      'body' => $this->t('body'),
      'originalurlpath' => $this->t('originalurlpath'),
      'title' => $this->t('title'),
    ];

    return $fields;
  }

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

I was finally able to trace the problem. The migration code was expecting a value for the key nid. I was able to provide the appropriate value by overriding the function prepareRow.

Here is the code I used in both MyMigrate.php and MyMigrateFr.php:

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $nid = $row->getSourceProperty('pageid');
    $row->setSourceProperty('nid', $nid);
    $row->setDestinationProperty('nid', $nid);

    return parent::prepareRow($row);
  }
| improve this answer | |

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.