Drupal – Check if a User has a specific role

Here is a quick way to determine if a user has a specific role

<?php
  // Bring the user object into scope.
  global $user;

  // Check to see if $user has the administrator user role.
  if (in_array('administrator', array_values($user->roles))) {
    // Do something.
  }
?>

Drupal – Adding Javascript to your module

When creating your own Drupal module, you may need to add some styling or Javascript to improve the usability of your module. Here is how to do it.

drupal_add_js(drupal_get_path('module', 'MODULE_NAME') . '/common.js');

Similarly you can add CSS to your module as well

drupal_add_css(drupal_get_path('module', 'MODULE_NAME') . '/styles.css');

Drupal – Use hook_form_alter() to set redirect path on the form

One popular use of this hook is to change the destination of a form submission. Here is how it is accomplished:

<?php
function YOURMODULE_form_alter($form_id, &$form) {
  switch ($form['#id']) {
    case 'node-form':
       if ($form['type']['#value'] == 'story') {
         $form['#redirect'] = 'new/url';
       }
     break;
  }
}
?>

Drupal – The Best and my Favorite Modules

I’ve worked with Drupal in the past for several projects and have come across some modules that have become my favorites. Here they are:

  • Tiny MCE

    This module was the first to integrate Moxiecode’s popular TinyMCE WYSIWYG editor into a Drupal site for editing advance site content.

  • Simple News

    Simplenews publishes and sends newsletters to lists of subscribers. Both anonymous and authenticated users can opt-in to different mailing lists. HTML email can be send by adding Mime Mail module.

  • Views

    The Views module provides a flexible method for Drupal site designers to control how lists and tables of content (nodes in Views 1, almost anything in Views 2) are presented. Traditionally, Drupal has hard-coded most of this, particularly in how taxonomy and tracker lists are formatted.

  • Panels

    The Panels module allows a site administrator to create customized layouts for multiple uses. At its core it is a drag and drop content manager that lets you visually design a layout and place content within that layout.

  • Content Construction Kit (CCK)

    The Content Construction Kit allows you to add custom fields to nodes using a web browser.

  • Path Auto

    The Pathauto module automatically generates path aliases for various kinds of content (nodes, categories, users) without requiring the user to manually specify the path alias. This allows you to get aliases like /category/my-node-title.html instead of /node/123.

  • Five Star

    The Five Star voting module adds a clean, attractive voting widget to nodes in Drupal

  • Poor Mans Cron

    A module which runs the Drupal cron operations without needing the cron application.

  • Node Words

    This module allows you to set some meta tags for each node, view or panels page.

  • Global Redirect

    Checks the current URL for an alias and does a 301 redirect to it if it is not being used.

  • Page Title

    This module gives you granular control over the page title. You can specify patterns for how the title should be structured and, on content creation pages, specify the page title separately to the content’s title.

  • XML Sitemap

    The XML sitemap module creates a sitemap that conforms to the sitemaps.org specification. The sitemap created by the module can be automatically submitted to Ask, Google, Bing (formerly Windows Live Search), and Yahoo! search engines.

  • Print

    This module allows you to generate page, email and PDFprinter-friendly versions of any node.

  • Username Check

    This very simple module allows visitors to check username originality quickly using AJAX request during registration (completing registration form).

  • Node Hierarchy

    Node Hierarchy allows nodes to be children of other nodes creating a tree-like hierarchy of content.