3 namespace Drupal\block_content\Plugin\Block;
5 use Drupal\block_content\BlockContentUuidLookup;
6 use Drupal\Core\Access\AccessResult;
7 use Drupal\Core\Block\BlockBase;
8 use Drupal\Core\Block\BlockManagerInterface;
9 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
10 use Drupal\Core\Entity\EntityTypeManagerInterface;
11 use Drupal\Core\Form\FormStateInterface;
12 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
13 use Drupal\Core\Routing\UrlGeneratorInterface;
14 use Drupal\Core\Session\AccountInterface;
15 use Symfony\Component\DependencyInjection\ContainerInterface;
18 * Defines a generic custom block type.
21 * id = "block_content",
22 * admin_label = @Translation("Custom block"),
23 * category = @Translation("Custom"),
24 * deriver = "Drupal\block_content\Plugin\Derivative\BlockContent"
27 class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInterface {
30 * The Plugin Block Manager.
32 * @var \Drupal\Core\Block\BlockManagerInterface
34 protected $blockManager;
37 * The entity type manager service.
39 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
41 protected $entityTypeManager;
44 * The Drupal account to use for checking for access to block.
46 * @var \Drupal\Core\Session\AccountInterface
51 * The block content entity.
53 * @var \Drupal\block_content\BlockContentInterface
55 protected $blockContent;
60 * @var \Drupal\Core\Routing\UrlGeneratorInterface
62 protected $urlGenerator;
65 * The block content UUID lookup service.
67 * @var \Drupal\block_content\BlockContentUuidLookup
69 protected $uuidLookup;
72 * The entity display repository.
74 * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
76 protected $entityDisplayRepository;
79 * Constructs a new BlockContentBlock.
81 * @param array $configuration
82 * A configuration array containing information about the plugin instance.
83 * @param string $plugin_id
84 * The plugin ID for the plugin instance.
85 * @param mixed $plugin_definition
86 * The plugin implementation definition.
87 * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
88 * The Plugin Block Manager.
89 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
90 * The entity type manager service.
91 * @param \Drupal\Core\Session\AccountInterface $account
92 * The account for which view access should be checked.
93 * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
95 * @param \Drupal\block_content\BlockContentUuidLookup $uuid_lookup
96 * The block content UUID lookup service.
97 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
98 * The entity display repository.
100 public function __construct(array $configuration, $plugin_id, $plugin_definition, BlockManagerInterface $block_manager, EntityTypeManagerInterface $entity_type_manager, AccountInterface $account, UrlGeneratorInterface $url_generator, BlockContentUuidLookup $uuid_lookup, EntityDisplayRepositoryInterface $entity_display_repository) {
101 parent::__construct($configuration, $plugin_id, $plugin_definition);
103 $this->blockManager = $block_manager;
104 $this->entityTypeManager = $entity_type_manager;
105 $this->account = $account;
106 $this->urlGenerator = $url_generator;
107 $this->uuidLookup = $uuid_lookup;
108 $this->entityDisplayRepository = $entity_display_repository;
114 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
119 $container->get('plugin.manager.block'),
120 $container->get('entity_type.manager'),
121 $container->get('current_user'),
122 $container->get('url_generator'),
123 $container->get('block_content.uuid_lookup'),
124 $container->get('entity_display.repository')
131 public function defaultConfiguration() {
135 'view_mode' => 'full',
140 * Overrides \Drupal\Core\Block\BlockBase::blockForm().
142 * Adds body and description fields to the block configuration form.
144 public function blockForm($form, FormStateInterface $form_state) {
145 $block = $this->getEntity();
149 $options = $this->entityDisplayRepository->getViewModeOptionsByBundle('block_content', $block->bundle());
151 $form['view_mode'] = [
153 '#options' => $options,
154 '#title' => $this->t('View mode'),
155 '#description' => $this->t('Output the block in this view mode.'),
156 '#default_value' => $this->configuration['view_mode'],
157 '#access' => (count($options) > 1),
159 $form['title']['#description'] = $this->t('The title of the block as shown to the user.');
166 public function blockSubmit($form, FormStateInterface $form_state) {
167 // Invalidate the block cache to update custom block-based derivatives.
168 $this->configuration['view_mode'] = $form_state->getValue('view_mode');
169 $this->blockManager->clearCachedDefinitions();
175 protected function blockAccess(AccountInterface $account) {
176 if ($this->getEntity()) {
177 return $this->getEntity()->access('view', $account, TRUE);
179 return AccessResult::forbidden();
185 public function build() {
186 if ($block = $this->getEntity()) {
187 return $this->entityTypeManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
191 '#markup' => $this->t('Block with uuid %uuid does not exist. <a href=":url">Add custom block</a>.', [
192 '%uuid' => $this->getDerivativeId(),
193 ':url' => $this->urlGenerator->generate('block_content.add_page'),
195 '#access' => $this->account->hasPermission('administer blocks'),
201 * Loads the block content entity of the block.
203 * @return \Drupal\block_content\BlockContentInterface|null
204 * The block content entity.
206 protected function getEntity() {
207 if (!isset($this->blockContent)) {
208 $uuid = $this->getDerivativeId();
209 if ($id = $this->uuidLookup->get($uuid)) {
210 $this->blockContent = $this->entityTypeManager->getStorage('block_content')->load($id);
213 return $this->blockContent;