home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (devel_generate_batch.inc)

This example Drupal source code file (devel_generate_batch.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, batch, context, devel_generate_batch_finished, file, function, if, message, num, operations, php, success, title, vars

The devel_generate_batch.inc Drupal example source code

<?php
// $Id: devel_generate_batch.inc,v 1.1.2.5 2009/02/02 21:00:52 drewish Exp $

/**
 * Devel Generate batch handling functions using the BatchAPI
 * included by devel_generate.module
 */

/**
 * Functions called from FAPI:
 */

function devel_batch_generate_content($form_state) {
  $operations = array();

  // Setup the batch operations and save the variables.
  $operations[] = array('devel_generate_batch_content_pre_node', array($form_state['values']));

  // add the kill operation
  if ($form_state['values']['kill_content']) {
    $operations[] = array('devel_generate_batch_content_kill', array());
  }

  // add the operations to create the nodes
  for ($num = 0; $num < $form_state['values']['num_nodes']; $num ++) {
    $operations[] = array('devel_generate_batch_content_add_node', array());
  }

  // start the batch
  $batch = array(
    'title' => t('Generating Content'),
    'operations' => $operations,
    'finished' => 'devel_generate_batch_finished',
    'file' => drupal_get_path('module', 'devel_generate') . '/devel_generate_batch.inc',
  );
  batch_set($batch);
}

/**
 * Create Content Batch Functions:
 */

function devel_generate_batch_content_kill(&$context) {
  require_once('devel_generate.inc');
  devel_generate_content_kill($context['results']);
}

function devel_generate_batch_content_pre_node($vars, &$context) {
  $context['results'] = $vars;
  $context['results']['num_nids'] = 0;
  require_once('devel_generate.inc');
  devel_generate_content_pre_node($context['results']);
}

function devel_generate_batch_content_add_node(&$context) {
  require_once('devel_generate.inc');
  devel_generate_content_add_node($context['results']);
  $context['results']['num_nids'] ++;
}

function devel_generate_batch_finished($success, $results, $operations) {
  if ($success) {
    $message = t('Finished @num_nids nodes created successfully.', array('@num_nids' => $results['num_nids']));
  }
  else {
    $message = t('Finished with an error.');
  }
  drupal_set_message($message);
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal devel_generate_batch.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.