Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate entities for node types doc and content type doc #325

Open
wants to merge 1 commit into
base: 8.x-1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
id: drupalfr_node_doc_d7
label: 'Nodes Documents (doc type in D7)'
audit: true
migration_group: drupalfr_d7
migration_tags:
- Drupal 7
- Content
deriver: Drupal\node\Plugin\migrate\D7NodeDeriver
source:
plugin: d7_node
node_type: doc
process:
# If you are using this file to build a custom migration consider removing
# the nid and vid fields to allow incremental migrations.
# In D7, nodes always have a tnid, but it's zero for untranslated nodes.
# We normalize it to equal the nid in that case.
# @see \Drupal\node\Plugin\migrate\source\d7\Node::prepareRow().
nid: vid
langcode:
plugin: default_value
source: language
default_value: "und"
title: title
uid: node_uid
status: status
created: created
changed: changed
promote: promote
sticky: sticky
revision_uid: revision_uid
revision_log: log
revision_timestamp: timestamp
type:
plugin: default_value
default_value: doc
body:
plugin: sub_process
source: body
process:
value: value
format:
-
plugin: default_value
default_value: basic_html
destination:
plugin: entity:node
default_bundle: doc
migration_dependencies:
required:
- d7_user
- d7_node_type
- drupalfr_nodetypes_d7
optional:
- d7_field_instance
- d7_comment_field_instance
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
id: drupalfr_nodetypes_d7
label: Node type configuration
migration_group: drupalfr_d7
migration_tags:
- Drupal 7
- Configuration
source:
plugin: drupalfr_nodetypes_d7
types:
- doc
constants:
preview: 1 # DRUPAL_OPTIONAL
process:
type: type
name: name
description: description
help: help
title_label: title_label
preview_mode: 'constants/preview'
display_submitted: display_submitted
new_revision: 'options/revision'
create_body: create_body
create_body_label: body_label
'third_party_settings/menu_ui/available_menus': available_menus
'third_party_settings/menu_ui/parent': parent
destination:
plugin: entity:node_type
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
langcode: fr
status: true
dependencies: { }
id: drupalfr_d7
label: Migrate content
description: 'A container for any migrations not explicitly assigned to a group.'
source_type: 'Drupal 7'
module: null
shared_configuration: null
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,22 @@
function drupalfr_migrate_d7_uninstall() {
/** @var \Drupal\Core\File\FileSystemInterface $file_system */
$file_system = \Drupal::service('file_system');
$files = $file_system->scanDirectory(drupal_get_path('module', 'drupalfr_migrate_d7') . "/config/install", "/.*\.yml/");
$files = $file_system->scanDirectory(drupal_get_path('module', 'drupalfr_migrate_d7') . '/config/install', "/.*\.yml/");
foreach ($files as $file) {
\Drupal::configFactory()->getEditable($file->name)->delete();
}

// Uninstall Migration Entities.
$group = \Drupal::entityTypeManager()->getStorage('migration_group')->load('drupalfr_d7');
if ($group) {
$group->delete();
}
$entity = \Drupal::entityTypeManager()->getStorage('migration')->load('drupalfr_nodetypes_d7');
if ($entity) {
$entity->delete();
}
$entity = \Drupal::entityTypeManager()->getStorage('migration')->load('drupalfr_node_doc_d7');
if ($entity) {
$entity->delete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
drupalfr_migrate_d7.entities_form:
path: '/drupalfr_migrate_d7/form/entities'
defaults:
_form: '\Drupal\drupalfr_migrate_d7\Form\DeleteEntitiesForm'
_title: 'EntitiesForm'
requirements:
_access: 'TRUE'
23 changes: 23 additions & 0 deletions www/modules/custom/drupalfr_migrate_d7/includes/nodes.batch.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* @file
* Include to delete all nodes of a type.
*/

/**
* Delete nodes multiple.
*/
function delete_nodes_multiple($nids) {
$storage_controller = \Drupal::service('entity_type.manager')->getStorage('node');

$nodes = $storage_controller->loadMultiple($nids);
$storage_controller->delete($nodes);
}

/**
* Message at end of batch.
*/
function finish_batch($nids): void {
\Drupal::service('messenger')->addMessage(sprintf('%s nodes deleted.', count($nids)));
}
151 changes: 151 additions & 0 deletions www/modules/custom/drupalfr_migrate_d7/src/Form/DeleteEntitiesForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
<?php

namespace Drupal\drupalfr_migrate_d7\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Class EntitiesForm.
*/
class DeleteEntitiesForm extends FormBase {

private const DELETE_NUMBER_MAX = 300;

/**
* Drupal\Core\Entity\EntityTypeManagerInterface definition.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;

/**
* Drupal\Core\Messenger\Messenger definition.
*
* @var \Drupal\Core\Messenger\Messenger
*/
protected $messenger;

/**
* Drupal\Core\Database\Connection definition.
*
* @var \Drupal\Core\Database\Connection
*/
protected $database;

/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
$instance = parent::create($container);
$instance->entityTypeManager = $container->get('entity_type.manager');
$instance->messenger = $container->get('messenger');
$instance->database = $container->get('database');

return $instance;
}

/**
* {@inheritdoc}
*/
public function getFormId() {
return 'entities_form';
}

/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$options = $this->getNodesTypes();
if (empty($options)) {
return [
'#type' => 'markup',
'#markup' => $this->t('No entities existing.'),
];
}

$form['deletable_nodes'] = [
'#type' => 'checkboxes',
'#title' => $this->t('Nodes types to delete'),
'#description' => $this->t('Choose nodes types and contents to delete'),
'#options' => $options,
'#required' => TRUE,
'#weight' => '0',
];

$form['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Submit'),
];
$this->getNodesTypes();

return $form;
}

/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
// Display result.
$selection = $form_state->getValue('deletable_nodes');
$selections = [];
foreach ($selection as $sel) {
if (0 !== $sel) {
$selections[] = $sel;
}
}
$this->deleteNodesByTypes($selections);
}

/**
* Get Nodes Types.
*/
private function getNodesTypes() {
$query = $this->entityTypeManager->getStorage('node')->getAggregateQuery()
->groupBy('type');
$result = $query->execute();
$types = [];
foreach ($result as $res) {
$types[$res['type']] = $res['type'];
}
foreach ($types as $key => $type) {
$res = $this->database->select('node', 'n')
->fields('n', ['nid'])
->condition('type', $key)
->execute()
->fetchAll();
$types[$key] = sprintf('%s (%s nodes)', $key, count($res));
}
return $types;
}

/**
* Delete Nodes by types.
*/
private function deleteNodesByTypes($types) {
$res = $this->database->select('node', 'n')
->fields('n', ['nid'])
->condition('type', $types, 'in')
->execute()
->fetchAllKeyed();
$nids = array_keys($res);

$batch = [
'title' => $this->t('Deleting nodes'),
'init_message' => $this->t('Get nodes to delete.'),
'progress_message' => $this->t('Completed step @current of @total.'),
'file' => drupal_get_path('module', 'drupalfr_migrate_d7') . '/includes/nodes.batch.inc',
'finished' => ['finishBatch', $nids],
];
while (count($nids) > 0) {
$numberToExtract = count($nids) >= self::DELETE_NUMBER_MAX ? self::DELETE_NUMBER_MAX : count($nids);
$extract = array_slice($nids, 0, $numberToExtract);
array_splice($nids, 0, $numberToExtract);
$batch['operations'][] = ['delete_nodes_multiple', [$extract]];
$batch['error_message'] = $this->t('Error deleting nodes.');
}
batch_set($batch);
}

}
Loading