Security update for Core, with self-updated composer
[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\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;
16
17 /**
18  * Defines a generic custom block type.
19  *
20  * @Block(
21  *  id = "block_content",
22  *  admin_label = @Translation("Custom block"),
23  *  category = @Translation("Custom"),
24  *  deriver = "Drupal\block_content\Plugin\Derivative\BlockContent"
25  * )
26  */
27 class BlockContentBlock extends BlockBase implements ContainerFactoryPluginInterface {
28
29   /**
30    * The Plugin Block Manager.
31    *
32    * @var \Drupal\Core\Block\BlockManagerInterface.
33    */
34   protected $blockManager;
35
36   /**
37    * The entity type manager service.
38    *
39    * @var \Drupal\Core\Entity\EntityTypeManagerInterface
40    */
41   protected $entityTypeManager;
42
43   /**
44    * The Drupal account to use for checking for access to block.
45    *
46    * @var \Drupal\Core\Session\AccountInterface.
47    */
48   protected $account;
49
50   /**
51    * The block content entity.
52    *
53    * @var \Drupal\block_content\BlockContentInterface
54    */
55   protected $blockContent;
56
57   /**
58    * The URL generator.
59    *
60    * @var \Drupal\Core\Routing\UrlGeneratorInterface
61    */
62   protected $urlGenerator;
63
64   /**
65    * The block content UUID lookup service.
66    *
67    * @var \Drupal\block_content\BlockContentUuidLookup
68    */
69   protected $uuidLookup;
70
71   /**
72    * The entity display repository.
73    *
74    * @var \Drupal\Core\Entity\EntityDisplayRepositoryInterface
75    */
76   protected $entityDisplayRepository;
77
78   /**
79    * Constructs a new BlockContentBlock.
80    *
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
94    *   The 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.
99    */
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);
102
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;
109   }
110
111   /**
112    * {@inheritdoc}
113    */
114   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
115     return new static(
116       $configuration,
117       $plugin_id,
118       $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')
125     );
126   }
127
128   /**
129    * {@inheritdoc}
130    */
131   public function defaultConfiguration() {
132     return [
133       'status' => TRUE,
134       'info' => '',
135       'view_mode' => 'full',
136     ];
137   }
138
139   /**
140    * Overrides \Drupal\Core\Block\BlockBase::blockForm().
141    *
142    * Adds body and description fields to the block configuration form.
143    */
144   public function blockForm($form, FormStateInterface $form_state) {
145     $block = $this->getEntity();
146     if (!$block) {
147       return $form;
148     }
149     $options = $this->entityDisplayRepository->getViewModeOptionsByBundle('block_content', $block->bundle());
150
151     $form['view_mode'] = [
152       '#type' => 'select',
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),
158     ];
159     $form['title']['#description'] = $this->t('The title of the block as shown to the user.');
160     return $form;
161   }
162
163   /**
164    * {@inheritdoc}
165    */
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();
170   }
171
172   /**
173    * {@inheritdoc}
174    */
175   protected function blockAccess(AccountInterface $account) {
176     if ($this->getEntity()) {
177       return $this->getEntity()->access('view', $account, TRUE);
178     }
179     return AccessResult::forbidden();
180   }
181
182   /**
183    * {@inheritdoc}
184    */
185   public function build() {
186     if ($block = $this->getEntity()) {
187       return $this->entityTypeManager->getViewBuilder($block->getEntityTypeId())->view($block, $this->configuration['view_mode']);
188     }
189     else {
190       return [
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')
194         ]),
195         '#access' => $this->account->hasPermission('administer blocks')
196       ];
197     }
198   }
199
200   /**
201    * Loads the block content entity of the block.
202    *
203    * @return \Drupal\block_content\BlockContentInterface|null
204    *   The block content entity.
205    */
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);
211       }
212     }
213     return $this->blockContent;
214   }
215
216 }