diff --git a/insert_block.module b/insert_block.module index 8403ae4..4d2aa78 100644 --- a/insert_block.module +++ b/insert_block.module @@ -16,8 +16,6 @@ * module will be displayed. */ -use Drupal\filter\Plugin\FilterInterface; - /** * Implements of hook_help(). */ @@ -27,27 +25,3 @@ function insert_block_help($path, $arg) { return t('

Use special tags to insert the contents of a block into a node.

You may use [block:block_entity_id] 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.

'); } } - -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; -} - diff --git a/lib/Drupal/insert_block/Plugin/Filter/FilterInsertBlock.php b/lib/Drupal/insert_block/Plugin/Filter/FilterInsertBlock.php deleted file mode 100644 index 4770095..0000000 --- a/lib/Drupal/insert_block/Plugin/Filter/FilterInsertBlock.php +++ /dev/null @@ -1,59 +0,0 @@ - 'checkbox', - '#title' => $this->t('Check roles permissions.'), - '#default_value' => $this->settings['check_roles'], - '#description' => $this->t('If user does not have permissions to view block it will be hidden.'), - ); - return $form; - } - - - /** - * {@inheritdoc} - */ - public function process($text, $langcode, $cache, $cache_id) { - return _insert_block($text, $this); - } - - /** - * {@inheritdoc} - */ - public function tips($long = FALSE) { - if ($long) { - return t('You may use [block:block_entity_id] 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.'); - } - else { - return t('You may use [block:block_entity_id] tags to display the contents of block.', - array("@insert_block_help" => url("filter/tips/filter_insert_block", array('fragment' => 'filter-insert_block')))); - } - } - -} \ No newline at end of file diff --git a/src/Plugin/Filter/InsertBlockFilter.php b/src/Plugin/Filter/InsertBlockFilter.php new file mode 100644 index 0000000..7dbd17e --- /dev/null +++ b/src/Plugin/Filter/InsertBlockFilter.php @@ -0,0 +1,85 @@ + 'checkbox', + '#title' => $this->t('Check roles permissions.'), + '#default_value' => $this->settings['check_roles'], + '#description' => $this->t('If user does not have permissions to view block it will be hidden.'), + ); + return $form; + } + + /** + * {@inheritdoc} + */ + public function process($text, $langcode) { + + 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 = ''; + if ($block = \Drupal::service('entity.manager')->getStorage('block')->load($block_id)) { + $block_view = \Drupal::service('entity.manager') + ->getViewBuilder('block') + ->view($block); + $replacement = \Drupal::service('renderer')->render($block_view); + } + + $repl[] = $replacement; + } + $text = str_replace($raw_tags, $repl, $text); + } + + return new FilterProcessResult($text); + + } + + /** + * {@inheritdoc} + */ + public function tips($long = FALSE) { + if ($long) { + return t('You may use [block:block_entity_id] 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.'); + } + else { + $tips_url = \Drupal\Core\Url::fromRoute("filter.tips_all", array(), array('fragment' => 'filter-insert_block')); + return t('You may use [block:block_entity_id] tags to display the contents of block.', + array("@insert_block_help" => $tips_url->toString())); + } + } + +} \ No newline at end of file