You are here

function _insert_block_substitute_tags in Insert Block 6

Same name and namespace in other branches
  1. 5 insert_block.module \_insert_block_substitute_tags()
1 call to _insert_block_substitute_tags()
insert_block_filter in ./insert_block.module
Implementation of hook_filter().

File

./insert_block.module, line 85
Insert blocks into the body of a node

Code

function _insert_block_substitute_tags($text, $format) {
  global $user;
  if (preg_match_all("/\\[block:([^=\\]]+)=?([^\\]]*)?\\]/i", $text, $match)) {
    $block_roles = _insert_block_roles();
    $raw_tags = $repl = array();
    foreach ($match[2] as $key => $value) {
      $module = $match[1][$key];
      $delta = $match[2][$key];
      $replacement = '';
      $block = (object) module_invoke($module, 'block', 'view', $delta);
      if (isset($block->content)) {
        $check_roles_settings = variable_get("insert_block_check_roles_{$format}", NULL);
        $check_roles = !empty($check_roles_settings);
        $block_settings_result = db_query("SELECT * FROM {blocks} WHERE module = '%s' AND delta = '%s'", $module, $delta);
        $block_settings = db_fetch_object($block_settings_result);

        // If a block has no roles associated, it is displayed for every role.
        // For blocks with roles associated, if none of the user's roles matches
        // the settings from this block, remove it from the block list.
        if ($check_roles && isset($block_roles[$block_settings->module][$block_settings->delta]) && !array_intersect($block_roles[$block_settings->module][$block_settings->delta], array_keys($user->roles))) {

          // No match.
          $block = NULL;
        }
        if ($block) {
          init_theme();
          global $theme_key;
          $title = db_result(db_query_range("SELECT b.title FROM {blocks} b WHERE b.theme = '%s' AND b.module = '%s' AND b.delta = '%s'", $theme_key, $module, $delta, 0, 1));
          if ($title) {
            $block->subject = $title == '<none>' ? '' : check_plain($title);
          }
          $block->module = $module;
          $block->delta = $delta;
          $block->region = 'insert_block';
          $raw_tags[] = $match[0][$key];
          $replacement = theme('block', $block);
        }
        $repl[] = $replacement;
      }
    }
    return str_replace($raw_tags, $repl, $text);
  }
  return $text;
}