Drupal 7

Alter user passwords


	require_once 'includes/password.inc';
	$uids = db_select('users', 'u')
	->fields('u', array('uid'))
	->execute()->fetchCol();
	
	foreach($uids as $uid) {
		$user = null;
		if ($uid != 0) {
			$user = user_load($uid);
			$user->pass = user_hash_password('test');
			user_save($user);
		}
	}

Export all user mail addresses to CSV



/**
 * Implements hook_menu().
 */
function export_mails_menu() {
  return array(
    'export_mails.csv' => array(
      'page callback' => '_export_mails',
      'access callback' => 'user_is_logged_in',
    ),
  );
}

/**
 * Implements hook_block_info().
 */
function export_mails_block_info() {

  $blocks['export_mails'] = array(
    'info' => t('Export mails'),
  );

  return $blocks;

}

/**
 * Implements hook_block_view().
 */
function export_mails_block_view($delta) {
  switch ($delta) {
    case 'export_mails' :
      return array(

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.

Delete all commerce products

The commerce_product_delete_multiple function can be used to bulk delete products. Similar to other ENTITY_delete_multiple functions, an array of entity ids is passed as an argument.
Drupal_set_time_limit is used to override the default 30 seconds that a script is allowed to run.

Delete all commerce orders

The commerce_order_delete_multiple function can be used to bulk delete orders. Similar to other ENTITY_delete_multiple functions, an array of entity ids is passed as an argument.
Drupal_set_time_limit is used to override the default 30 seconds that a script is allowed to run.

Check if taxonomy term exists and save the term if it doesn't

A dynamic query can be used to check if a taxonomy term exists.

Query conditions are the taxonomy term name and the vocabulary.

Create a form


/**
 * Implements hook_form().
 */
function MY_CUSTOM_FORM_form($node, &$form_state) {
  $form = array();
   
  $form['CUSTOM_FIELD'] = array(
    '#type' => 'textfield',
    '#label' => 'MY_LABEL',
  );
   
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit')
  )
   
  return $form;
}
 
/**
 * MY_CUSTOM_FORM validation handler.
 */
function MY_CUSTOM_FORM_form_validate($node, &$form_state) {
   
}
 
/**
 * MY_CUSTOM_FORM submit handler.
 */
function MY_CUSTOM_FORM_form_submit($node, &$form_state) {
   
}

Pages

Subscribe to RSS - Drupal 7