You are here

function panels_ajax_router in Panels 7.3

Same name and namespace in other branches
  1. 8.3 panels.module \panels_ajax_router()
  2. 6.3 panels.module \panels_ajax_router()

Route Panels' AJAX calls to the correct object.

Panels' AJAX is controlled mostly by renderer objects. This menu callback accepts the incoming request, figures out which object should handle the request, and attempts to route it. If no object can be found, the default Panels editor object is used.

Calls are routed via the ajax_* method space. For example, if visiting panels/ajax/add-pane then $renderer::ajax_add_pane() will be called. This means commands can be added without having to create new callbacks.

The first argument *must always* be the cache key so that a cache object can be passed through. Other arguments will be passed through untouched so that the method can do whatever it needs to do.

1 string reference to 'panels_ajax_router'
panels_menu in ./panels.module
Implements hook_menu().

File

./panels.module, line 1588
Core functionality for the Panels engine.

Code

function panels_ajax_router() {
  $args = func_get_args();
  if (count($args) < 3) {
    return MENU_NOT_FOUND;
  }
  ctools_include('display-edit', 'panels');
  ctools_include('plugins', 'panels');
  ctools_include('ajax');
  ctools_include('modal');
  ctools_include('context');
  ctools_include('content');
  $plugin_name = array_shift($args);
  $method = array_shift($args);
  $cache_key = array_shift($args);
  $plugin = panels_get_display_renderer($plugin_name);
  if (!$plugin) {

    // This is the default renderer for handling AJAX commands.
    $plugin = panels_get_display_renderer('editor');
  }
  $cache = panels_edit_cache_get($cache_key);
  if (empty($cache)) {
    return MENU_ACCESS_DENIED;
  }
  $renderer = panels_get_renderer_handler($plugin, $cache->display);
  if (!$renderer) {
    return MENU_ACCESS_DENIED;
  }
  $method = 'ajax_' . str_replace('-', '_', $method);
  if (!method_exists($renderer, $method)) {
    return MENU_NOT_FOUND;
  }
  $renderer->cache =& $cache;
  ctools_include('cleanstring');
  $renderer->clean_key = ctools_cleanstring($cache_key);
  $op = $renderer
    ->get_panels_storage_op_for_ajax($method);
  if (!$cache->display
    ->access($op)) {
    return MENU_ACCESS_DENIED;
  }
  $output = call_user_func_array(array(
    $renderer,
    $method,
  ), $args);
  if (empty($output) && !empty($renderer->commands)) {
    return array(
      '#type' => 'ajax',
      '#commands' => $renderer->commands,
    );
  }
  else {
    return $output;
  }
}