You are here

public function MetatagEntities::transform in Metatag 8

Performs the associated process.

Parameters

mixed $value: The value to be transformed.

\Drupal\migrate\MigrateExecutableInterface $migrate_executable: The migration in which this process is being executed.

\Drupal\migrate\Row $row: The row from the source to process. Normally, just transforming the value is adequate but very rarely you might need to change two columns at the same time or something like that.

string $destination_property: The destination property currently worked on. This is only used together with the $row above.

Return value

string|array The newly transformed value.

Overrides ProcessPluginBase::transform

File

src/Plugin/migrate/process/d7/MetatagEntities.php, line 24

Class

MetatagEntities
Migrate entity data from Metatag on D7.

Namespace

Drupal\metatag\Plugin\migrate\process\d7

Code

public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {

  // If there's no data, there's no need to store anything.
  if (empty($value)) {
    return NULL;
  }

  // Re-shape D7 entries into for D8 entries.
  $old_tags = unserialize($value);

  // This is expected to be an array, if it isn't then something went wrong.
  if (!is_array($old_tags)) {
    throw new MigrateException('Data from Metatag-D7 was not a serialized array.');
  }
  $tags_map = $this
    ->tagsMap();
  $metatags = [];
  foreach ($old_tags as $d7_metatag_name => $metatag_value) {

    // If there's no data for this tag, ignore everything.
    if (empty($metatag_value)) {
      continue;
    }

    // @todo Skip these values for now, maybe some version supported these?
    if (!is_array($metatag_value) || empty($metatag_value['value'])) {
      continue;
    }

    // Convert the D7 meta tag name to the D8 equivalent. If this meta tag
    // is not recognized, skip it.
    if (empty($tags_map[$d7_metatag_name])) {
      continue;
    }
    $d8_metatag_name = $tags_map[$d7_metatag_name];

    // Convert the nested arrays to a flat structure.
    if (is_array($metatag_value['value'])) {

      // Remove empty values.
      $metatag_value['value'] = array_filter($metatag_value['value']);

      // Convert the array into a comma-separated list.
      $metatag_value = implode(', ', $metatag_value['value']);
    }
    else {
      $metatag_value = $metatag_value['value'];
    }
    if (!Unicode::validateUtf8($metatag_value)) {
      $metatag_value = Unicode::convertToUtf8($metatag_value, 'Windows-1252');
    }

    // Keep the entire data structure.
    $metatags[$d8_metatag_name] = $metatag_value;
  }
  return serialize($metatags);
}