3 namespace Drupal\block_content\Plugin\Block;
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Block\BlockBase;
7 use Drupal\Core\Block\BlockManagerInterface;
8 use Drupal\Core\Entity\EntityManagerInterface;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
11 use Drupal\Core\Routing\UrlGeneratorInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Defines a generic custom block type.
19 * id = "block_content",
20 * admin_label = @Translation("Custom block"),
21 * category = @Translation("Custom"),
22 * deriver = "Drupal\block_content\Plugin\Derivative\BlockContent"
25 class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInterface {
28 * The Plugin Block Manager.
30 * @var \Drupal\Core\Block\BlockManagerInterface.
32 protected $blockManager;
35 * The entity manager service.
37 * @var \Drupal\Core\Entity\EntityManagerInterface
39 protected $entityManager;
42 * The Drupal account to use for checking for access to block.
44 * @var \Drupal\Core\Session\AccountInterface.
49 * The block content entity.
51 * @var \Drupal\block_content\BlockContentInterface
53 protected $blockContent;
58 * @var \Drupal\Core\Routing\UrlGeneratorInterface
60 protected $urlGenerator;
63 * Constructs a new BlockContentBlock.
65 * @param array $configuration
66 * A configuration array containing information about the plugin instance.
67 * @param string $plugin_id
68 * The plugin ID for the plugin instance.
69 * @param mixed $plugin_definition
70 * The plugin implementation definition.
71 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
72 * The Plugin Block Manager.
73 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
74 * The entity manager service.
75 * @param \Drupal\Core\Session\AccountInterface $account
76 * The account for which view access should be checked.
77 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
80 public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityManagerInterface $entity_manager, AccountInterface $account, UrlGeneratorInterface $url_generator) {
81 parent::__construct($configuration, $plugin_id, $plugin_definition);
83 $this->blockManager = $block_manager;
84 $this->entityManager = $entity_manager;
85 $this->account = $account;
86 $this->urlGenerator = $url_generator;
92 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
97 $container->get('plugin.manager.block'),
98 $container->get('entity.manager'),
99 $container->get('current_user'),
100 $container->get('url_generator')
107 public function defaultConfiguration() {
111 'view_mode' => 'full',
116 * Overrides \Drupal\Core\Block\BlockBase::blockForm().
118 * Adds body and description fields to the block configuration form.
120 public function blockForm($form, FormStateInterface $form_state) {
121 $uuid = $this->getDerivativeId();
122 $block = $this->entityManager->loadEntityByUuid('block_content', $uuid);
123 $options = $this->entityManager->getViewModeOptionsByBundle('block_content', $block->bundle());
125 $form['view_mode'] = [
127 '#options' => $options,
128 '#title' => $this->t('View mode'),
129 '#description' => $this->t('Output the block in this view mode.'),
130 '#default_value' => $this->configuration['view_mode'],
131 '#access' => (count($options) > 1),
133 $form['title']['#description'] = $this->t('The title of the block as shown to the user.');
140 public function blockSubmit($form, FormStateInterface $form_state) {
141 // Invalidate the block cache to update custom block-based derivatives.
142 $this->configuration['view_mode'] = $form_state->getValue('view_mode');
143 $this->blockManager->clearCachedDefinitions();
149 protected function blockAccess(AccountInterface $account) {
150 if ($this->getEntity()) {
151 return $this->getEntity()->access('view', $account, TRUE);
153 return AccessResult::forbidden();
159 public function build() {
160 if ($block = $this->getEntity()) {
161 return $this->entityManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
165 '#markup' => $this->t('Block with uuid %uuid does not exist. <a href=":url">Add custom block</a>.', [
166 '%uuid' => $this->getDerivativeId(),
167 ':url' => $this->urlGenerator->generate('block_content.add_page')
169 '#access' => $this->account->hasPermission('administer blocks')
175 * Loads the block content entity of the block.
177 * @return \Drupal\block_content\BlockContentInterface|null
178 * The block content entity.
180 protected function getEntity() {
181 $uuid = $this->getDerivativeId();
182 if (!isset($this->blockContent)) {
183 $this->blockContent = $this->entityManager->loadEntityByUuid('block_content', $uuid);
185 return $this->blockContent;