You are here

block_filter.module in Block Filter 5

Same filename and directory in other branches
  1. 6 block_filter.module
  2. 7 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
 */

/**
 * Implementation of hook_filter().
 */
function block_filter_filter($op, $delta = 0, $format = 1, $text = '') {
  switch ($op) {
    case 'list':
      return array(
        t('Block Filter'),
      );
    case 'description':
      return t('Provides a method of embedding blocks into content');
    case 'process':
      return preg_replace_callback('#\\[block:(.+):(.+)]#s', 'block_filter_embed', $text);
    default:
      return $text;
  }
}

/**
 * Implementation of hook_filter_tips().
 */
function block_filter_filter_tips($delta, $format, $long = FALSE) {
  if ($long) {
    return t('To embed any block into content, use the following syntat: <code>[block:{module}:{delta}]</code>, for example <code>[block:user:0]</code> (this will embed the user login form).');
  }
  else {
    return t('You may embed blocks using <code>[block:{module}:{delta}]</code>');
  }
}

/**
 *  Function to return a rendered block where $match is an array returned from the pattern matching in block_filter_filter.
 *
 *  @see block_filter_filter()
 *
 *  @param $match
 *   An array where key 0 is a sting of the full match, key 1 is the module name and key 2 is the delta- for example
 *   $match = array(0 => '[block:user:0]', 1=> 'user', 2 => 0)
 *
 *  @return
 *    a string containing the requested block in rendered form.
 */
function block_filter_embed($match) {
  global $user;
  $theme = variable_get('theme_default', 'garland');
  $module = $match[1];
  $delta = $match[2];

  //Make sure this module implements hook_block
  if (!module_hook($module, 'block')) {
    return '';
  }

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

  //Create a placeholder for each role ID
  $placeholders = implode(',', array_fill(0, count($rids), '%d'));

  //Run a query to get the block from the DB (this checks the block is enabled for thie current users role)
  $block = db_fetch_object(db_query("SELECT b.* FROM {blocks} b LEFT JOIN {blocks_roles} r ON b.module = r.module AND b.delta = r.delta WHERE b.module = '%s' AND b.delta = '%s' AND b.theme = '%s' AND (r.rid IN ({$placeholders}) OR r.rid IS NULL)", array_merge(array(
    $module,
    $delta,
    $theme,
  ), $rids)));

  // CODE BORROWED FROM block_list
  // Use the user's block visibility setting, if necessary
  if ($block->custom != 0) {
    if ($user->uid && isset($user->block[$block->module][$block->delta])) {
      $enabled = $user->block[$block->module][$block->delta];
    }
    else {
      $enabled = $block->custom == 1;
    }
  }
  else {
    $enabled = TRUE;
  }

  //Check the page display stuff
  if ($block->pages) {
    if ($block->visibility < 2) {
      $path = drupal_get_path_alias($_GET['q']);
      $regexp = '/^(' . preg_replace(array(
        '/(\\r\\n?|\\n)/',
        '/\\\\\\*/',
        '/(^|\\|)\\\\<front\\\\>($|\\|)/',
      ), array(
        '|',
        '.*',
        '\\1' . preg_quote(variable_get('site_frontpage', 'node'), '/') . '\\2',
      ), preg_quote($block->pages, '/')) . ')$/';

      // Compare with the internal and path alias (if any).
      $page_match = preg_match($regexp, $path);
      if ($path != $_GET['q']) {
        $page_match = $page_match || preg_match($regexp, $_GET['q']);
      }
      $page_match = !($block->visibility xor $page_match);
    }
    else {
      $page_match = drupal_eval($block->pages);
    }
  }
  else {
    $page_match = TRUE;
  }
  $block->enabled = $enabled;
  $block->page_match = $page_match;
  if ($block->enabled && $block->page_match) {

    // Check the current throttle status and see if block should be displayed based on server load.
    if (!($block->throttle && module_invoke('throttle', 'status') > 0)) {
      $array = module_invoke($block->module, 'block', 'view', $block->delta);
      if (isset($array) && is_array($array)) {
        foreach ($array as $k => $v) {
          $block->{$k} = $v;
        }
      }
    }
    if (isset($block->content) && $block->content) {

      // Override default block title if a custom display title is present.
      if ($block->title) {

        // Check plain here to allow module generated titles to keep any markup.
        $block->subject = $block->title == '<none>' ? '' : check_plain($block->title);
      }
      return theme('block', $block);
    }
  }
  return '';
}

Functions

Namesort descending Description
block_filter_embed Function to return a rendered block where $match is an array returned from the pattern matching in block_filter_filter.
block_filter_filter Implementation of hook_filter().
block_filter_filter_tips Implementation of hook_filter_tips().