You are here

function context_reaction_block::render_ajax in Context 6

Same name and namespace in other branches
  1. 6.3 plugins/context_reaction_block.inc \context_reaction_block::render_ajax()
  2. 7.3 plugins/context_reaction_block.inc \context_reaction_block::render_ajax()

Block renderer for AJAX requests. Triggered when $_GET['context_block'] is set. See ->execute() for how this is called.

1 call to context_reaction_block::render_ajax()
context_reaction_block::execute in plugins/context_reaction_block.inc
Execute.

File

plugins/context_reaction_block.inc, line 565

Class

context_reaction_block
Expose blocks as context reactions.

Code

function render_ajax($param) {

  // Besure the page isn't a 404 or 403.
  $headers = drupal_set_header();
  foreach (explode("\n", $headers) as $header) {
    if ($header == "HTTP/1.1 404 Not Found" || $header == "HTTP/1.1 403 Forbidden") {
      return;
    }
  }

  // Set the header right away. This will inform any players in the stack
  // that we are in the middle of responding to an AJAX request.
  drupal_set_header('Content-Type: text/javascript; charset=utf-8');
  if (strpos($param, ',') !== FALSE) {
    list($bid, $context) = explode(',', $param);
    list($module, $delta) = explode('-', $bid, 2);

    // Ensure $bid is valid.
    $info = $this
      ->get_blocks();
    if (isset($info[$bid])) {
      $block = $info[$bid];
      $block->context = $context;
      $block->region = '';
      $block = $this
        ->build_block($block);
      if (empty($block->content)) {
        $block->content = "<div class='context-block-empty'>" . t('This block appears empty when displayed on this page.') . "</div>";
      }
      $css = array();

      // On behalf of panels we try to load some css, but we don't support
      // panels inside panels currently.
      if ($block->module == 'panels_mini') {
        $panel = panels_mini_load($block->delta);
        $layout = panels_get_layout($panel->display->layout);
        if (!empty($layout['css'])) {
          if (file_exists(path_to_theme() . '/' . $layout['css'])) {
            $css[] = path_to_theme() . '/' . $layout['css'];
          }
          else {
            $css[] = $layout['path'] . '/' . $layout['css'];
          }
        }
      }
      echo drupal_to_js(array(
        'status' => 1,
        'block' => theme('context_block_editable_block', $block),
        'css' => $css,
      ));
      exit;
    }
  }
  echo drupal_to_js(array(
    'status' => 0,
  ));
  exit;
}