You are here

function block_filter_embed in Block Filter 7

Same name and namespace in other branches
  1. 5 block_filter.module \block_filter_embed()
  2. 6 block_filter.module \block_filter_embed()

Filter process callback

1 string reference to 'block_filter_embed'
block_filter_filter_block_filter_process in ./block_filter.module
Implements hook_filter_FILTER_process().

File

./block_filter.module, line 49
Block Filter provides a simple input filter to allow an editor (with permission) to embed any block on the site into the content

Code

function block_filter_embed($match) {

  // We need to know the current user and theme
  global $user;
  global $theme_key;
  $recursion_prevention =& drupal_static(__FUNCTION__, array());

  // Extract the module and delta from the match
  $module = $match[1];
  $delta = $match[2];
  if (!empty($recursion_prevention[$module][$delta])) {
    return;
  }
  $recursion_prevention[$module][$delta] = TRUE;

  // Make sure this module implements the block_ info hook.
  if (!module_hook($module, 'block_info')) {
    return '';
  }

  // Initialise the theme
  drupal_theme_initialize();

  //Get the users roles
  $rids = array_keys($user->roles);

  // Query for the block against the Block Role table
  $query = db_select('block', 'b');
  $query
    ->leftJoin('block_role', 'r', 'b.module = r.module AND b.delta = r.delta');
  $block_info = $query
    ->fields('b')
    ->condition('b.module', $module)
    ->condition('b.delta', $delta)
    ->condition('b.theme', $theme_key)
    ->condition(db_or()
    ->condition('r.rid', $rids)
    ->condition('r.rid', NULL))
    ->range(0, 1)
    ->addTag('block_load')
    ->addTag('translatable')
    ->execute()
    ->fetchAllAssoc('bid');

  // If block not found
  if (empty($block_info)) {
    return '';
  }

  // TODO - drupal_alter('block_list', $blocks); - we cant seem to do this as
  // the node implementation causes an infinite loop with the filter
  // This seems to be the only relevant core alter hook
  // TODO - this isn't very nice... Maybe itterate over all
  // module_implements('block_list_alter') and skip 'node?
  block_block_list_alter($block_info);

  // The above "alter" will unset any blocks which are not valid
  if (!empty($block_info)) {
    $blocks = array();
    foreach ($block_info as $block) {
      $blocks["{$block->module}_{$block->delta}"] = $block;
    }

    // Render the block and prepare it for drupal to render
    $blocks = _block_render_blocks($blocks);
    $blocks = _block_get_renderable_array($blocks);

    // Now Drupal can render it
    return drupal_render($blocks);
  }

  // If we've ended up here, lets just provide an empty string.
  return '';
}