migrate_plugins-8.x-1.x-dev/migrate_plugins.module

migrate_plugins.module
<?php

/**
 * @file
 * Contains migrate_plugins.module.
 */

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\migrate\Plugin\MigrateSourceInterface;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\migrate\Row;

/**
 * Implements hook_help().
 */
function migrate_plugins_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    // Main module help for the migrate_plugins module.
    case 'help.page.migrate_plugins':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provides migrate source and processor plugins.') . '</p>';
      return $output;

    default:
  }
}

/**
 * Implements hook_migrate_prepare_row().
 */
function migrate_plugins_migrate_prepare_row(Row $row, MigrateSourceInterface $source, MigrationInterface $migration) {
  $references = [];
  // Get the source primary ID key.
  $source_ids = array_keys($source->getIds());
  $source_primary_id = $source_ids[0];
  // Migrations IDS that have OG reference fields to process.
  $migration_ids = [
    'iul_users' => 'user',
    'iul_node_subject_posts' => 'node',
    'iul_node_news' => 'node',
    'iul_node_user_profiles' => 'node',
    'iul_node_basic_pages' => 'node',
    'iul_node_events' => 'node',
    'iul_node_services' => 'node',
    'iul_node_onsite_exhibit' => 'node',
    'iul_node_digital_exhibit' => 'node',
    'iul_node_feed_items' => 'node',
  ];
  $name_patterns = "^og_|^field_affiliation$";

  // Alter the migrations that has OG reference fields that needs
  // custom select to load the OG references properly.
  if (isset($migration_ids[$migration->id()])) {
    $source_data = $row->getSource();
    $field_names = array_keys($source_data);
    $entity_type = $migration_ids[$migration->id()];

    // Identify the OG reference fields.
    foreach ($field_names as $field_name) {
      // For OG fields we need to run a query on og_membership
      // table and get the reference values.
      if (preg_match("/$name_patterns/", $field_name)) {
        $params = [
          ':name' => $field_name,
          ':entity_type' => $entity_type,
          ':group_type' => 'node',
          ':eid' => $row->getSourceProperty($source_primary_id),
        ];

        $select = 'SELECT gid FROM {og_membership}
        WHERE field_name = :name AND etid = :eid
        AND entity_type = :entity_type AND group_type = :group_type';

        // @var \Drupal\Core\Database\Statement $query
        $query = $source->getDatabase()->query($select, $params);
        $values = $query->fetchAll();

        foreach ($values as $value) {
          $references[] = ['target_id' => $value->gid];
        }

        // Set the OG references as property value.
        if ($references) {
          $row->setSourceProperty($field_name, $references);
        }
      }
    }
  }

  // Get the source entity alias, we support the most common.
  // @var \Drupal\file_entity\Plugin\migrate\source\d7\FileEntity $source
  $plugin_definition = $source->getPluginDefinition();
  $source_module = $source->getSourceModule();

  // Fallback on source_provider when source module not defined.
  if (empty($source_module)) {
    $source_module = $plugin_definition['source_provider'];
  }

  switch ($source_module) {
    case 'node':
      $entity_id = $row->getSourceProperty('nid');
      $source_path = "node/{$entity_id}";
      break;

    case 'user':
      $entity_id = $row->getSourceProperty('uid');
      $source_path = "user/{$entity_id}";
      break;

    case 'taxonomy':
      $entity_id = $row->getSourceProperty('tid');
      $source_path = "taxonomy/term/{$entity_id}";
      break;

    case 'file_entity':
      $entity_id = $row->getSourceProperty('fid');
      $source_path = "file/{$entity_id}";
      break;

    default:
      $source_path = NULL;
      break;
  }

  // Query for URL alias retrieval.
  if ($source_path) {
    $query = $source->getDatabase()->select('url_alias', 'ua');
    $query->fields('ua', ['alias']);
    $query->condition('ua.source', $source_path);
    $alias = $query->execute()->fetchField();

    // Set the alias property.
    if (!empty($alias)) {
      $row->setSourceProperty('path_alias', '/' . $alias);
    }
  }
}

/**
 * Extract the relative path using 'files' as root directory.
 *
 * @param string $filepath
 *   The file path string.
 *
 * @return string
 *   The file path relative to files directory.
 */
function migrate_plugins_extract_relative_path($filepath) {
  return str_replace('sites/default/files/', '', $filepath);
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc