You are here

function homebox_prepare_block in Homebox 6.3

Same name and namespace in other branches
  1. 6.2 homebox.module \homebox_prepare_block()
  2. 7.3 homebox.module \homebox_prepare_block()
  3. 7.2 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 AHAH callbacks.

File

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

Code

function homebox_prepare_block($block_key, $page) {
  global $user;

  // 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];
    if ($user_settings !== FALSE && isset($user_settings[$block_key])) {
      $block_settings = homebox_merge_settings($block_settings, $user_settings[$block_key]);
    }
  }
  $block_settings['key'] = $block_key;
  $block = new stdClass();

  // Get the edit form early, in case it changes the block.
  if (module_hook($block_settings['module'], 'homebox_block_edit_form')) {
    $block->edit_form = drupal_get_form('homebox_block_edit_' . $block_settings['module'] . '_' . $block_settings['delta'] . '_form', $page, (object) $block_settings);

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

    // 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 = homebox_merge_settings($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 = $block_settings['title'];
  $block->module = $block_settings['module'];
  $block->delta = $block_settings['delta'];
  $block->region = (int) $block_settings['region'];
  $block->weight = (int) $block_settings['weight'];
  $block->status = (bool) $block_settings['status'];
  $block->open = (bool) $block_settings['open'];
  $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, $user)) {

    // 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, array(
      'homebox' => $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) || 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_result(db_query("SELECT title FROM {blocks} WHERE bid = %d", $block->bid));
  }
  if (!$block->subject && $block->module == 'views') {
    $block->subject = _homebox_get_view_name($block);
  }
  if (!$block->subject) {
    $module_blocks = module_invoke($block->module, 'block', 'list');
    $block->subject = $module_blocks[$block->delta]['info'];
  }

  // Fail safe
  if (!$block->subject) {
    $block->subject = t('<em>No title defined</em>');
  }
  return $block;
}