Advanced

Reset users


/**
 * Delete all users except uid 0 and 1.
 * Reset auto increment on users table.
 */
function _MY_MODULE_delete_users() {
  drupal_set_time_limit(0);
	
  // Delete.
  $uids = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->condition('uid', array(0, 1), 'NOT IN')
  ->execute()->fetchCol();

  user_delete_multiple($uids);

  // Reset auto increment.
  db_query("ALTER TABLE users AUTO_INCREMENT = 1"); 
  // Users table doesn't use AI, reset uid sequence.
  db_query("UPDATE sequences SET value = 1");

Create a batch Drupal 7


/**
 * Implements hook_form().
 */
function _demo_batch_form($node, &$form_state) {
  $form = array();
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'submit',
  );
  
  return $form;
}

/**
 * hook_form() submit handler.
 */
function _demo_batch_form_submit($node, &$form_state) {
  $batch = _demo_batch();
 
  batch_set($batch);
}

/**
 * Demo batch.
 * 
 * Alter the node title from node ID {$nid} {$count} times.
 */
function _demo_batch() {

  $nid = 150;
  $count = 500;
  $operations = array();
  

Create a custom commerce checkout pane


/**
 * @module: 
 * Create a custom checkout pane for Drupal Commerce.
 */

/**
 * Implements hook_commerce_checkout_pane_info()
 */
function MY_MODULE_pane_commerce_checkout_pane_info() {
  $panes = array();
  // Custom checkout pane.
  $panes['CUSTOM_MODULE_PANE_NAME'] = array(
    'title' =--> t('Shipping service'),
    'page' => 'shipping',
    'weight' => 11,
    'base' => 'CUSTOM_MODULE_PANE_NAME_pane',
    'fieldset' => TRUE,
  );
  return $panes;
}

/**
 * Implements base_settings_form().
 */

How to render an item list

To use the theme_item_list function, a $variables array needs to be declared which contains an items array, a title which can be empty, a type element (ul or ol) and an attributes array. When a title is passed, a h3 element is added to the HTML markup.

The fastest way to get nids in a default no cache situation

In this test case scenario a function was made to show the differences when querying for node ids. Mostly this kind of query is the first step to render nodes in a custom made view. Each query returns the same array of node ids. Execution times are tested by using the Drupal 7 bootstrap timer functions.

Export all users as a JSON object


/**
 * Implements hook_menu().
 */
function MYMODULE_export_menu() {
  $items = array();
  
  $items['export-users'] = array(
    'title' => 'Export users',
    'description' => 'Export users',
    'page callback' => '_export_users',
    'page arguments' => array(1),
    'access callback' => true,
    'type' => MENU_CALLBACK,
  );

  return $items;
}

/**
 * Export users.
 *
 */
function _export_users() {

  drupal_set_time_limit(0);

  $uids = db_select('users', 'u')
  ->fields('u', array('uid'))
  ->execute()
  ->fetchCol();

Handle multiple wrappers on an ajax callback

In this case the complete form is rebuild and the original messages array is replaced by the messages array related to the new form state. 

Subscribe to RSS - Advanced