I have a requirement that I need to convert specific nodes to another content type. I made modification to the alpha version to include this functionality. Could something like this be included in the next release?
diff --git a/docroot/modules/contrib/convert_nodes/src/Form/ConvertNodesForm.php b/docroot/modules/contrib/convert_nodes/src/Form/ConvertNodesForm.php
index 82ced06..0bf3f9f 100644
--- a/docroot/modules/contrib/convert_nodes/src/Form/ConvertNodesForm.php
+++ b/docroot/modules/contrib/convert_nodes/src/Form/ConvertNodesForm.php
@@ -11,6 +11,7 @@
use Drupal\Core\Entity\Query\QueryFactory;
use Drupal\Core\Entity\EntityFieldManager;
use Drupal\Core\Routing\RouteBuilderInterface;
+use Drupal\node\Entity\Node;
/**
* ConvertNodesForm.
@@ -87,6 +88,8 @@ class ConvertNodesForm extends FormBase implements FormInterface {
*/
protected $routeBuilder;
+ protected $input_nids = NULL;
+
/**
* {@inheritdoc}
*/
@@ -124,6 +127,9 @@ public function convertNodes() {
$update_fields = $userInput['update_fields'];
$field_table_names = ConvertNodes::getFieldTableNames($this->fieldsFrom);
$nids = ConvertNodes::getNids($this->fromType);
+ if(!empty($this->input_nids)){
+ $nids = $this->input_nids;
+ }
$map_fields = ConvertNodes::getOldFieldValues($nids, $map_fields, $this->fieldsTo);
$batch = [
'title' => $this->t('Converting Base Tables...'),
@@ -311,11 +317,15 @@ public function buildForm(array $form, FormStateInterface $form_state) {
break;
case 5:
- drupal_set_message($this->t('Are you sure you want to convert all nodes of type <em>@from_type</em> to type <em>@to_type</em>?',
+ drupal_set_message($this->t('Are you sure you want to convert nodes of type <em>@from_type</em> to type <em>@to_type</em>?',
[
'@from_type' => $this->fromType,
'@to_type' => $this->toType,
]), 'warning');
+ $form['nids'] = [
+ '#type' => 'textarea',
+ '#title' => $this->t('Enter the comma separated node ids you want to convert or leave this field empty to convert all of the specified type.'),
+ ];
$form['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Convert'),
@@ -343,6 +353,24 @@ public function validateForm(array &$form, FormStateInterface $form_state) {
$form_state->setErrorByName('convert_nodes_content_type_to', $this->t('Please select different content types.'));
}
break;
+ case 5:
+ $nid_value = str_replace(' ', '', $form['nids']['#value']);
+ if(!empty($nid_value)){
+ $nids = explode(",",$nid_value);
+ $invalid_nids = array();
+ foreach($nids as $nid) {
+ $node = Node::load($nid);
+ if($node->bundle() != $this->from_type) {
+ array_push($invalid_nids, $nid);
+ }
+ }
+ if(count($invalid_nids) > 0) {
+ $form_state->setErrorByName('nids', $this->t('Not all passed node ids are of type @type: @nids', array('@type' => $this->from_type, '@nids' => implode(',', $invalid_nids))));
+ } else {
+ $this->input_nids = $nids;
+ }
+
+ }
default:
// TODO - validate other steps.
Comments
Comment #2
lomasr commentedI have applied and created a patch as suggested. Textarea is created in step 5 but didnt respected the values given in the textarea.
Comment #3
lomasr commentedComment #4
dillix commentedComment #5
el1_1el commentedSo this seems to work just fine... but im wondering if we shouldnt convert everything over to ActionBase (ala https://www.drupal.org/project/bulk_update_fields and https://www.drupal.org/project/bulk_copy_fields) to utilize the core bulk operations instead of a list of nids. Any thoughts?
Comment #6
dillix commentedI think it should be an action in /admin/content like this: http://joxi.ru/E2pVyzEu9l5Qwr
Comment #7
el1_1el commentedI agree, but we need to decide what to do when nodes of multiple content types are selected...which is why I didn't do it that way to begin with. Any ideas welcome.
Comment #8
dat deaf drupaler commented+1 for this feature... need to use to work with rules/workflow.
Create a separate module for bulk actions?
Comment #9
dillix commentedFor single nodes we can create separate tab like View, Edit
Comment #10
el1_1el commentedSee also https://www.drupal.org/project/convert_nodes/issues/2955448
I'll try to start work on this next week. Assistance/patches welcome!
Comment #11
el1_1el commentedSo here's a start to this - https://github.com/mlibrary/convert_bundles
You can select multiple nodes on /admin/content and use the "Convert Entity Bundles" action on nodes of multiple bundles (content types). Could use suggestions and testing
Comment #12
dat deaf drupaler commentedI tested convert_bundles from /admin/content to another node which produced without any errors, however if I ran from /admin/config/content/convert_bundles page, which seems to convert *all* bundles of selected entity from initial page and got these errors-
Notice: Undefined index: entity_subqueue in Drupal\convert_bundles\Form\ConvertBundlesForm->buildForm() (line 363 ...)
Notice: Undefined index: paragraph in Drupal\convert_bundles\Form\ConvertBundlesForm->buildForm() (line 363 ...)
Using with just basic fields (texts and references) but not with paragraph fields, not sure why it would bootstrap through these? Seems to work well.. will need to find time to test with rules/workflow.
Edit: I like how the field mapping would 'auto-match' the prospective fields of new bundles if given fields' machine name are identical :)
Comment #13
el1_1el commentedSo I fixed those errors. In order to be able to convert all entities, this gets all entity types (which in your case included 'entity_subqueue' and 'paragraph'), and then gets all bundles. When it looked for bundles in your instance for paragraphs and entity_subqueues, it must not have any set. so its now checking for them.
I expect more issues with paragraphs and other non-node and possibly tax term entities that will have to be worked through. Also, actions will need to be created somehow for all entity types on a site as mentioned at https://www.drupal.org/project/convert_nodes/issues/2955448
Let me know when and if you think it would be useful to move this to its own alpha project to work on issues there. I will likely discourage further use of convert_nodes in favor of convert_bundles at some point and mark convert_nodes feature complete or obsolete
Comment #14
prashant.c@lomasr
your patch applies successfully except few minor errors
Also Shouldn't the Class
ConvertNodes.phpbe implemented as a Service ?Comment #15
el1_1el commented@Prashant.c - So this issue is turning into https://github.com/mlibrary/convert_bundles since we can create an action and use node operations bulk form for individual nodes as well as use tabs on the node and make the whole conversion more generic for other entity types.
"Also Shouldn't the Class ConvertNodes.php be implemented as a Service ?" - Sure! Patches welcome! Though I'm likely to obsolete this module or consider feature complete in favor of the more entity agnostic convert_bundles
I could also keep the convert_nodes name and make convert_bundles a 2x version if people prefer
Comment #16
el1_1el commentedtabs have been added for each entity - https://github.com/mlibrary/convert_bundles
Comment #17
el1_1el commentedThis feature has been added to convert_bundles.