You are here

class BlockShortcode in Shortcode 2.0.x

Same name and namespace in other branches
  1. 8 shortcode_basic_tags/src/Plugin/Shortcode/BlockShortcode.php \Drupal\shortcode_basic_tags\Plugin\Shortcode\BlockShortcode

Insert a custom block to the text.

Plugin annotation


@Shortcode(
  id = "block",
  title = @Translation("Block"),
  description = @Translation("Insert a block.")
)

Hierarchy

Expanded class hierarchy of BlockShortcode

File

shortcode_basic_tags/src/Plugin/Shortcode/BlockShortcode.php, line 21

Namespace

Drupal\shortcode_basic_tags\Plugin\Shortcode
View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockShortcode::$entityTypeManager protected property The entity type manager.
BlockShortcode::$renderer protected property The renderer service.
BlockShortcode::create public static function Creates an instance of the plugin. Overrides ShortcodeBase::create
BlockShortcode::process public function Performs the shortcode processing. Overrides ShortcodeInterface::process
BlockShortcode::tips public function Generates a filter's tip. Overrides ShortcodeBase::tips
BlockShortcode::__construct public function Constructs a new Shortcode plugin. Overrides ShortcodeBase::__construct
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
ShortcodeBase::$pluginId protected property The plugin ID of this filter. Overrides PluginBase::$pluginId
ShortcodeBase::$provider public property The name of the provider that owns this filter.
ShortcodeBase::$settings public property An associative array containing the configured settings of this filter.
ShortcodeBase::$status public property A Boolean indicating whether this filter is enabled.
ShortcodeBase::addClass public function Add a class into a classes string if not already inside.
ShortcodeBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies
ShortcodeBase::defaultConfiguration public function Gets default configuration for this plugin. Overrides ConfigurableInterface::defaultConfiguration
ShortcodeBase::getAttributes public function Combines user attributes with known attributes.
ShortcodeBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
ShortcodeBase::getDescription public function Returns the administrative description for this shortcode plugin. Overrides ShortcodeInterface::getDescription
ShortcodeBase::getImageProperties public function Returns image properties for a given image media entity id.
ShortcodeBase::getLabel public function Returns the administrative label for this shortcode plugin. Overrides ShortcodeInterface::getLabel
ShortcodeBase::getMediaField public function Get a media entity field.
ShortcodeBase::getMediaFileUrl public function Get the file url for a media object.
ShortcodeBase::getMidFromPath public function Extracts the media id from a 'media/x' system path.
ShortcodeBase::getTitleFromAttributes public function Returns a suitable title string given the user provided title and text.
ShortcodeBase::getType public function
ShortcodeBase::getUrlFromPath public function Returns a url to be used in a link element given path or url.
ShortcodeBase::render public function Wrapper for renderPlain.
ShortcodeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
ShortcodeBase::settingsForm public function Generates a shortcode's settings form. Overrides ShortcodeInterface::settingsForm
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.