Module development

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

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().
 */

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.

Set weight on a module


**
 * Implements hook_install().
 */
function MY_MODULE_install() {
	
  // Set module weight in order to invoke module after other modules.
  db_update('system')
  ->fields(array('weight' => 100))
  ->condition('name', 'MY_MODULE')
  ->execute();
}

Pages

Subscribe to RSS - Module development