You are here

block_filter.module in Block Filter 7

Same filename and directory in other branches
  1. 5 block_filter.module
  2. 6 block_filter.module

Block Filter provides a simple input filter to allow an editor (with permission) to embed any block on the site into the content

File

block_filter.module
View source
<?php

/**
 * @file
 * Block Filter provides a simple input filter to allow an editor (with permission) to embed any block on the site into the content
 */

/**
 * Implements hook_filter_info().
 */
function block_filter_filter_info() {
  return array(
    'block_filter' => array(
      'title' => t('Block Filter'),
      'description' => t('Provides a method of embedding blocks into content'),
      'process callback' => 'block_filter_filter_block_filter_process',
      'tips callback' => 'block_filter_filter_block_filter_tips',
    ),
  );
}

/**
 * Implements hook_filter_FILTER_process().
 */
function block_filter_filter_block_filter_process($text, $filter, $format, $langcode, $cache, $cache_id) {
  return preg_replace_callback('#\\[block:(.+):(.+)]#sU', 'block_filter_embed', $text);
}

/**
 * Implements hook_filter_tips().
 */
function block_filter_filter_block_filter_tips($filter, $format, $long) {
  if ($long) {
    return t('You may embed blocks using the syntax <code>[block:MODULE:DELTA]</code>, for example: <code>[block:system:0]</code> will provide the <em>Powered By Drupal</em> block.');
  }
  else {
    return t('You may embed blocks using the syntax <code>[block:MODULE:DELTA]</code>.');
  }
}

/**
 * Filter process callback
 */
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 '';
}

Functions

Namesort descending Description
block_filter_embed Filter process callback
block_filter_filter_block_filter_process Implements hook_filter_FILTER_process().
block_filter_filter_block_filter_tips Implements hook_filter_tips().
block_filter_filter_info Implements hook_filter_info().