Create a new user

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

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 a textfield and an instance

  $field = array(
    'field_name' => 'demo_textfield',
    'type' => 'text',    
  $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',


Use of external style sheets in Drupal 7

External style sheets can't be added in the themes .info file

In Drupal 7, drupal_add_css is used to add external style sheets. In this example, the minified Bootstrap's CSS is fetched.

 * Implements template_preprocess_html().
function subtheme_preprocess_html(&$variables) {
    array('type' => 'external'));

Also, externally hosted style sheets are never aggregated or compressed.

Create a subtheme Drupal 7

This example show the basics to create a subtheme in Drupal 7.

First of all, create a new directory in the sites theme folder to add the custom theme.

Create the .info file, aside the mandatory name and core properties, and the recommended desription property, add the base theme property.

The custom theme can be activated on the /admin/appearance page.

Create extra fields on nodes

Use hook_field_extra_fields to use the new fields with the manage display function. The content of the new field is built in hook_node_view.

Create table by using hook_schema

 * Implements hook_schema().
function MYMODULE_schema() {
  $schema['MYTABLE'] = array(
    'description' => 'DESCRIPTION',
    'fields' => array(
      'id' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
      'language' => array(
        'type' => 'varchar',
        'length' => 4,
        'not null' => TRUE,

Find files with a pattern in a directory


$filenames = scandir(DIR_PATH);

$pattern = '/^PATTERN/';
$matches = array();
$matches[] = preg_grep($pattern, $filenames);



Subscribe to RSS - Intermediate