-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathNodeNews.php
More file actions
175 lines (151 loc) · 4.55 KB
/
NodeNews.php
File metadata and controls
175 lines (151 loc) · 4.55 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
namespace Drupal\server_general\Plugin\EntityViewBuilder;
use Drupal\media\MediaInterface;
use Drupal\node\NodeInterface;
use Drupal\server_general\EntityViewBuilder\NodeViewBuilderAbstract;
use Drupal\server_general\SocialShareTrait;
use Drupal\server_general\TagTrait;
use Drupal\server_general\ThemeTrait\ElementNodeNewsThemeTrait;
use Drupal\server_general\ThemeTrait\NewsTeasersThemeTrait;
use Drupal\server_general\ThemeTrait\SearchThemeTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* The "Node News" plugin.
*
* @EntityViewBuilder(
* id = "node.news",
* label = @Translation("Node - News"),
* description = "Node view builder for News bundle."
* )
*/
class NodeNews extends NodeViewBuilderAbstract {
use ElementNodeNewsThemeTrait;
use NewsTeasersThemeTrait;
use SearchThemeTrait;
use SocialShareTrait;
use TagTrait;
/**
* The renderer.
*
* This is not used in this file, but the `SearchThemeTrait` uses it.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
$plugin = parent::create($container, $configuration, $plugin_id, $plugin_definition);
$plugin->renderer = $container->get('renderer');
return $plugin;
}
/**
* 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) {
// The node's label.
$node_type = $this->entityTypeManager->getStorage('node_type')->load($entity->bundle());
$label = $node_type->label();
// The hero responsive image.
$medias = $entity->get('field_featured_image')->referencedEntities();
$image = $this->buildEntities($medias, 'hero');
$element = $this->buildElementNodeNews(
$entity->label(),
$label,
$this->getFieldOrCreatedTimestamp($entity, 'field_publish_date'),
$image,
$this->buildProcessedText($entity),
$this->buildTags($entity),
$this->buildSocialShare($entity),
);
$build[] = $element;
return $build;
}
/**
* Build Teaser view mode.
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildTeaser(array $build, NodeInterface $entity) {
$media = $this->getReferencedEntityFromField($entity, 'field_featured_image');
$image = $media instanceof MediaInterface ? $this->buildImageStyle($media, 'card', 'field_media_image') : [];
$title = $entity->label();
$url = $entity->toUrl();
$summary = $this->buildProcessedTextTrimmed($entity, 'field_body');
$timestamp = $this->getFieldOrCreatedTimestamp($entity, 'field_publish_date');
$element = $this->buildElementNewsTeaser(
$image,
$title,
$url,
$summary,
$timestamp
);
$build[] = $element;
return $build;
}
/**
* Build "Featured" view mode.
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildFeatured(array $build, NodeInterface $entity) {
$media = $this->getReferencedEntityFromField($entity, 'field_featured_image');
$image = $media instanceof MediaInterface ? $this->buildImageStyle($media, 'card', 'field_media_image') : [];
$title = $entity->label();
$url = $entity->toUrl();
$summary = $this->buildProcessedText($entity, 'field_body');
$timestamp = $this->getFieldOrCreatedTimestamp($entity, 'field_publish_date');
$element = $this->buildElementNewsTeaserFeatured(
$image,
$title,
$url,
$summary,
$timestamp
);
$build[] = $element;
return $build;
}
/**
* Build "Search index" view mode.
*
* @param array $build
* The existing build.
* @param \Drupal\node\NodeInterface $entity
* The entity.
*
* @return array
* Render array.
*/
public function buildSearchIndex(array $build, NodeInterface $entity) {
$element = $this->buildElementSearchResult(
$this->t('News'),
$entity->label(),
$entity->toUrl(),
$this->buildProcessedText($entity, 'field_body'),
$this->getFieldOrCreatedTimestamp($entity, 'field_publish_date')
);
$build[] = $element;
return $build;
}
}