How to load an OpenLayers view into the content area with an AJAX link? I have a link in the side bar that links to a OL view.

I would like to Ajaxify it so the view is loaded into the content area without a page re-load.

I am using the Ajax link example in the Example module and saving the $output at ajax_link_response() with this code:

&lt;?php
...
function ajax_link_response($type = 'ajax') {
  ...
  $view = views_get_view('view-name');
  $view-&gt;set_display('display-name');
  $view-&gt;exposed_input['myfilter'] = 'filter-value';
  $view-&gt;pre_execute();

  // $output = t("This is some content delivered via AJAX");

  $output = $view-&gt;render();
  ...


This solution works fine for non OL views but fails to load a OL view into the wrapper div. What am I missing?

Gist files:


ajax_example.module
ajax_example.misc.inc
NOTE FROM THE QUESTION'S AUTHOR:
This solution works for non OpenLayers views. It fails to build up the wrapper when the view is an OpenLayers though.

So the question remains to be answered.




You have to create a div wrapper where you are supposed to show the
ajaxified view content. You have to add the div wrapper in the
template file (MYTEMPLATE.tpl.php)
Create a module that will register the ajax link in hook_menu().
In the callback of the menu item you have to render the view content
using ajax_commands.


Sample code:

function MYMODULE_init() {
  // Add the Drupal AJAX library.
  // You can optionally provide a check to add AJAX library only on certain pages.
  drupal_add_library('system', 'drupal.ajax');
}

function MYMODULE_menu() {
  $items = array();

  $items['ajax-link/nojs'] = array(
    'title' =&gt; 'TITLE',
    'description' =&gt; 'DESCRIPTION',
    'page callback' =&gt; 'MYMODULE_ajax_link_callback',
    'page arguments' =&gt; array(1),
    'access callback' =&gt; PERMISSION,
    'type' =&gt; MENU_CALLBACK,
  );

  $items['ajax-link/ajax'] = array(
    'delivery callback' =&gt; 'ajax_deliver',
  ) + $items['ajax-link/nojs'];
}

function MYMODULE_ajax_link_callback($ajax) {
  // Check whether this is AJAX callback.
  $is_ajax = $ajax === 'ajax';

  if ($is_ajax) {
    // Your code to obtain view content in HTML format and store it in some variable say $ajax_content.

    $commands = array();

    $commands[] = ajax_command_replace('div.WRAPPER-YOU-ADDED-PREVIOUSLY', $ajax_content);

    return array(
      '#type' =&gt; 'ajax',
      '#commands' =&gt; $commands,
    );

  }
  else {
    drupal_set_message('Content added');
    drupal_goto();
  }
}