Drupal 7

Load all nodes by type


  $nids = db_select('node', 'n')
  ->fields('n', array('nid'))
  ->condition('n.type', 'NODE_TYPE')
  ->execute()->fetchCol();
  
  $nodes = node_load_multiple($nids);


 

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

How to theme a table Drupal 7


  
  $items = array(
    array(
      'sender' => 'Lorem',
      'receiver' => 'Dolorem',
      'message' => 'This is a message',
      'amount' => 5,
    ),
    array(
      'sender' => 'Dolorem',
      'receiver' => 'Lorem',
      'message' => 'This is the reply',
      'amount' => 7,
    ),
  );
  
  $header = array(t('From'), t('To'), t('Message'), t('Amount'));
  $rows = array();
  foreach($items as $item) {
    $rows[] = array(
      $item['sender'],
      $item['receiver'],
      $item['message'],
      $item['amount'],
    );
  }

Create and update database fields by using hook_update_N

Db_add_field() can be used in hook_update_N() to add fields to a database table. Always remember to add the field to hook_schema() to allow entities to properly read the table. Also, this is needed to properly install the module in other projects or to reinstall the module in the current project, mostly in the development fase. 

Bulk create menu lnks


$items = array(
    array(
      'link_path' => '<front>',
      'link_title' => 'TITLE',
      'menu_name' => 'footer-menu',
    ),
    array(
      'link_path' => '<front>',
      'link_title' => 'TITLE',
      'menu_name' => 'footer-menu',
    ),
    array(
      'link_path' => '<front>',
      'link_title' => 'TITLE',
      'menu_name' => 'footer-menu',
    ),
    array(
      'link_path' => '<front>',
      'link_title' => 'TITLE',
      'menu_name' => 'footer-menu',
  );
 

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.

Pages

Subscribe to RSS - Drupal 7