Tablefield Data migration from D7 to D8

Created on 5 January 2019, over 5 years ago
Updated 30 June 2021, almost 3 years ago

A migrate process plugin to migrate an existing Tablefield data from D7 to D8 would be very helpful.

In the meatime, I created a following process plugin in my custom module that worked for me (replace my_module with name of your custom module you want to place process plugin into + read doc comments below to configure a migration of caption properly):

<?php

namespace Drupal\my_module\Plugin\migrate\process;

use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Row;

/**
 * Migrates Tablefield content from D7 to D8.
 *
 * @MigrateProcessPlugin(
 *   id = "tablefield"
 * )
 *
 * Optionally, "captionField" configuration can specify configuration target
 * field where caption should be stored (in D7 TableField, this was part of the
 * field data)
 *
 * Example usage:
 * @code
 *   field_table/value:
 *     plugin: tablefield
 *     captionField: field_caption
 *     source: field_table/0/value
 * @endcode
 */
class Tablefield extends ProcessPluginBase {

  /**
   * {@inheritdoc}
   */
  public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
    $data = unserialize($value);

    if (!$data) {
      return NULL;
    }

    $targetData = [];

    if (isset($this->configuration['captionField'])) {
      $row->setDestinationProperty($this->configuration['captionField'], $data['caption']);
    }

    foreach ($data as $key => $val) {
      if (strpos($key, 'cell_') === 0) {
        $keyParts = explode('_', $key);
        $x = $keyParts[1];
        $y = $keyParts[2];
        $targetData[$x][$y] = $val;
      }
    }

    return $targetData;
  }

}

✨ Feature request
Status

RTBC

Version

2.0

Component

Code

Created by

πŸ‡©πŸ‡ͺGermany mirsoft

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.69.0 2024