Commit d545b1fe authored by Beatriz Varela's avatar Beatriz Varela Committed by owenpm3
Browse files

Issue #3125560: \Drupal calls should be avoided in classes, use dependency injection instead

parent 0a440de0
Loading
Loading
Loading
Loading
+22 −7
Original line number Diff line number Diff line
Queue Import

This module is meant to give another option to migrating content into Drupal 8/9 sites. It uses the Queue API and Drush commands to queue content and then use QueueWorkers to use that queued data to create entities on the new site.
This module is meant to give another option to migrating content into Drupal 8/9
sites. It uses the Queue API and Drush commands to queue content and then use
QueueWorkers to use that queued data to create entities on the new site.

Currently, the module can be used to see an example of queueing and importing content by enabling the module and running the drush command of "drush itest" to import a single article into the site.
Currently, the module can be used to see an example of queueing and importing
content by enabling the module and running the drush command of "drush itest"
to import a single article into the site.

The piece that's currently in progress is a way to plug in a database connection to a legacy Drupal 7 database and queue content directly out of that. The trick is creating the QueueWorkers with the correct field. At this time, I'm working on the automation of the creation of those workers.
The piece that's currently in progress is a way to plug in a database connection
to a legacy Drupal 7 database and queue content directly out of that. The trick
is creating the QueueWorkers with the correct field. At this time, I'm working
on the automation of the creation of those workers.

Essentially, we pull the fields from the D7 db and create a QueueWorker with those fields mapped out along with a list of currently available fields from the D8/D9 site. That, along with the methods for converting that queued data into taxonomy terms or media entities, will allow you to begin importing content quickly and in a way that will you allow you to update the content on any follow-up imports based on mapping the legacy Node ID.
Essentially, we pull the fields from the D7 db and create a QueueWorker with
those fields mapped out along with a list of currently available fields from the
D8/D9 site. That, along with the methods for converting that queued data into
taxonomy terms or media entities, will allow you to begin importing content
quickly and in a way that will you allow you to update the content on any
follow-up imports based on mapping the legacy Node ID.

I'll continue to update the information here as we get farther along.

@@ -16,9 +28,12 @@ Instructions after installing the module:
 - Run "drush queue-run node_queue_processor -v"
 - Navigate to the form at admin/config/development/queue-import
 - Fill in the information for a database connection (must be Drupal 7)
 - Run "drush d7fm article" (article will create MapArticleQueueProcessor.php, page would create MapPageQueueProcessor.php, etc.)
 - Fix the mapping in MapArticleQueueProcessor.php and rename to ArticleQueueProcessor.php
 - Run "drush d7fm article" (article will create MapArticleQueueProcessor.php,
 page would create MapPageQueueProcessor.php, etc.)
 - Fix the mapping in MapArticleQueueProcessor.php and rename to
 ArticleQueueProcessor.php
 - Run "drush d7 article 1" (content_type and query limit for how many to queue)
 - Run "drush queue-run article_queue_processor"
 - Check admin/content for new content and review errors
 - If needed, you can remove all imported content with "drush qicount article --delete"
 - If needed, you can remove all imported content with "drush qicount article
 --delete"
+1 −0
Original line number Diff line number Diff line
services:
  queue_import.commands:
    class: \Drupal\queue_import\Commands\QueueImportCommands
    arguments: ['@queue', '@module_handler', '@node_queue_processor', '@node', '@media', '@entity_type.manager']
    tags:
      - { name: drush.command }
+8 −6
Original line number Diff line number Diff line
<?php

/**
 @file
 Contains queue_import.module.
 * @file
 * Contains queue_import.module.
 */

use Drupal\Core\Field\FieldConfigInterface;
@@ -10,6 +10,9 @@ use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;

/**
 * Custom function for import install.
 */
function queue_import_install() {
  // If we've already created the field, don't create it again.
  if (!FieldStorageConfig::loadByName('node', 'field_migration_id')) {
@@ -47,7 +50,7 @@ function queue_import_install() {
/**
 * Custom function for finding field existence.
 */
function checkFieldExists($content_type) {
function check_Field_Exists($content_type) {
  $entityManager = \Drupal::service('entity_field.manager');
  $fields = [];

@@ -55,8 +58,7 @@ function checkFieldExists($content_type) {
    $fields = array_filter(
      $entityManager->getFieldDefinitions('node', $content_type),
      function ($field_definition) {
        return
          $field_definition instanceof FieldConfigInterface;
        return $field_definition instanceof FieldConfigInterface;
      }
    );
  }
@@ -65,7 +67,7 @@ function checkFieldExists($content_type) {
}

/**
 Implements hook_help().
 * Implements hook_help().
 */
function queue_import_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
+57 −7
Original line number Diff line number Diff line
@@ -4,6 +4,10 @@ namespace Drupal\queue_import\Commands;

use Drupal\queue_import\Controller\DataSourceConnection;
use Drush\Commands\DrushCommands;
use Drupal\Core\Queue\QueueFactory;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Entity\EntityTypeManager;

/**
 * A Drush commandfile.
@@ -14,6 +18,52 @@ use Drush\Commands\DrushCommands;
 */
class QueueImportCommands extends DrushCommands {

  /**
   * Queue service.
   *
   * @var \Drupal\Core\Queue\QueueFactory
   */
  protected $queueFactory;

  /**
   * Module handler service.
   *
   * @var \Drupal\Core\Extension\ModuleHandler
   */
  protected $moduleHandler;

  /**
   * Node queue processor service.
   *
   * @var \Drupal\queue_import\Plugin\QueueWorker\NodeQueueProcessor
   */
  protected $nodeQueueProcessor;

  /**
   * Entity query service.
   *
   * @var \Drupal\Core\Entity\Query\QueryInterface
   */
  protected $entityQuery;

  /**
   * Entity type manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * {@inheritdoc}
   */
  public function __construct(QueueFactory $queueFactory, ModuleHandler $moduleHandler, NodeQueueProcessor $nodeQueueProcessor, QueryInterface $entityQuery, EntityTypeManager $entityTypeManager) {
    $this->queueFactory = $queueFactory;
    $this->moduleHandler = $moduleHandler;
    $this->nodeQueueProcessor = $nodeQueueProcessor;
    $this->entityQuery = $entityQuery;
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * Importing Drupal content.
   *
@@ -26,7 +76,7 @@ class QueueImportCommands extends DrushCommands {
   */
  public function importDrupalContent($content_type = 'node', $limit = 1) {
    $queue = $content_type . '_queue_processor';
    $queue = \Drupal::queue($queue);
    $queue = $this->queueFactory->$queue;
    $queue->createQueue();

    $query = "select nid, type from node where type = '$content_type' order by nid desc limit $limit";
@@ -53,7 +103,7 @@ class QueueImportCommands extends DrushCommands {
  public function fieldMap($content_type) {
    $queue_worker_builder = DataSourceConnection::queueWorkerGenerator($content_type);

    $module_handler = \Drupal::service('module_handler');
    $module_handler = $this->moduleHandler;
    $module_path = $module_handler->getModule('queue_import')->getPath();
    $file = "$module_path/src/Plugin/QueueWorker/Map" . ucfirst($content_type) . "QueueProcessor.php";

@@ -80,7 +130,7 @@ class QueueImportCommands extends DrushCommands {
      'migration_id' => 1,
    ];

    $queue = \Drupal::queue('node_queue_processor');
    $queue = $this->nodeQueueProcessor;
    $queue->createQueue();
    $queue->createItem($data);
  }
@@ -100,7 +150,7 @@ class QueueImportCommands extends DrushCommands {
   */
  public function deleteNodes($content_type, $options = ['delete' => FALSE]) {
    if ($content_type !== 'image') {
      $result = \Drupal::entityQuery('node')
      $result = $this->entityQuery
        ->condition('type', $content_type)
        ->execute();

@@ -108,7 +158,7 @@ class QueueImportCommands extends DrushCommands {
        // entity_delete_multiple('node', $result);.
        $result = array_slice($result, 0, 1000);

        $storage_handler = \Drupal::entityTypeManager()->getStorage("node");
        $storage_handler = $this->entityTypeManager->getStorage("node");
        $entities = $storage_handler->loadMultiple($result);
        $storage_handler->delete($entities);

@@ -119,7 +169,7 @@ class QueueImportCommands extends DrushCommands {
      }
    }
    else {
      $result = \Drupal::entityQuery('media')
      $result = $this->entityQuery
        ->condition('bundle', $content_type)
        ->execute();

@@ -127,7 +177,7 @@ class QueueImportCommands extends DrushCommands {
        // entity_delete_multiple('node', $result);.
        $result = array_slice($result, 0, 1000);

        $storage_handler = \Drupal::entityTypeManager()->getStorage("media");
        $storage_handler = $this->entityTypeManager->getStorage("media");
        $entities = $storage_handler->loadMultiple($result);
        $storage_handler->delete($entities);

+2 −1
Original line number Diff line number Diff line
@@ -5,7 +5,7 @@ namespace Drupal\queue_import\Controller;
use Drupal\Core\Controller\ControllerBase;

/**
 * Class DataSourceConnection.
 * Those class basically create the fields list.
 */
class DataSourceConnection extends ControllerBase {

@@ -118,4 +118,5 @@ class DataSourceConnection extends ControllerBase {
    }

  }

}
Loading