/
NodeNews.php
100 lines (86 loc) · 2.52 KB
/
NodeNews.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace Drupal\server_general\Plugin\EntityViewBuilder;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
use Drupal\server_general\EntityViewBuilder\NodeViewBuilderAbstract;
/**
* The "Node News" plugin.
*
* @EntityViewBuilder(
* id = "node.news",
* label = @Translation("Node - News"),
* description = "Node view builder for News bundle."
* )
*/
class NodeNews extends NodeViewBuilderAbstract {
/**
* Build full view mode.
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildFull(array $build, NodeInterface $entity) {
$this->messenger()->addMessage('Add your Node News elements in \Drupal\server_general\Plugin\EntityViewBuilder\NodeNews');
// Header.
$build[] = $this->buildHeroHeader($entity, 'field_featured_image');
// Tags.
$build[] = $this->buildContentTags($entity);
// Body.
$element = $this->buildProcessedText($entity);
$build[] = $this->wrapElementWideContainer($element);
return $build;
}
/**
* Default build in "Teaser" view mode.
*
* Show nodes as "cards".
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildTeaser(array $build, NodeInterface $entity) {
$image_info = $this->getMediaImageAndAlt($entity, 'field_featured_image');
$element = parent::buildTeaser($build, $entity);
$element += [
'#image' => $image_info['url'] ?? NULL,
'#image_alt' => $image_info['alt'] ?? NULL,
'#tags' => $this->buildTags($entity),
'#body' => $this->buildProcessedText($entity),
];
$build[] = $element;
return $build;
}
/**
* Build card view mode.
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildCard(array $build, NodeInterface $entity) {
$media = $this->getReferencedEntityFromField($entity, 'field_featured_image');
$element = [
'#theme' => 'server_theme_card',
'#title' => $entity->label(),
'#image' => $media instanceof MediaInterface ? $this->buildImageStyle($media, 'large', 'field_media_image') : NULL,
'#url' => $entity->toUrl(),
'#body' => $this->buildProcessedText($entity),
];
$build[] = $element;
return $build;
}
}