logo

Update node field value of a content type using batch process in Drupal 9

31
July

Update node field value of a content type using batch process in Drupal 9
By: Anonymous | Published On: Mon, 07/31/2023 - 17:02

To update the text field value of a content type in Drupal 9 using a batch process, you can follow these steps:

Create a custom module: If you don't have one already, create a custom module in the 'modules/custom' directory of your Drupal installation.

Define a form to receive the user input: Create a form in your custom module that takes the new value for the text field as input from the user. This form will be used to trigger the batch process.

Implement the batch process: Create a batch process in your custom module that iterates through all the nodes of the content type and updates the text field with the new value provided by the user.

Here's the code for each step:

Step 1: Create a custom module (replace 'your_module_name' with the actual name of your module):

Create the module folder and the 'your_module_name.info.yml' file:

# modules/custom/your_module_name/your_module_name.info.yml
name: 'Your Module Name'
type: module
description: 'Updates bulk nodes text field value using batch process'
package: 'Custom'
core_version_requirement: ^8 || ^9
dependencies:
  - node

Declaring a route:

The routing information is saved in batch_und_langcode /batch_und_langcode.routing.yml.

your_module_name.your_batch_update:
  path: '/batch-langcode/replace'
  defaults:
	_form: '\Drupal\your_module_name\Form\YourModuleBatchUpdateForm'
	_title: 'Batch Language Code'
  requirements:
	_permission: 'access content'

Create a form:

Create a new subfolder "modules/custom/your_module_name/src/Form." Create a file called "YourModuleBatchUpdateForm.php" in this folder with the following content and in this, just a submit button is required on the form to process the request.

/**
* Form constructor for the batch update form.
*/
function your_module_name_batch_form($form, &$form_state) {
$form['new_text_value'] = array(
'#type' => 'textfield',
'#title' => 'New Text Value',
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Update Nodes',
);
return $form;
}

/**
* Submit function for the batch update form.
*/
function your_module_name_batch_form_submit($form, &$form_state) {
$new_text_value = $form_state['values']['new_text_value'];

// Queue the batch process.
batch_set(your_module_name_batch_process($new_text_value));
}

Create the 'your_module_name.module' file and put the code changes as required

/**
* Batch process callback to update nodes.
*/
function your_module_name_batch_process($new_text_value) {
$batch = array(
'title' => t('Updating Nodes...'),
'operations' => array(),
'init_message' => t('Starting update...'),
'progress_message' => t('Processed @current out of @total.'),
'error_message' => t('An error occurred during processing'),
'finished' => 'your_module_name_batch_finished',
);

// Query all nodes of the content type you want to update.
$query = \Drupal::entityQuery('node')
->condition('type', 'your_content_type'); // Replace 'your_content_type' with the actual content type machine name.
$nids = $query->execute();

foreach ($nids as $nid) {
$batch['operations'][] = array('your_module_name_batch_update_node', array($nid, $new_text_value));
}

return $batch;
}

/**
* Batch operation to update the node.
*/
function your_module_name_batch_update_node($nid, $new_text_value, &$context) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($nid);
if ($node) {
// Update the text field with the new value.
$node->set('field_your_text_field', $new_text_value); // Replace 'field_your_text_field' with the actual text field machine name.
$node->save();
}

$context['message'] = t('Updated node @nid', array('@nid' => $nid));
}

/**
* Batch finished callback.
*/
function your_module_name_batch_finished($success, $results, $operations) {
if ($success) {
drupal_set_message(t('Update completed successfully.'));
} else {
drupal_set_message(t('Update encountered an error.'), 'error');
}
}

Step 2: Enable the module:

Go to the Extend page (/admin/modules) and enable your custom module.

Step 3: Trigger the batch process:

Visit the form page for the batch update process (/admin/config/content/update-nodes), enter the new text value in the field, and click the "Update Nodes" button. The batch process will then update all the nodes of the specified content type with the new text value.

Please ensure to replace 'your_content_type' and 'field_your_text_field' with the actual machine names of your content type and text field, respectively.

Need Help ?