You are here

insert_block.module in Insert Block 8.x

Insert blocks into the body of a node

Sidebar blocks contain all sorts of nifty stuff, but sometimes you want to stick that stuff into the body of your node. Instead of using PHP snippets (a possible security hole on public sites), you can use this module. When it's activated...

[block: block ID]

...will insert the contents of a rendered sidebar block into the body of your node. If no delta is specified, the default block for that module will be displayed.

File

insert_block.module
View source
<?php

/**
 * @file
 * Insert blocks into the body of a node
 *
 * Sidebar blocks contain all sorts of nifty stuff, but sometimes you
 * want to stick that stuff into the body of your node. Instead of using
 * PHP snippets (a possible security hole on public sites), you can use
 * this module. When it's activated...
 *
 * [block: block ID]
 *
 * ...will insert the contents of a rendered sidebar block into the body
 * of your node. If no delta is specified, the default block for that
 * module will be displayed.
 */
use Drupal\filter\Plugin\FilterInterface;

/**
 * Implements of hook_help().
 */
function insert_block_help($path, $arg) {
  switch ($path) {
    case 'admin/help#insert_block':
      return t('<p>Use special tags to insert the contents of a block into a node.</p><p>You may use [block:<em>block_entity_id</em>] tags to display the contents of block. To discover block entity id, visit admin/structure/block and hover over a block\'s configure link and look in your browser\'s status bar. The last "word" you see is the block ID.</p>');
  }
}
function _insert_block($text, FilterInterface $filter) {
  if (preg_match_all("/\\[block:([^\\]]+)+\\]/", $text, $match)) {

    // @todo implement role restrictions.
    foreach ($match[1] as $key => $value) {
      $raw_tags[] = $match[0][$key];
      $block_id = $match[1][$key];
      $replacement = '';

      /** @var Drupal\block\BlockInterface $block */
      if ($block = entity_load('block', $block_id)) {
        $replacement = entity_view($block, 'block');
      }
      $repl[] = $replacement;
    }
    return str_replace($raw_tags, $repl, $text);
  }
  return $text;
}

Functions

Namesort descending Description
insert_block_help Implements of hook_help().
_insert_block