3

I have tried in my custom module:

Declaring a path for Ajax callback

function custom_menu() {

  $items['ajax-block/%'] = array(
    'title' => 'Ajax test callback',
    'type' => MENU_CALLBACK,
    'page callback' => 'ajax_link_callback',
    'page arguments' => array(1),
    'delivery callback' => 'ajax_deliver',
    'access arguments' => array('access content'),
  );

  return $items;
}

now the callback itself

/**
 * Callback ajax
 */
function ajax_link_callback($from_cid, $mode = NULL) {

  if ($mode != 'ajax') {
    drupal_set_message('Turn Javascript');
    drupal_goto(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '<front>');
  }

  $commands[] = ajax_command_html('.region-content', some_func());

  return array('#type' => 'ajax', '#commands' => $commands);
}

I have a few manually created links:

<a class="use-ajax" href="/ajax-block/1/nojs">link1</a>
<a class="use-ajax" href="/ajax-block/2/nojs">link2</a>
<a class="use-ajax" href="/ajax-block/3/nojs">link3</a>

Now I need such functionality - when I press link1 - block1 loads content in to .region-content, when I click link2 - block2 and etc.

How can I do this?

0

Looks like your missing the ajax_deliver command

$commands[] = ajax_command_html('.region-content', some_func());
$page = array('#type' => 'ajax', '#commands' => $commands);
ajax_deliver($page);
  • How can i show different content on different links? – Alexander Kim Jan 8 '14 at 6:21
  • You will need to create functions to return HTML loading a block or whatever, it would replace with some_func() – chadpeppers Jan 8 '14 at 17:49
  • 2
    The call to ajax_deliver() is not necessary, since that is set as delivery callback; this means Drupal will call it automatically, before returning the page to the browser. – kiamlaluno May 18 '16 at 8:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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