diff --git a/metatag.module b/metatag.module index 337c89f..811ffda 100644 --- a/metatag.module +++ b/metatag.module @@ -21,7 +21,10 @@ use Drupal\migrate\Plugin\migrate\source\SqlBase; use Drupal\migrate\Plugin\MigrateSourceInterface; use Drupal\migrate\Plugin\MigrationInterface; use Drupal\migrate\Row; +use Drupal\node\Plugin\migrate\source\d7\Node; +use Drupal\taxonomy\Plugin\migrate\source\d7\Term; use Drupal\taxonomy\TermInterface; +use Drupal\user\Plugin\migrate\source\d7\User; /** * Implements hook_help(). @@ -596,32 +599,42 @@ function metatag_generate_entity_metatags($entity) { */ function metatag_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) { // @todo Write a more general version rather than hard-coded. - $source_can_have_metatags = in_array($source->getPluginId(), [ - 'd7_node', - 'd7_node_revision', - 'd7_taxonomy_term', - 'd7_user' - ]); - if ($source_can_have_metatags && $migration->getDestinationPlugin() instanceof EntityContentBase) { + // Support a know subset of d7 sources. + if (is_a($source, Node::class)) { + // E.g. d7_node, d7_node_revision. + $source_type = 'node'; + } + elseif (is_a($source, Term::class)) { + // E.g. d7_taxonomy_term. + $source_type = 'taxonomy'; + } + elseif (is_a($source, User::class)) { + // E.g. d7_user. + $source_type = 'user'; + } + else { + // Not supported now, nothing to do. + return; + } + if ($migration->getDestinationPlugin() instanceof EntityContentBase) { $entity_type = NULL; $entity_id = NULL; $revision_id = NULL; // @todo Write a more general version rather than switch statement. - switch ($source->getPluginId()) { - case 'd7_node': - case 'd7_node_revision': + switch ($source_type) { + case 'node': $entity_type = 'node'; $entity_id = $row->getSourceProperty('nid'); $revision_id = $row->getSourceProperty('vid'); break; - case 'd7_taxonomy_term': + case 'taxonomy': $entity_type = 'taxonomy_term'; $entity_id = $row->getSourceProperty('tid'); break; - case 'd7_user': + case 'user': $entity_type = 'user'; $entity_id = $row->getSourceProperty('uid'); break; @@ -635,11 +648,26 @@ function metatag_migrate_prepare_row(Row $row, MigrateSourceInterface $source, M if (!is_null($revision_id)) { $query->condition('revision_id', $revision_id); } - $metatag_value = []; - foreach ($query->execute() as $metatag_row) { - $metatag_value[]['value'] = $metatag_row->data; + $metatag = []; + $data_entries = $query->execute()->fetchCol(); + foreach ($data_entries as $data_entry) { + // Re-shape D7 entries into for D8 entries. + // @todo This could live in a migrate process plugin. + foreach (unserialize($data_entry) as $metatag_name => $data) { + if (empty($data)) { + // Nothing to do. + continue; + } + if (!is_array($data) || empty($data['value'])) { + // @fixme Skip these values for now, maybe an some version supported + // these? + continue; + } + // Use the known 'value' key. + $metatag[$metatag_name] = $data['value']; + } } - $row->setSourceProperty('field_metatag', $metatag_value); + $row->setSourceProperty('field_metatag', serialize($metatag)); } } @@ -651,13 +679,16 @@ function metatag_migrate_prepare_row(Row $row, MigrateSourceInterface $source, M function metatag_migration_plugins_alter(array &$migrations) { foreach ($migrations as &$migration) { if (isset($migration['destination']['plugin'])) { + // Follow logic on hook_entity_base_field_info(), and exclude metatag itself. + if (in_array($migration['destination']['plugin'], ['entity:comment', 'entity:metatag'])) { + continue; + } $plugin_definition = \Drupal::service('plugin.manager.migrate.destination') ->getDefinition($migration['destination']['plugin']); $destination_plugin = DefaultFactory::getPluginClass($migration['destination']['plugin'], $plugin_definition); if (is_subclass_of($destination_plugin, EntityContentBase::class) || $destination_plugin == EntityContentBase::class) { $migration['process']['field_metatag'] = 'field_metatag'; - $migration['migration_dependencies']['required'][] = 'd7_metatag_field_instance'; } } }