How to Create 1000 Nodes Using Batch Process in Drupal

Follow this step-by-step guide to efficiently create 1000 nodes in Drupal using multiple batch processes.

Step 1: Create a Custom Module

Create a custom module if you don't already have one. You can name it something like custom_batch_node_creator.

Step 2: Define a URL for the Batch Process

In your custom module folder, create a PHP file, for example, custom_batch_node_creator.module, and implement hook_menu() to define a URL where you will trigger the batch process:


function custom_batch_node_creator_menu() {
  $items = array();

  $items['custom-batch-node-creator'] = array(
    'title' => 'Create 1000 Nodes Batch',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('custom_batch_node_creator_form'),
    'access arguments' => array('administer site configuration'), // Adjust permissions accordingly.
    'type' => MENU_NORMAL_ITEM,
  );

  return $items;
}
    

Step 3: Create a Batch Process Form

Create a batch process form in your module:


function custom_batch_node_creator_form($form, &$form_state) {
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Create 1000 Nodes',
  );

  return $form;
}

function custom_batch_node_creator_form_submit($form, &$form_state) {
  // Define the total number of nodes to create.
  $total_nodes = 1000;
  // Define the batch size.
  $batch_size = 100; // You can adjust this based on your server's capabilities.

  // Calculate the number of batches needed.
  $num_batches = ceil($total_nodes / $batch_size);

  // Initialize the batch.
  $batch = array(
    'operations' => array(),
    'title' => t('Creating Nodes'),
    'init_message' => t('Starting to create nodes...'),
    'progress_message' => t('Processed @current out of @total.'),
    'error_message' => t('An error occurred during node creation.'),
    'finished' => 'custom_batch_node_creator_finished',
  );

  // Create batches with operations.
  for ($batch_number = 0; $batch_number < $num_batches; $batch_number++) {
    $start = $batch_number * $batch_size + 1;
    $end = min(($batch_number + 1) * $batch_size, $total_nodes);
    $batch['operations'][] = array('custom_batch_node_creator_create_nodes', array($start, $end));
  }

  batch_set($batch);
  batch_process('custom-batch-node-creator');
}
    

Step 4: Create Nodes in Batches

Define the function to create nodes within the given range:


function custom_batch_node_creator_create_nodes($start, $end, &$context) {
  // Loop and create nodes within the given range ($start to $end).
  for ($node_number = $start; $node_number <= $end; $node_number++) {
    // Create a node here.
    // You can use the node_save() function to create a node programmatically.

    $node = new stdClass();
    $node->type = 'your_content_type'; // Replace with your content type.
    $node->title = 'Node ' . $node_number;
    // Add more fields as needed.
    
    node_save($node);

    // Update progress.
    $context['message'] = t('Created node @node_number', array('@node_number' => $node_number));
  }
}

function custom_batch_node_creator_finished($success, $results, $operations) {
  if ($success) {
    drupal_set_message(t('Nodes created successfully.'));
  }
  else {
    drupal_set_message(t('An error occurred while creating nodes.'), 'error');
  }
}
    

Step 5: Trigger the Batch Process

Visit the URL you defined in hook_menu, e.g., custom-batch-node-creator, and click the "Create 1000 Nodes" button. This will trigger the batch process, which will create nodes in batches of 100 until all 1000 nodes are created.

Make sure to replace 'your_content_type' with the actual content type machine name you want to use for the nodes, and adjust the access arguments in hook_menu to match your desired permission requirements. You can also adjust the batch size ($batch_size) based on your server's capabilities.

Published By: Krishanu Jadiya
Updated at: 2024-07-16 15:41:31

Card Image

How to Set Up a Local SSL Certificate on Apache: Step-by-Step Guide

Learn how to set up a local SSL certificate on Apache with this comprehensive step-by-step guide. Secure your local development environment with HTTPS.

Card Image

Latest Features of Coding Technology

Explore the latest features and advancements in coding technology, including new programming languages, frameworks, DevOps tools, AI integration, and more.

Card Image

Understanding Laravel Mix Webpack Configuration: Step-by-Step Guide

Step-by-step explanation of a Laravel Mix Webpack configuration file, including asset management for JavaScript, CSS, and Vue.js support.

Card Image

How Emojis Can Enhance Your Git Commits | Gitmoji Guide

Discover how to enhance your Git commits with emojis. Learn about the best practices for creating informative and visually distinctive commit messages.