BlockShortcode.php in Shortcode 2.0.x
File
shortcode_basic_tags/src/Plugin/Shortcode/BlockShortcode.php
View source
<?php
namespace Drupal\shortcode_basic_tags\Plugin\Shortcode;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Render\RendererInterface;
use Drupal\shortcode\Plugin\ShortcodeBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class BlockShortcode extends ShortcodeBase {
protected $entityTypeManager;
protected $renderer;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, RendererInterface $renderer) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->entityTypeManager = $entity_type_manager;
$this->renderer = $renderer;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('renderer'));
}
public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {
$attributes = $this
->getAttributes([
'id' => '',
'view' => 'full',
], $attributes);
if ((int) $attributes['id']) {
$block_entity = BlockContent::load($attributes['id']);
if ($block_entity) {
$block_view = $this->entityTypeManager
->getViewBuilder('block_content')
->view($block_entity, $attributes['view']);
if ($block_view) {
return $this->renderer
->render($block_view);
}
}
}
}
public function tips($long = FALSE) {
$output = [];
$output[] = '<p><strong>' . $this
->t('[block id="1" (view="full") /]') . '</strong>';
$output[] = $this
->t('Inserts a block.') . '</p>';
if ($long) {
$output[] = '<p>' . $this
->t('The block display view can be specified using the <em>view</em> parameter.') . '</p>';
}
return implode(' ', $output);
}
}