2

I'm writing a migrate source plugin for migrating D7 entityform (entityform module) submissions. If anyone has done this or knows an existing plugin, please let me know.

I am trying to extend the FieldableEntity base class from Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity; because I want to use its getFields() and getFieldValues() methods when I override prepareRow() to include custom fields from my D7 entityform.

My problem is that when my source plugin extends FieldableEntity, drush reports [error] No migrations found. for my migration. Importantly, however, if my source plugin instead uses the SqlBase class from Drupal\migrate\Plugin\migrate\source\SqlBase, then drush can import all of my entityforms from D7 into D9 contact forms no problem. So it would seem that I am missing something when I try to implement FieldableEntity preventing it from retrieving data for the migration.

Here is my plugin, which shows up fine in drush ms:

<?php

namespace Drupal\mymodule_migrate\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\d7\FieldableEntity;

/**
 * The 'mymodule_migrate_entityform' source plugin.
 *
 * @MigrateSource(
 *   id = "mymodule_migrate_entityform",
 *   source_module = "mymodule_migrate"
 * )
 */
class Entityform extends FieldableEntity {

  /**
   * {@inheritdoc}
   */
  public function query() {
    $query = $this->select('entityform', 'e')
      ->fields('e', [
        'entityform_id',
        'type',
        'created',
        'changed',
        'uid'
      ]);
    return $query;
  }

  /**
   * {@inheritdoc}
   */
  public function fields() {
    return array(
      'entityform_id' => $this->t('The ID of the entityform submission.'),
      'type' => $this->t('The entityform type.'),
      'created' => $this->t('The time that the entityform was submitted.'),
      'changed' => $this->t('Last updated time.'),
      'uid' => $this->t('The {users}.uid who submitted the entityform. If set to 0, this entityform was submitted by an anonymous user.'),
    );
  }

  /**
   * {@inheritdoc}
   */
  public function getIds() {
    $ids['entityform_id']['type'] = 'integer';
    $ids['entityform_id']['alias'] = 'e';
    return $ids;
  }

  /**
   * {@inheritdoc}
   */
  public function prepareRow(Row $row) {
    $entityform_id = $row->getSourceProperty('entityform_id');
    // Get Field API field values.
    foreach ($this->getFields('entityform', 'BUNDLE_MACHINE_NAME') as $field_name => $field) {
      $row->setSourceProperty($field_name, $this->getFieldValues('entityform', $field_name, $entityform_id));
    }
    return parent::prepareRow($row);
  }

}

I use this yml migration definition. My migrate destination is D8/D9 contact_message entity.

id: mymodule_contact_message
label: D8 Contact message migration from D7 entityform submission
source:
  plugin: mymodule_migrate_entityform
process:
  id: entityform_id
  langcode:
    plugin: default_value
    source: language
    default_value: "und"
  created: created
  changed: changed
destination:
  plugin: entity:contact_message
  default_bundle: MY_DESTINATION_BUNDLE

As stated above, all of this works great when I use SqlBase in my plugin. But with FieldableEntity drush comes back with [error] No migrations found.

2
  • Maybe the namespace is wrong. Looking at other classes that extend FieldableEntity, such as Comment (api.drupal.org/api/drupal/…), shows the namespace to be \Drupal\[module]\Plugin\migrate\source\d7 – Jaypan Feb 23 at 0:01
  • Than you for pointing that out @Jaypan, I corrected the namespace but my issue remains. – Link Swanson Feb 23 at 2:32
1

I resolved my problem when I identified a mistake in my @MigrateSource annotation on my plugin. I had included my own custom migration module in the source_module entry of the plugin annotation. Here is what I did wrong:

/**
 * The 'mymodule_migrate_entityform' source plugin.
 *
 * @MigrateSource(
 *   id = "mymodule_migrate_entityform",
 *   source_module = "mymodule_migrate"
 * )
 */

Well I looked at the api docs on source_module

Identifies the system providing the data the source plugin will read.

The source plugin itself determines how the value is used. For example, Migrate Drupal's source plugins expect source_module to be the name of a module that must be installed and enabled in the source database.

It didn't work because source_module was set to my D9 custom migration module, which is obviously not on the D7 source, so the migration just silently gets removed from the list of migrations.

So I simply deleted the annotation line. My working plugin annotation looks like this now, and the plugin extends FieldableEntity and works as I had hoped it would.

/**
 * The 'mymodule_migrate_entityform' source plugin.
 *
 * @MigrateSource(
 *   id = "mymodule_migrate_entityform"
 * )
 */

I hope my plugin here will prove useful to someone migrating from D7 entityform to D8/9 core contact module (with contact_storage contrib module). It works.

1
  • Nice follow up. – Jaypan Feb 24 at 21:41

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.