2

I have menu on left column, When we click menu-link I need to post data in right column without page refresh. I am looking to ajaxify menu.

Could you please give me an example how to ajaxify menu links?

1 Answer 1

0

This is really simple by using Drupal ajax framework.

First, define your menu callback page.

/**
 * Implements hook_menu().
 */
function yourmodule_menu() {
  $items = array();

  $items['yourmodule/callback-page'] = array(
    'title' => 'AJAX callback - refresh the elements on the right hand',
    'page callback' => 'yourmodule_refresh',
    'page arguments' => array(1),
    'access arguments' => array('access content'),
    'type' => MENU_CALLBACK,
  );

  return $items;
}

Secondly, do ajax deliver on callback page.

/**
 * Menu callback.
 */
function yourmodule_refresh($action, $type) {
  if ($type != 'ajax') {
    // This is js only.
    return 'Oh well';
  }

  $content = t('What ever you want');

  $commands = array();
  $commands[] = ajax_command_replace('.right-hand-wrapper-class', $content);
  $page = array('#type' => 'ajax', '#commands' => $commands);
  ajax_deliver($page);
}
6
  • Does this work? How can the page be in the menu if the type is MENU_CALLBACK. Also, there are two arguments in the page callback but only 1 in the menu item arguments array so the example above will fail. Commented Jul 25, 2012 at 21:25
  • This is my fault on the explain the way we get the second parameter to use ajax. But the above code work well, Drupal hook menu enough smart to understand the second parameter :). You only need put second on case you use. For example, call "yourmodule/callback-page/ajax" link with class 'use-ajax'.
    – Tran Huy
    Commented Jul 26, 2012 at 8:51
  • My understanding is this would work using a link with 'use-ajax' class. But how would this work inside a menu? Adding 'use-ajax' does not work for menu items? Commented Jul 26, 2012 at 9:38
  • 1
    Can you update this answer on how to create the link? Adding "use-ajax" does not work.
    – giorgio79
    Commented Oct 10, 2012 at 11:54
  • I got this working by binding the ajax event to the menu items manually. Google "new Drupal.ajax(base, this, element_settings)" Commented Oct 31, 2012 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.