e97e740187e173d711ca776dc386b48eac43862b
[yaffs-website] / web / core / modules / block_content / src / Plugin / Block / BlockContentBlock.php
1 <?php
2
3 namespace Drupal\block_content\Plugin\Block;
4
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;
14
15 /**
16  * Defines a generic custom block type.
17  *
18  * @Block(
19  *  id = "block_content",
20  *  admin_label = @Translation("Custom block"),
21  *  category = @Translation("Custom"),
22  *  deriver = "Drupal\block_content\Plugin\Derivative\BlockContent"
23  * )
24  */
25 class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInterface {
26
27   /**
28    * The Plugin Block Manager.
29    *
30    * @var \Drupal\Core\Block\BlockManagerInterface.
31    */
32   protected $blockManager;
33
34   /**
35    * The entity manager service.
36    *
37    * @var \Drupal\Core\Entity\EntityManagerInterface
38    */
39   protected $entityManager;
40
41   /**
42    * The Drupal account to use for checking for access to block.
43    *
44    * @var \Drupal\Core\Session\AccountInterface.
45    */
46   protected $account;
47
48   /**
49    * The block content entity.
50    *
51    * @var \Drupal\block_content\BlockContentInterface
52    */
53   protected $blockContent;
54
55   /**
56    * The URL generator.
57    *
58    * @var \Drupal\Core\Routing\UrlGeneratorInterface
59    */
60   protected $urlGenerator;
61
62   /**
63    * Constructs a new BlockContentBlock.
64    *
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
78    *   The URL generator.
79    */
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);
82
83     $this->blockManager = $block_manager;
84     $this->entityManager = $entity_manager;
85     $this->account = $account;
86     $this->urlGenerator = $url_generator;
87   }
88
89   /**
90    * {@inheritdoc}
91    */
92   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
93     return new static(
94       $configuration,
95       $plugin_id,
96       $plugin_definition,
97       $container->get('plugin.manager.block'),
98       $container->get('entity.manager'),
99       $container->get('current_user'),
100       $container->get('url_generator')
101     );
102   }
103
104   /**
105    * {@inheritdoc}
106    */
107   public function defaultConfiguration() {
108     return [
109       'status' => TRUE,
110       'info' => '',
111       'view_mode' => 'full',
112     ];
113   }
114
115   /**
116    * Overrides \Drupal\Core\Block\BlockBase::blockForm().
117    *
118    * Adds body and description fields to the block configuration form.
119    */
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());
124
125     $form['view_mode'] = [
126       '#type' => 'select',
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),
132     ];
133     $form['title']['#description'] = $this->t('The title of the block as shown to the user.');
134     return $form;
135   }
136
137   /**
138    * {@inheritdoc}
139    */
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();
144   }
145
146   /**
147    * {@inheritdoc}
148    */
149   protected function blockAccess(AccountInterface $account) {
150     if ($this->getEntity()) {
151       return $this->getEntity()->access('view', $account, TRUE);
152     }
153     return AccessResult::forbidden();
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function build() {
160     if ($block = $this->getEntity()) {
161       return $this->entityManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
162     }
163     else {
164       return [
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')
168         ]),
169         '#access' => $this->account->hasPermission('administer blocks')
170       ];
171     }
172   }
173
174   /**
175    * Loads the block content entity of the block.
176    *
177    * @return \Drupal\block_content\BlockContentInterface|null
178    *   The block content entity.
179    */
180   protected function getEntity() {
181     $uuid = $this->getDerivativeId();
182     if (!isset($this->blockContent)) {
183       $this->blockContent = $this->entityManager->loadEntityByUuid('block_content', $uuid);
184     }
185     return $this->blockContent;
186   }
187
188 }