You are here

function _insert_block in Insert Block 8

Same name and namespace in other branches
  1. 7 insert_block.module \_insert_block()
  2. 8.x insert_block.module \_insert_block()
1 call to _insert_block()
FilterInsertBlock::process in lib/Drupal/insert_block/Plugin/Filter/FilterInsertBlock.php
Performs the filter processing.

File

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

Code

function _insert_block($text, FilterInterface $filter) {
  if (preg_match_all("/\\[block:([^\\]]+)+\\]/", $text, $match)) {

    // @todo implement role restrictions.
    $raw_tags = $repl = array();
    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[] = drupal_render($replacement);
    }
    return str_replace($raw_tags, $repl, $text);
  }
  return $text;
}