I want to call a block on a page by AJAX. This is appearing in content and not in a block region.

[Use case: I have a View listing countries and their details; I want to add, at the top of the View, the user's country. I want the page to be cached, hence using AJAX to call back a View Block]

In the header of my view I have:

<div id="load-ajax"></div>
(function ($) {
}) (jQuery);

In hook_menu I have

$items['example/test'] = array(
    'title' => 'AJAX Delivery Test', 
    'page callback' => 'show_example_block', 
    'delivery callback' => '_basic_print', 
    'access callback' => TRUE, 
    'type' => MENU_CALLBACK,

I then have two functions:

function _basic_print($page_callback_result){
  print $page_callback_result;

function show_example_block(){
  if (isset($_COOKIE['country'])){
    return views_embed_view('example', 'example_block', $_COOKIE['country']);

This works fine, but I'm wondering whether I'm missing anything. Is there a simpler, slightly more native way of doing this?

  • 2
    This is a pretty nice way of doing it if you ask me. For those wondering, _basic_print is essentially a way of returning an AJAX request via hook_menu without all the standard Drupal templating of a page as specified in drupal_deliver_html_page. – cdmo Sep 24 '15 at 18:10
$block = module_invoke('module_name', 'block', 'view', 0);
print $block['content'];

Try including the view block.

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.