Add an extra field to a content type display

Hook_node_view() can be used to add the block to the renderable $node->content array.
If the weight/order of the extra field needs to be set on the manage display tabs from your article content type, use hook_field_extra_fields() to tell Drupal about the new field.

Performance - using the bootstrap timer functions

Use timer_start(), timer_read() and timer_stop() to measure code performance

These practical and easy to use performance measuring functions can be found in the bootstrap.inc file, provided by Drupal core. The same timer can be started and stopped multiple times, the measured intervals will be accumulated.

Drupal entities

When installing a fresh Drupal 7 site, there are five different entities available. You can see which entities are available by using entity_get_info().

Create a block

Basically all we need is hook_block_info() and hook_block_view() to create a block. 
By using hook_block_info() we declare which blocks are avaialable in our module. As seen on admin/structure/block. 
Hook_block_view() handles the rendering part of the block.

Handle multiple wrappers on an ajax callback

In this case the complete form is rebuild and the original messages array is replaced by the messages array related to the new form state. 

Create a lightweight commerce cart block

We build a custom block and use hook_theme to render the cart data. Usually a view is made to create a custom shopping cart block. However, a custom block to render number of items and the total price of an order is more exciting to make and more performant than a view.

Keep form values after form submit

Sometimes form state values need to be kept to keep filter values on a form submit. In order to do this, the $form_state['rebuild'] boolean has to be set to true. This is done in the submit handler of the form.

Create a block with a form


/**
 * Implements hook_block_info().
 */
function MY_CUSTOM_MODULE_block_info() {
   
  $blocks['MY_CUSTOM_BLOCK'] = array(
    'info' => t('My custom block title as shown on admin/structure/block'),
  );

  return $blocks;

}

/**
 * Implements hook_block_view().
 */
function MY_CUSTOM_MODULE_block_view($delta) {

  switch ($delta) {
    case 'MY_CUSTOM_BLOCK' :
      return array(
      'subject' => t('MY_CUSTOM_BLOCK title.'),
      'content' => drupal_get_form('MY_CUSTOM_FORM'),
      );
      break;
  }
   
}

/**
 * Implements hook_form().
 */

Pages

Subscribe to Dribbit.eu RSS