-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate entities for node types doc and content type doc
- Loading branch information
Showing
8 changed files
with
459 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
...custom/drupalfr_migrate_d7/config/install/migrate_plus.migration.drupalfr_node_doc_d7.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
27 changes: 27 additions & 0 deletions
27
...ustom/drupalfr_migrate_d7/config/install/migrate_plus.migration.drupalfr_nodetypes_d7.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
9 changes: 9 additions & 0 deletions
9
...es/custom/drupalfr_migrate_d7/config/install/migrate_plus.migration_group.drupalfr_d7.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
www/modules/custom/drupalfr_migrate_d7/drupalfr_migrate_d7.routing.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
32 changes: 32 additions & 0 deletions
32
www/modules/custom/drupalfr_migrate_d7/includes/nodes.batch.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Include to delete all nodes of a type. | ||
*/ | ||
|
||
/** | ||
* Delete nodes multiple. | ||
*/ | ||
function deleteNodesMultiple($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 deleteNodeFromNid($nid) { | ||
$storage_controller = \Drupal::service('entity_type.manager')->getStorage('node'); | ||
$node = $storage_controller->load($nid); | ||
$storage_controller->delete([$node]); | ||
} | ||
|
||
/** | ||
* Message at end of batch. | ||
*/ | ||
function finishBatch($nids): void { | ||
\Drupal::service('messenger')->addMessage(sprintf('%s nodes deleted.', count($nids))); | ||
} |
151 changes: 151 additions & 0 deletions
151
www/modules/custom/drupalfr_migrate_d7/src/Form/DeleteEntitiesForm.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
Oops, something went wrong.