Skip to content

Commit

Permalink
Migrate entities for node types doc and content type doc
Browse files Browse the repository at this point in the history
  • Loading branch information
informaid authored and Ines WALLON committed Mar 27, 2021
1 parent a815bf4 commit 4727942
Show file tree
Hide file tree
Showing 8 changed files with 472 additions and 1 deletion.
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'
31 changes: 31 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,31 @@
<?php

/**
* @file
*/

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

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

/**
*
*/
function deleteNodeFromNid($nid) {
$storage_controller = \Drupal::service('entity_type.manager')->getStorage('node');
$node = $storage_controller->load($nid);
$storage_controller->delete([$node]);
}

/**
*
*/
function finishBatch($nids): void {
\Drupal::service('messenger')->addMessage(sprintf('%s nodes deleted.', count($nids)));
}
169 changes: 169 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,169 @@
<?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;

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

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',
];

// $users = $this->getUsersUids();
/*if (count($users)>0) {
$form['deletable_users'] = [
'#type' => 'checkbox',
'#title' => $this->t('Delete Users'),
'#default_value' => false,
'#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');
$selectionUsers = $form_state->getValue('deletable_users');
$selections = [];
foreach ($selection as $sel) {
if (0 !== $sel) {
$selections[] = $sel;
}

}
$this->deleteNodesByTypes($selections);

}

/**
*
*/
private function getUsersUids(): array {
$uids = \Drupal::database()->select('users', 'u')
->fields('u', ['uid'])
->execute()
->fetchAllKeyed();
dump($uids);
return $uids;
}

/**
*
*/
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 = \Drupal::database()->select('node', 'n')
->fields('n', ['nid'])
->condition('type', $key)
->execute()
->fetchAll();
$types[$key] = sprintf('%s (%s nodes)', $key, count($res));
}
return $types;
}

/**
*
*/
private function deleteNodesByTypes($types) {
$res = \Drupal::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'][] = ['deleteNodesMultiple', [$extract]];
$batch['error_message'] = $this->t('Error deleting nodes.');
}
batch_set($batch);
}

}
Loading

0 comments on commit 4727942

Please sign in to comment.