Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\block_content;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityViewBuilder;
|
Chris@0
|
8
|
Chris@0
|
9 /**
|
Chris@0
|
10 * View builder handler for custom blocks.
|
Chris@0
|
11 */
|
Chris@0
|
12 class BlockContentViewBuilder extends EntityViewBuilder {
|
Chris@0
|
13
|
Chris@0
|
14 /**
|
Chris@0
|
15 * {@inheritdoc}
|
Chris@0
|
16 */
|
Chris@0
|
17 public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL) {
|
Chris@0
|
18 return $this->viewMultiple([$entity], $view_mode, $langcode)[0];
|
Chris@0
|
19 }
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * {@inheritdoc}
|
Chris@0
|
23 */
|
Chris@0
|
24 public function viewMultiple(array $entities = [], $view_mode = 'full', $langcode = NULL) {
|
Chris@0
|
25 $build_list = parent::viewMultiple($entities, $view_mode, $langcode);
|
Chris@0
|
26 // Apply the buildMultiple() #pre_render callback immediately, to make
|
Chris@0
|
27 // bubbling of attributes and contextual links to the actual block work.
|
Chris@0
|
28 // @see \Drupal\block\BlockViewBuilder::buildBlock()
|
Chris@0
|
29 unset($build_list['#pre_render'][0]);
|
Chris@0
|
30 return $this->buildMultiple($build_list);
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 protected function getBuildDefaults(EntityInterface $entity, $view_mode) {
|
Chris@0
|
37 $build = parent::getBuildDefaults($entity, $view_mode);
|
Chris@0
|
38 // The custom block will be rendered in the wrapped block template already
|
Chris@0
|
39 // and thus has no entity template itself.
|
Chris@0
|
40 unset($build['#theme']);
|
Chris@0
|
41 return $build;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * {@inheritdoc}
|
Chris@0
|
46 */
|
Chris@0
|
47 protected function alterBuild(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
|
Chris@0
|
48 parent::alterBuild($build, $entity, $display, $view_mode);
|
Chris@0
|
49 // Add contextual links for this custom block.
|
Chris@0
|
50 if (!$entity->isNew()) {
|
Chris@0
|
51 $build['#contextual_links']['block_content'] = [
|
Chris@0
|
52 'route_parameters' => ['block_content' => $entity->id()],
|
Chris@0
|
53 'metadata' => ['changed' => $entity->getChangedTime()],
|
Chris@0
|
54 ];
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 }
|