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. 

How to install Xdebug on Debian based OS like Ubuntu

sudo apt-get install php5-dev

sudo pecl install xdebug

Edit the php.ini file, located on /etc/php5/apache2/php.ini

Add following lines to the php.ini file:

[Xdebug]
zend_extension = "/usr/lib/php5/CHECK_OUT_DIRECTORY_NAME/xdebug.so"
; Use Get or Post parameter XDEBUG_PROFILE=1 to create a new cachegrind file.
xdebug.profiler_enable_trigger = 1
xdebug.profiler_enable = 0
xdebug.remote_enable = 1
xdebug.profiler_output_dir = "/tmp"
xdebug.profiler_output_name = "cachegrind.out.%t-%s"

 

How to install LAMP stack on Debian

  • sudo apt-get update
  • sudo apt-get install apache2
  • sudo apt-get install mysql-server
  • sudo apt-get install php5 php5-mysql

Get IP address by using ifconfig

ifconfig.PNG

From another device on the same network, browse to IP address. E.g. 192.168.0.114

If you see the "It works' page, it actually works.

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

Pages

Subscribe to Dribbit.eu RSS