User

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 new user


  $user = new stdClass();
  $user->name = 'Dribbit';
  $user->pass = 'test123';
  $user->mail = 'test@dribbit.eu';
  $user->status = 1;
  // Role id 3 is administrator.
  $roles = array(3 => true);
  $user->roles = $roles;
  user_save($user);

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(

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();

Add an extra field to a user display


/**
 * Implements hook_field_extra_fields().
 */
function MYMODULE_field_extra_fields() {
  $extra['user']['user'] = array (
    'display' => array(
      'extra_field' => array(
        'label' => t('User extra field'),
        'description' => t('Adds an extra field to a user view.'),
        'weight' => -99,
      )
    )
  );

  return $extra;
}

/**
 * Implements hook_user_view(). 
 */
function MYMODULE_user_view($account, $view_mode, $langcode) {
  $account->content['extra_user_transfer_link'] = array('#markup' => 'Data');
}
Subscribe to RSS - User