You are here

function metatag_migrate_prepare_row in Metatag 8

Implements hook_migrate_prepare_row().

File

./metatag.module, line 698
Contains metatag.module.

Code

function metatag_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {

  // Don't bother if there source doesn't allow the getDatabase() method.
  if (!method_exists($source, 'getDatabase')) {
    return;
  }

  // Work out what sort of migration to do. Cache the results of this logic so
  // that it isn't checked on every single row being processed.
  $metatag_table_exists =& drupal_static('metatag_migrate_prepare_row_metatag_table_exists');
  $nodewords_table_exists =& drupal_static('metatag_migrate_prepare_row_nodewords_table_exists');
  if (!isset($metatag_table_exists)) {
    $metatag_table_exists = $source
      ->getDatabase()
      ->schema()
      ->tableExists('metatag');
    $nodewords_table_exists = $source
      ->getDatabase()
      ->schema()
      ->tableExists('nodewords');
  }

  // The source is Metatag-D7.
  if ($metatag_table_exists) {

    // @todo Write a more general version rather than hard-coded.
    // Support a know subset of D7 sources.
    if (is_a($source, Node7::class)) {

      // E.g. d7_node, d7_node_revision.
      $source_type = 'node';
    }
    elseif (is_a($source, Term7::class)) {

      // E.g. d7_taxonomy_term.
      $source_type = 'taxonomy';
    }
    elseif (is_a($source, User7::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 a switch statement.
      switch ($source_type) {
        case 'node':
          $entity_type = 'node';
          $entity_id = $row
            ->getSourceProperty('nid');
          $revision_id = $row
            ->getSourceProperty('vid');
          break;
        case 'taxonomy':
          $entity_type = 'taxonomy_term';
          $entity_id = $row
            ->getSourceProperty('tid');
          break;
        case 'user':
          $entity_type = 'user';
          $entity_id = $row
            ->getSourceProperty('uid');
          break;
      }

      /** @var \Drupal\migrate\Plugin\migrate\source\SqlBase $source */

      /** @var \Drupal\Core\Database\Query\SelectInterface $query */
      $query = $source
        ->getDatabase()
        ->select('metatag', 'm')
        ->fields('m', [
        'data',
      ])
        ->condition('entity_type', $entity_type)
        ->condition('entity_id', $entity_id);
      if (!is_null($revision_id)) {
        if ($source
          ->getDatabase()
          ->schema()
          ->fieldExists('metatag', 'revision_id')) {
          $query
            ->condition('revision_id', $revision_id);
        }
      }
      $value = $query
        ->execute()
        ->fetchCol();
      if (!empty($value) && is_array($value)) {
        $value = array_pop($value);
      }
      $row
        ->setSourceProperty('pseudo_metatag_entities', $value);
    }
  }
  elseif ($nodewords_table_exists) {

    // @todo Write a more general version rather than hard-coded.
    // Support a know subset of D6 sources.
    if (is_a($source, Node6::class)) {

      // E.g. d6_node, d6_node_revision.
      $source_type = 'node';
    }
    elseif (is_a($source, Term6::class)) {

      // E.g. d6_taxonomy_term.
      $source_type = 'taxonomy_term';
    }
    elseif (is_a($source, User6::class)) {

      // E.g. d6_user.
      $source_type = 'user';
    }
    else {

      // Not supported now, nothing to do.
      return;
    }
    if ($migration
      ->getDestinationPlugin() instanceof EntityContentBase) {
      $nodeword_type = $entity_id = NULL;

      // @todo Write a more general version rather than a switch statement.
      switch ($source_type) {
        case 'node':

          // define('NODEWORDS_TYPE_NODE',       5);
          $nodeword_type = 5;
          $entity_id = $row
            ->getSourceProperty('nid');
          break;
        case 'taxonomy_term':

          // define('NODEWORDS_TYPE_TERM',       6);
          $nodeword_type = 6;
          $entity_id = $row
            ->getSourceProperty('tid');
          break;
        case 'user':

          // define('NODEWORDS_TYPE_USER',       8);
          $nodeword_type = 8;
          $entity_id = $row
            ->getSourceProperty('uid');
          break;
      }

      // @todo
      // define('NODEWORDS_TYPE_BLOG',       13);
      // define('NODEWORDS_TYPE_DEFAULT',    1);
      // define('NODEWORDS_TYPE_ERRORPAGE',  2);
      // define('NODEWORDS_TYPE_FORUM',      12);
      // define('NODEWORDS_TYPE_FRONTPAGE',  3);
      // define('NODEWORDS_TYPE_NONE',       0);
      // define('NODEWORDS_TYPE_OFFLINE',    11);
      // define('NODEWORDS_TYPE_PAGE',       10);
      // define('NODEWORDS_TYPE_PAGER',      4);
      // define('NODEWORDS_TYPE_TRACKER',    7);
      // define('NODEWORDS_TYPE_VOCABULARY', 9);

      /** @var \Drupal\migrate\Plugin\migrate\source\SqlBase $source */

      /** @var \Drupal\Core\Database\Query\SelectInterface $query */
      $query = $source
        ->getDatabase()
        ->select('nodewords', 'nw')
        ->fields('nw', [
        'name',
        'content',
      ])
        ->condition('type', $nodeword_type)
        ->condition('id', $entity_id)
        ->orderBy('nw.name');
      $value = $query
        ->execute()
        ->fetchAllKeyed();
      $row
        ->setSourceProperty('pseudo_metatag_entities', $value);
    }
  }
}