Friday, August 26, 2011

Dries Keynote at DrupalCon London

Must listen to Dries Keynote at DrupalCon London. It is always great to hear him lead the Drupal Community!
He talks about Drupal and how it’s acceptance and usage has been increased. Drupal is now adopted more and more by Media, Government, Technology companies as a standard solution. He also highlights the way Drupal helps companies to innovate and move their interactions with their customers to a different level. Presents the summary of his State of Drupal 2011 survey, Drupal 8 and the way forward.

He highlights how opportunity should be the one that defines a products road map! The need for more marketing initiatives. Just jump in and watch Dries at London... I am missing being a part of it all!

Sunday, August 14, 2011

Auto Complete form field with multiple Values

To create an auto-complete field in Drupal, all you need to do to is to use the From API attribute #autocomplete_path and associate with a custom menu callback that provides the AJAX response for the auto-complete field. The custom menu callback can define the query that needs to be made in the database before a result is returned. The below code can b used to create a multi-select auto-complete field.

The best examples of the auto-complete field in core are the author field in node create and edit and taxonomy auto-complete.

function my_module_menu() {

$items['my-module/my-autocomplete'] = array(
    'title' =>'My module autocomplete',
    'page callback' => 'my_module_autocomplete',
    'access callback' => 'my_access',
    'access arguments' => array('my module access'),
    'type' => MENU_CALLBACK,
 return $items;

function my_module_autocomplete($string = '') {
  $array = drupal_explode_tags($string);

  // Fetch last value
  $last_string = trim(array_pop($array));
  $matches = array();
  if ($last_string){
    $result =  Define Query here... 
    while($obj = db_fetch_object($result)) {
      $prefix = count($array) ? implode(', ', $array) .', ' : '';
      $matches[$prefix.$obj->;name] = check_plain($obj->name);

1. Helper function taxonomy_autocomplete in taxonomy module
2. Function user_autocomplete in user module.

Working with Drupal’s hook_menu

Drupal’s hook_menu is one of the most important drupal hook. It defines paths in the drupal system and what they do. You can not write a custom module without understanding hook_menu

The functionality could be listing of content on the site or an administrative page or it could just be a special page that needs to be generated and maintained as a custom module, all these can be met by writing a menu callback. hook_menu
is normally used in a custom module when using drupal’s page, views generated pages do not meet our custom requirements. 

Often we may not want to create new menu callbacks but may use functionalities provided by other custom modules menu callbacks. There may be a need to customize the menucall backs of core or custom modules to meet the requirements.

The below note gives an overview of the different usecases of hook_menu and also a peek in to how to alter a core/contributed menu callbacks in our own custom module.

The different usecases of a hook_menu 

1) Custom page or functionality on the site. e.g: Blog module’s callback blog_page that is Listing of all blogs on the site.

$items['blog'] = array(
'title' => 'blogs',
'description' => 'Listing of blogs.',
'page callback' => 'blog_page',
'access arguments' => array('access content'),

User modules’s callback admin/user/user. This menucallback lists users, provides for links to edit and add new users

$items['admin/user/user'] = array(
   'title' => 'Users',
   'description' => 'List, add, and edit users.',
   'page callback' => 'user_admin',
   'page arguments' => array('list'),
   'access arguments' => array('administer users'),
   'file' => '',

2) Generate Forms: All forms that are built in Drupal will have an associated menu callback. Forms that need beyond the node create and edit forms that are available with Drupal need to be built using page callback drupal_get_form. These could range from admin forms to user forms. Refer Form API for more information on building forms.

e.g: Node modules: create and edit content forms.
Contact module: Site wide contact form, user contact forms and Contact admin settings forms.

$items['admin/build/contact'] = array(

   'title' => 'Contact form',
   'description' => 'Create a system contact form and set up categories for the form to use.',
   'page callback' => 'contact_admin_categories',
   'access arguments' => array('administer site-wide contact form'),
   'file' => '',

3) AJAX callbacks: Any  AJAX  functionality on the site will have an associated callback that defines the action that happens or the value that needs to be retrieved on  AJAX .

e.g: user/autocomplete  AJAX callback that is called in used in the author field of the create and edit node form.

$items['user/autocomplete'] = array(
   'title' => 'User autocomplete',
   'page callback' => 'user_autocomplete',
   'access callback' => 'user_access',
   'access arguments' => array('access user profiles'),
   'type' => MENU_CALLBACK,
   'file' => '',

4) Create RSS feeds

The hook_menu not only allows us to define other content formats that can be generated in response to a http request.

e.g: Node/blog modules provides for rss.xml callback that provides rss feeds.

$items['rss.xml'] = array(
   'title' => 'RSS feed',
   'page callback' => 'node_feed',
   'access arguments' => array('access content'),
   'type' => MENU_CALLBACK,

For  a full list of parameters that can be associated with a menucallback, visit hook_menu drupal 6 api page. Most importantly, in all the above the menu callback created can be associated with specific permissions. The access arguments help in defining the access rules and also in providing security to the menu callbacks.

Proper use of these parameters is important in implementing the functionality and also in organising the associated code.

Customizing core/contributed module’s menucallbacks
Some usecases include:

1. Rename the path of a core or contributed module. This can be achieved using: path_set_alias. 

path_set_alias('og/xxx/'. $node->nid, $alias_path);
This is particularly useful for modules that are not supported by pathauto. Path auto module provides for an admin interface to path alias various content in Drupal.

2. Reset some of the menucallback's properties by using Hook Menu Alter.

Removing user profile tabs using menu_alter
function my_module_menu_alter(&$items) {
$items['user/...']['type'] = MENU_CALLBACK;
} In the above example we are changing the type of Menu callback user/... from MENU_LOCAL_TASK to a regular MENU_CALLBACK. This callback will no longer be displayed in a tab.

menu_alter could be extended to change the access callback, redirect to another custom callback, etc.
function my_module_menu_alter(&$items) {
  $items['cart']['access arguments'] = array('access my module'); $items['cart']['page callback'] = 'drupal_goto';   $items['cart']['page arguments'] = array('my_module_menu');
} In the above example we reset the access arguments of the ‘cart’ callback in ubercart module to my modules permission 'access my module', redirect the cart callback to my modules callback my_module_menu.

Search This Blog

Chennai Drupal Community - Community plumbing

Shyamala's Drupal SEO