You are here

BlockShortcode.php in Shortcode 2.0.x

Same filename and directory in other branches
  1. 8 shortcode_basic_tags/src/Plugin/Shortcode/BlockShortcode.php

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;

/**
 * Insert a custom block to the text.
 *
 * @Shortcode(
 *   id = "block",
 *   title = @Translation("Block"),
 *   description = @Translation("Insert a block.")
 * )
 */
class BlockShortcode extends ShortcodeBase {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new Shortcode plugin.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin ID for the plugin instance.
   * @param mixed $plugin_definition
   *   The plugin implementation definition.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer service.
   */
  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;
  }

  /**
   * {@inheritdoc}
   */
  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'));
  }

  /**
   * {@inheritdoc}
   */
  public function process(array $attributes, $text, $langcode = Language::LANGCODE_NOT_SPECIFIED) {

    // Merge with default attributes.
    $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);
        }
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Classes

Namesort descending Description
BlockShortcode Insert a custom block to the text.