You are here

function homebox_prepare_block in Homebox 7.2

Same name and namespace in other branches
  1. 6.3 homebox.module \homebox_prepare_block()
  2. 6.2 homebox.module \homebox_prepare_block()
  3. 7.3 homebox.module \homebox_prepare_block()

Prepare a block for rendering with theme('homebox_block').

Parameters

$block_key: A string identifying the block. Should match an array key in $page->settings['blocks'], except for a user's custom block.

$page: A homebox page object, as loaded by homebox_get_page().

Return value

A block object that can be passed to theme('homebox_block', $block).

2 calls to homebox_prepare_block()
homebox_build in ./homebox.module
Responsible for firing the hook_theme().
homebox_build_block in ./homebox.module
Render a single block, for AJAX callbacks.

File

./homebox.module, line 544
Homebox main file, takes care of global functions settings constants, etc.

Code

function homebox_prepare_block($block_key, $page) {

  // Load block settings.
  $user_settings = _homebox_get_user_settings($page);
  if ($user_settings !== FALSE && isset($user_settings[$block_key])) {

    // Custom blocks only exist in user settings.
    $block_settings = $user_settings[$block_key];
  }
  else {

    // Otherwise, start with the page defaults.
    $block_settings = $page->settings['blocks'][$block_key];
  }
  $block_settings['key'] = $block_key;
  $block = new stdClass();

  // Get the edit form early, in case it changes the block.
  // Before retrieving the full form from drupal_get_form(), ensure the block is
  // configurable. Just checking for the form hook implementation isn't
  // sufficient since one module may provide more than one block.
  if (module_hook($block_settings['module'], 'homebox_block_edit_form') && module_invoke($block_settings['module'], 'homebox_block_keys', (object) $block_settings)) {

    // Save messages not rendered by this block to avoid displaying them here.
    $messages = isset($_SESSION['messages']) ? $_SESSION['messages'] : array();
    $_SESSION['messages'] = array();
    $get_form = drupal_get_form('homebox_block_edit_' . $block_settings['module'] . '_' . $block_settings['delta'] . '_form', $page, (object) $block_settings);
    $block->edit_form = drupal_render($get_form);

    // Prepend messages the form may have generated.
    $block->edit_form = theme('status_messages', array(
      'display' => 'error',
    )) . $block->edit_form;

    // Reset messages to state before rendering block.
    $_SESSION['messages'] = $messages;

    // Apply user settings, they may have changed from the form submission.
    if (isset($_POST['form_id']) && ($user_settings = _homebox_get_user_settings($page))) {
      $block_settings = array_merge($block_settings, $user_settings[$block_key]);
    }
  }

  // Make sure unclosable blocks are not closed.
  if (!$block_settings['closable']) {
    $block_settings['status'] = TRUE;
  }

  // Build the $block object.
  $block->key = $block_key;
  $block->subject = isset($page->settings['blocks'][$block_key]['title']) ? $page->settings['blocks'][$block_key]['title'] : NULL;
  $block->content = isset($block_settings['content']) ? $block_settings['content'] : NULL;
  $block->module = isset($block_settings['module']) ? $block_settings['module'] : NULL;
  $block->delta = isset($block_settings['delta']) ? $block_settings['delta'] : NULL;
  $block->region = isset($block_settings['region']) ? (int) $block_settings['region'] : 0;
  $block->weight = isset($block_settings['weight']) ? (int) $block_settings['weight'] : 0;
  $block->status = isset($block_settings['status']) && $block_settings['status'] ? TRUE : FALSE;
  $block->open = isset($block_settings['open']) && $block_settings['open'] ? TRUE : FALSE;
  $block->closable = (bool) $block_settings['closable'];
  $block->homebox_classes = _homebox_get_css_classes_for_block($block_settings);
  if (module_hook($block->module, 'homebox_block_keys')) {
    foreach (module_invoke($block->module, 'homebox_block_keys', $block) as $key) {
      $block->{$key} = isset($block_settings[$key]) ? $block_settings[$key] : NULL;
    }
  }

  // Check block permissions
  if (!_homebox_can_view_block($block)) {

    // Permission denied, skip to the next block
    return NULL;
  }

  // Attempt to find a block in the cache table.
  if ((bool) $page->settings['cache'] && !count(module_implements('node_grants')) && $_SERVER['REQUEST_METHOD'] == 'GET' && ($cid = homebox_get_cache_id($page, $block)) && ($cache = cache_get($cid, 'cache_block'))) {
    $array = $cache->data;
  }
  else {

    // No cache, fetch the blocks from modules
    $array = module_invoke($block->module, 'block_view', $block->delta, $block);

    // Allow modules to modify the block before it is viewed, via either
    // hook_block_view_alter() or hook_block_view_MODULE_DELTA_alter().
    drupal_alter(array(
      'block_view',
      "block_view_{$block->module}_{$block->delta}",
    ), $array, $block);

    // Block.module will return 'n/a' if a custom block has been deleted
    if (isset($array['content']) && $array['content'] == 'n/a') {

      // If this is the case, skip this block
      return NULL;
    }
    if (isset($cid)) {
      cache_set($cid, $array, 'cache_block', CACHE_TEMPORARY);
    }
  }

  // Render block content
  if (isset($array) && is_array($array)) {
    foreach ($array as $k => $v) {

      // If block has custom title, leave it
      if ($k == 'subject' && !empty($block->subject)) {
        continue;
      }
      $block->{$k} = $v;
    }
  }

  // We don't continue to assign this block since Drupal didn't returned any
  // content which could be permissions rules applied by any module.
  if (!isset($block->content) || !is_array($block->content) && trim($block->content) == "") {
    return NULL;
  }

  // If no title provided we try to get one from blocks table
  if (!$block->subject && isset($block->bid)) {
    $block->subject = db_query("SELECT title FROM {block} WHERE bid = :bid", array(
      ':bid' => $block->bid,
    ))
      ->fetchField();
  }
  if (!$block->subject && $block->module == 'views') {
    $block->subject = _homebox_get_view_name($block);
  }
  if (!$block->subject) {
    $module_blocks = module_invoke($block->module, 'block_info');
    $block->subject = $module_blocks[$block->delta]['info'];
  }

  // Sanitize $block->subject for display, like Drupal core does.
  if ($block->subject) {
    $block->subject = check_plain($block->subject);
  }
  else {
    $block->subject = t('<em>No title defined</em>');
  }
  return $block;
}