Create a users table


CREATE TABLE `DATABASENAME`.`users` (
  `id` INT NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(255) NOT NULL,
  `email` VARCHAR(255) NOT NULL,
  `password` VARCHAR(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE INDEX `email_UNIQUE` (`email` ASC));

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

Delete all commerce orders and truncate tables


TRUNCATE commerce_customer_profile;
TRUNCATE commerce_customer_profile_revision;
TRUNCATE commerce_line_item;
TRUNCATE commerce_order;
TRUNCATE commerce_order_revision;
TRUNCATE commerce_payment_transaction;
TRUNCATE commerce_payment_transaction_revision;

TRUNCATE field_data_commerce_customer_address;
TRUNCATE field_data_commerce_customer_billing;
TRUNCATE field_data_commerce_customer_shipping;
TRUNCATE field_data_commerce_line_items;
TRUNCATE field_data_commerce_order_total;
TRUNCATE field_data_commerce_product;
TRUNCATE field_data_commerce_shipping_service;

Create an unmanaged file


/**
 * Create a directory, add an unmanaged file
 * and create url from the new filepath.
 */
function _demo_file_unmanaged() {
  
  $filedir = 'public://demo';
 
  // Create a demo directory in the public dir.
  // file_prepare_directory checks, creates and sets permissions on a directory path.
  $prepared = file_prepare_directory($filedir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY); 
  dpm('Directory prepared => ' . $prepared);
  
  // Save an unmanaged file.
  // Unmanaged means nothing is saved into the database.

Create a managed file


/**
 * Create a directory, add a managed file,
 * and create url from the new file object.
 */
function _demo_file_managed() {
 
  $filedir = 'public://demo';
 
  // Create a demo directory in the public dir.
  // file_prepare_directory checks, creates and sets permissions on a directory path.
  $prepared = file_prepare_directory($filedir, FILE_MODIFY_PERMISSIONS | FILE_CREATE_DIRECTORY);
  dpm('Directory prepared => ' . $prepared);
 
  // Save a managed file.
  // Managed means a database entry is created in the file_managed table.

Create a textfield and an instance


  $field = array(
    'field_name' => 'demo_textfield',
    'type' => 'text',    
  );
  
  field_create_field($field);
  
  $instance = array(
    'field_name' => 'demo_textfield',
    'entity_type' => 'node',
    'bundle' => 'article',
    'label' => t('Demo texfield'),
    'description' => t('Just a demo textfield'),
    'widget' => array(
      'type' => 'text_textfield',
    ),
  );

  field_create_instance($instance);

Use cache_set and cache_get


/**
 * Demo cache.
 * 
 * Get all articles from cache table
 * or load them using node_load when cache isn't set.
 * 
 * Time example is for 50 articles.
 */
function _demo_cache() {
 
  if(!cache_get('demo_cached_articles')) {
    timer_start('Demo cache -- No cache');
  
    $nids = db_select('node', 'n')
    ->fields('n', array('nid'))
    ->execute()->fetchCol();
   
    $nodes = node_load_multiple($nids);
 
    // Average is 22 ms to load 50 articles.
    dpm(timer_stop('Demo cache -- No cache'));
    

Pages

Subscribe to Dribbit.eu RSS