Hi All, during migration drupal 7 to drupal 8 Apigee Kickstart custom fields are not migrating with d7_field_instance properly. I have developed a custom plugin for faq content type from d7 to Drupal 8 Apigee kickstart profile

First install migration_tool, and migrate_plus module inside you Apigee_kickstart and enable migrate ,druapl_migrate and migration_tool migrate_plus.

step1: create migrate_plus.migration.migrate_faq_custom_fields.yml file

id: migrate_faq_custom_fields
label: Field configuration
migration_tags:
- Drupal 7
- Configuration
class: Drupal\migrate_drupal\Plugin\migrate\FieldMigration
field_plugin_method: alterFieldMigration
source:
plugin: migrate_faq_custom_fields
bundle: faq
constants:
status: true
langcode: und
process:
entity_type: entity_type
status: 'constants/status'
langcode: 'constants/langcode'
field_name: field_name
type:
plugin: process_field
source: type
method: getFieldType
# Translatable is not migrated and the Drupal 8 default of true is used.
# If translatable is false in field storage then the field can not be
# set to translatable via the UI.
#translatable: translatable
cardinality: cardinality
settings:
plugin: d7_field_settings
destination:
plugin: entity:field_storage_config
Step Two : Create plugin:
File name: field.php
<?php

namespace Drupal\migrate_faq_custom_fields\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
* Drupal 7 field source from database.
*
* @MigrateSource(
* id = "migrate_faq_custom_fields",
* source_module = "field_sql_storage"
* )
*/
class Field extends DrupalSqlBase {

/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('field_config', 'fc')
->distinct()
->fields('fc')
->fields('fci', ['entity_type'])
->condition('fc.active', 1)
->condition('fc.storage_active', 1)
->condition('fc.deleted', 0)
->condition('fci.deleted', 0)
->condition('fci.bundle', $this->configuration['bundle'])//adding condtion with bundle value provided in yml file
->condition('fci.field_name', array('body'),"NOT IN");//excluding faq body field as in d8 it has replced by filed_answer
$query->join('field_config_instance', 'fci', 'fc.id = fci.field_id');

// If the Drupal 7 Title module is enabled, we don't want to migrate the
// fields it provides. The values of those fields will be migrated to the
// base fields they were replacing.
if ($this->moduleExists('title')) {
$title_fields = [
'title_field',
'name_field',
'description_field',
'subject_field',
];
$query->condition('fc.field_name', $title_fields, 'NOT IN');
}

return $query;
}

/**
* {@inheritdoc}
*/
public function fields() {
return [
'id' => $this->t('The field ID.'),
'field_name' => $this->t('The field name.'),
'type' => $this->t('The field type.'),
'module' => $this->t('The module that implements the field type.'),
'active' => $this->t('The field status.'),
'storage_type' => $this->t('The field storage type.'),
'storage_module' => $this->t('The module that implements the field storage type.'),
'storage_active' => $this->t('The field storage status.'),
'locked' => $this->t('Locked'),
'data' => $this->t('The field data.'),
'cardinality' => $this->t('Cardinality'),
'translatable' => $this->t('Translatable'),
'deleted' => $this->t('Deleted'),
'instances' => $this->t('The field instances.'),
];
}

/**
* {@inheritdoc}
*/
public function prepareRow(Row $row, $keep = TRUE) {
foreach (unserialize($row->getSourceProperty('data')) as $key => $value) {
$row->setSourceProperty($key, $value);
}

$instances = $this->select('field_config_instance', 'fci')
->fields('fci')
->condition('field_name', $row->getSourceProperty('field_name'))
->condition('entity_type', $row->getSourceProperty('entity_type'))
->execute()
->fetchAll();
$row->setSourceProperty('instances', $instances);

return parent::prepareRow($row);
}

/**
* {@inheritdoc}
*/
public function getIds() {
return [
'field_name' => [
'type' => 'string',
'alias' => 'fc',
],
'entity_type' => [
'type' => 'string',
'alias' => 'fci',
],
];
}

}
Step three: create migrate_plus.migration.migrate_faq_custom_fields_instance.yml
id: migrate_faq_custom_fields_instance
label: Field instance configuration
migration_group: migrate
field_plugin_method: alterFieldInstanceMigration
source:
plugin: migrate_faq_custom_fields_instance
bundle: faq
constants:
status: true
process:
type:
plugin: process_field
source: type
method: getFieldType
entity_type: entity_type
field_name: field_name
# The bundle needs to be statically mapped in order to support comment types
# that might already exist before this migration is run. See
# d7_comment_type.yml for more information.
bundle:
plugin: static_map
source: bundle
bypass: true
map:
comment_node_forum: comment_forum
label: label
description: description
required: required
status: 'constants/status'
allowed_values:
-
plugin: sub_process
source: allowed_vid
process:
-
plugin: migration_lookup
migration: d7_taxonomy_vocabulary
source: vid
settings:
plugin: d7_field_instance_settings
source:
- settings
- widget
- field_definition
default_value_function: ''
default_value:
plugin: d7_field_instance_defaults
source:
- default_value
- widget
translatable: translatable
destination:
plugin: entity:field_config
migration_dependencies:
required:
- migrate_faq_custom_fields

Step4: Create FieldInstance.php
<?php

namespace Drupal\migrate_faq_custom_fields\Plugin\migrate\source;

use Drupal\migrate\Row;
use Drupal\migrate_drupal\Plugin\migrate\source\DrupalSqlBase;

/**
* Drupal 7 field instances source from database.
*
* @MigrateSource(
* id = "migrate_faq_custom_fields_instance",
* source_module = "field"
* )
*/
class FieldInstance extends DrupalSqlBase {

/**
* {@inheritdoc}
*/
public function query() {
$query = $this->select('field_config_instance', 'fci')
->fields('fci')
->fields('fc', ['type', 'translatable'])
->condition('fc.active', 1)
->condition('fc.storage_active', 1)
->condition('fc.deleted', 0)
->condition('fci.deleted', 0)
->condition('fci.bundle', $this->configuration['bundle'])//adding condtion with bundle value provided in yml file
->condition('fci.field_name', array('body'),"NOT IN");//excluding faq body field as in d8 it has replced by filed_answer
$query->join('field_config', 'fc', 'fci.field_id = fc.id');

// Optionally filter by entity type and bundle.
if (isset($this->configuration['entity_type'])) {
$query->condition('fci.entity_type', $this->configuration['entity_type']);

if (isset($this->configuration['bundle'])) {
$query->condition('fci.bundle', $this->configuration['bundle']);
}
}

// If the Drupal 7 Title module is enabled, we don't want to migrate the
// fields it provides. The values of those fields will be migrated to the
// base fields they were replacing.
if ($this->moduleExists('title')) {
$title_fields = [
'title_field',
'name_field',
'description_field',
'subject_field',
];
$query->condition('fc.field_name', $title_fields, 'NOT IN');
}

return $query;
}

/**
* {@inheritdoc}
*/
protected function initializeIterator() {
$results = $this->prepareQuery()->execute()->fetchAll();

// Group all instances by their base field.
$instances = [];
foreach ($results as $result) {
$instances[$result['field_id']][] = $result;
}

// Add the array of all instances using the same base field to each row.
$rows = [];
foreach ($results as $result) {
$result['instances'] = $instances[$result[3]];
$rows[] = $result;
}

return new \ArrayIterator($rows);
}

/**
* {@inheritdoc}
*/
public function fields() {
return [
'id' => $this->t('The field instance ID.'),
'field_id' => $this->t('The field ID.'),
'field_name' => $this->t('The field name.'),
'entity_type' => $this->t('The entity type.'),
'bundle' => $this->t('The entity bundle.'),
'data' => $this->t('The field instance data.'),
'deleted' => $this->t('Deleted'),
'type' => $this->t('The field type'),
'instances' => $this->t('The field instances.'),
'field_definition' => $this->t('The field definition.'),
];
}

/**
* {@inheritdoc}
*/
public function prepareRow(Row $row) {
foreach (unserialize($row->getSourceProperty('data')) as $key => $value) {
$row->setSourceProperty($key, $value);
}

$field_definition = $this->select('field_config', 'fc')
->fields('fc')
->condition('id', $row->getSourceProperty('field_id'))
->execute()
->fetch();
$row->setSourceProperty('field_definition', $field_definition);

// Determine the translatable setting.
$translatable = FALSE;
if ($row->getSourceProperty('entity_type') == 'node') {
$language_content_type_bundle = (int) $this->variableGet('language_content_type_' . $row->getSourceProperty('bundle'), 0);
// language_content_type_[bundle] may be
// - 0: no language support
// - 1: language assignment support
// - 2: node translation support
// - 4: entity translation support
if ($language_content_type_bundle === 2 || ($language_content_type_bundle === 4 && $row->getSourceProperty('translatable'))) {
$translatable = TRUE;
}
}
else {
// This is not a node entity. Get the translatable value from the source
// field_config table.
$field_data = unserialize($field_definition['data']);
$translatable = $field_data['translatable'];
}

// Check if this is an i18n synchronized field.
$synchronized_fields = $this->variableGet('i18n_sync_node_type_' . $row->getSourceProperty('bundle'), NULL);
if ($synchronized_fields) {
if (in_array($row->getSourceProperty('field_name'), $synchronized_fields)) {
$translatable = FALSE;
}
}
$row->setSourceProperty('translatable', $translatable);

// Get the vid for each allowed value for taxonomy term reference fields
// which is used in a migration_lookup in the process pipeline.
if ($row->getSourceProperty('type') == 'taxonomy_term_reference') {
$vocabulary = [];
$data = unserialize($field_definition['data']);
foreach ($data['settings']['allowed_values'] as $allowed_value) {
$vocabulary[] = $allowed_value['vocabulary'];
}
$query = $this->select('taxonomy_vocabulary', 'v')
->fields('v', ['vid'])
->condition('machine_name', $vocabulary, 'IN');
$allowed_vid = $query->execute()->fetchAllAssoc('vid');
$row->setSourceProperty('allowed_vid', $allowed_vid);

// If there is an i18n_mode use it to determine if this field is
// translatable. It is TRUE for i18n_modes 'Vocab Fixed' and 'Translate',
// for all others it is FALSE. When there is a term reference field with
// two vocabularies where one vocabulary is translatable and other is not
// the field itself is set to not translatable. Note mode '5' is not used
// for taxonomy but is listed here for completeness.
// - 0: No multilingual options.
// - 1: Localize. Localizable object.
// - 2: Fixed Language.
// - 4: Translate. Multilingual objects.
// - 5: Objects are translatable, if they have language or localizable
// if not)
if ($this->getDatabase()
->schema()
->fieldExists('taxonomy_vocabulary', 'i18n_mode')) {
$query = $this->select('taxonomy_vocabulary', 'v')
->fields('v', ['i18n_mode'])
->condition('machine_name', $vocabulary, 'IN');
$results = $query->execute()->fetchAllAssoc('i18n_mode');
$translatable = FALSE;
foreach ($results as $result) {
if ($result['i18n_mode'] == '2' || $result['i18n_mode'] == '4') {
$translatable = TRUE;
}
}
$row->setSourceProperty('translatable', $translatable);
}
}

return parent::prepareRow($row);
}

/**
* {@inheritdoc}
*/
public function getIds() {
return [
'entity_type' => [
'type' => 'string',
'alias' => 'fci',
],
'bundle' => [
'type' => 'string',
'alias' => 'fci',
],
'field_name' => [
'type' => 'string',
'alias' => 'fci',
],
];
}

/**
* {@inheritdoc}
*/
public function count($refresh = FALSE) {
return $this->initializeIterator()->count();
}

}

and run the migration command
drush mim migrate_faq_custom_fields
drush mim migrate_faq_custom_fields_instance

Thank you!

Comments

shreepa.bss@gmail.com’s picture

Issue summary: View changes