You are here

function context_reaction_block::render_ajax in Context 7.3

Same name and namespace in other branches
  1. 6.3 plugins/context_reaction_block.inc \context_reaction_block::render_ajax()
  2. 6 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 608

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_get_http_header();
  if (array_key_exists('status', $headers) && ($headers['status'] == "404 Not Found" || $headers['status'] == "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_add_http_header('Content-Type', 'text/javascript; charset=utf-8');
  if (strpos($param, ',') !== FALSE) {
    list($bid, $context) = explode(',', $param);
    list($module, $delta) = explode('-', $bid, 2);

    // Check token to make sure user has access to block.
    if (!(user_access('administer contexts') || user_access('context ajax block access') || $this
      ->context_block_ajax_rendering_allowed($bid))) {
      echo drupal_json_encode(array(
        'status' => 0,
      ));
      exit;
    }

    // Ensure $bid is valid.
    $info = $this
      ->get_blocks();
    if (isset($info[$bid])) {
      module_load_include('module', 'block', 'block');
      $block = $info[$bid];
      $block->title = isset($block->title) ? $block->title : '';
      $block->context = $context;
      $block->region = '';
      $rendered_blocks = _block_render_blocks(array(
        $block,
      ));

      // For E_STRICT warning
      $block = array_shift($rendered_blocks);
      if (empty($block->content['#markup'])) {
        $block->content['#markup'] = "<div class='context-block-empty'>" . t('This block appears empty when displayed on this page.') . "</div>";
      }
      $block = $this
        ->editable_block($block);
      $renderable_block = _block_get_renderable_array(array(
        $block,
      ));

      // For E_STRICT warning
      echo drupal_json_encode(array(
        'status' => 1,
        'block' => drupal_render($renderable_block),
      ));
      drupal_exit();
    }
  }
  echo drupal_json_encode(array(
    'status' => 0,
  ));
  drupal_exit();
}