Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\aggregator;
|
Chris@0
|
4
|
Chris@18
|
5 use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
|
Chris@18
|
6 use Drupal\Core\Entity\EntityRepositoryInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityTypeInterface;
|
Chris@18
|
8 use Drupal\Core\Entity\EntityTypeManagerInterface;
|
Chris@0
|
9 use Drupal\Core\Entity\EntityViewBuilder;
|
Chris@0
|
10 use Drupal\Core\Config\Config;
|
Chris@0
|
11 use Drupal\Core\Language\LanguageManagerInterface;
|
Chris@18
|
12 use Drupal\Core\Theme\Registry;
|
Chris@0
|
13 use Drupal\Core\Url;
|
Chris@0
|
14 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * View builder handler for aggregator feeds.
|
Chris@0
|
18 */
|
Chris@0
|
19 class FeedViewBuilder extends EntityViewBuilder {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@18
|
22 * The 'aggregator.settings' config.
|
Chris@18
|
23 *
|
Chris@18
|
24 * @var \Drupal\Core\Config\Config
|
Chris@18
|
25 */
|
Chris@18
|
26 protected $config;
|
Chris@18
|
27
|
Chris@18
|
28 /**
|
Chris@18
|
29 * The entity type manager.
|
Chris@18
|
30 *
|
Chris@18
|
31 * @var \Drupal\Core\Entity\EntityTypeManagerInterface
|
Chris@18
|
32 */
|
Chris@18
|
33 protected $entityTypeManager;
|
Chris@18
|
34
|
Chris@18
|
35 /**
|
Chris@0
|
36 * Constructs a new FeedViewBuilder.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
|
Chris@0
|
39 * The entity type definition.
|
Chris@18
|
40 * @param \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository
|
Chris@18
|
41 * The entity repository service.
|
Chris@0
|
42 * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
|
Chris@0
|
43 * The language manager.
|
Chris@0
|
44 * @param \Drupal\Core\Config\Config $config
|
Chris@0
|
45 * The 'aggregator.settings' config.
|
Chris@18
|
46 * @param \Drupal\Core\Theme\Registry $theme_registry
|
Chris@18
|
47 * The theme registry.
|
Chris@18
|
48 * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
|
Chris@18
|
49 * The entity display repository.
|
Chris@18
|
50 * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
|
Chris@18
|
51 * The entity type manager.
|
Chris@0
|
52 */
|
Chris@18
|
53 public function __construct(EntityTypeInterface $entity_type, EntityRepositoryInterface $entity_repository, LanguageManagerInterface $language_manager, Config $config, Registry $theme_registry, EntityDisplayRepositoryInterface $entity_display_repository, EntityTypeManagerInterface $entity_type_manager) {
|
Chris@18
|
54 parent::__construct($entity_type, $entity_repository, $language_manager, $theme_registry, $entity_display_repository);
|
Chris@0
|
55 $this->config = $config;
|
Chris@18
|
56 $this->entityTypeManager = $entity_type_manager;
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 /**
|
Chris@0
|
60 * {@inheritdoc}
|
Chris@0
|
61 */
|
Chris@0
|
62 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
|
Chris@0
|
63 return new static(
|
Chris@0
|
64 $entity_type,
|
Chris@18
|
65 $container->get('entity.repository'),
|
Chris@0
|
66 $container->get('language_manager'),
|
Chris@18
|
67 $container->get('config.factory')->get('aggregator.settings'),
|
Chris@18
|
68 $container->get('theme.registry'),
|
Chris@18
|
69 $container->get('entity_display.repository'),
|
Chris@18
|
70 $container->get('entity_type.manager')
|
Chris@0
|
71 );
|
Chris@0
|
72 }
|
Chris@0
|
73
|
Chris@0
|
74 /**
|
Chris@0
|
75 * {@inheritdoc}
|
Chris@0
|
76 */
|
Chris@0
|
77 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
|
Chris@0
|
78 parent::buildComponents($build, $entities, $displays, $view_mode);
|
Chris@0
|
79
|
Chris@0
|
80 foreach ($entities as $id => $entity) {
|
Chris@0
|
81 $bundle = $entity->bundle();
|
Chris@0
|
82 $display = $displays[$bundle];
|
Chris@0
|
83
|
Chris@0
|
84 if ($display->getComponent('items')) {
|
Chris@0
|
85 // When in summary view mode, respect the list_max setting.
|
Chris@0
|
86 $limit = $view_mode == 'summary' ? $this->config->get('source.list_max') : 20;
|
Chris@0
|
87 // Retrieve the items attached to this feed.
|
Chris@18
|
88 $items = $this->entityTypeManager
|
Chris@0
|
89 ->getStorage('aggregator_item')
|
Chris@0
|
90 ->loadByFeed($entity->id(), $limit);
|
Chris@0
|
91
|
Chris@18
|
92 $build[$id]['items'] = $this->entityTypeManager
|
Chris@0
|
93 ->getViewBuilder('aggregator_item')
|
Chris@0
|
94 ->viewMultiple($items, $view_mode, $entity->language()->getId());
|
Chris@0
|
95
|
Chris@0
|
96 if ($view_mode == 'full') {
|
Chris@0
|
97 // Also add the pager.
|
Chris@0
|
98 $build[$id]['pager'] = ['#type' => 'pager'];
|
Chris@0
|
99 }
|
Chris@0
|
100 }
|
Chris@0
|
101
|
Chris@0
|
102 if ($display->getComponent('description')) {
|
Chris@0
|
103 $build[$id]['description'] = [
|
Chris@0
|
104 '#markup' => $entity->getDescription(),
|
Chris@0
|
105 '#allowed_tags' => _aggregator_allowed_tags(),
|
Chris@0
|
106 '#prefix' => '<div class="feed-description">',
|
Chris@0
|
107 '#suffix' => '</div>',
|
Chris@0
|
108 ];
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 if ($display->getComponent('image')) {
|
Chris@0
|
112 $image_link = [];
|
Chris@0
|
113 // Render the image as link if it is available.
|
Chris@0
|
114 $image = $entity->getImage();
|
Chris@0
|
115 $label = $entity->label();
|
Chris@0
|
116 $link_href = $entity->getWebsiteUrl();
|
Chris@0
|
117 if ($image && $label && $link_href) {
|
Chris@0
|
118 $link_title = [
|
Chris@0
|
119 '#theme' => 'image',
|
Chris@0
|
120 '#uri' => $image,
|
Chris@0
|
121 '#alt' => $label,
|
Chris@0
|
122 ];
|
Chris@0
|
123 $image_link = [
|
Chris@0
|
124 '#type' => 'link',
|
Chris@0
|
125 '#title' => $link_title,
|
Chris@0
|
126 '#url' => Url::fromUri($link_href),
|
Chris@0
|
127 '#options' => [
|
Chris@0
|
128 'attributes' => ['class' => ['feed-image']],
|
Chris@0
|
129 ],
|
Chris@0
|
130 ];
|
Chris@0
|
131 }
|
Chris@0
|
132 $build[$id]['image'] = $image_link;
|
Chris@0
|
133 }
|
Chris@0
|
134
|
Chris@0
|
135 if ($display->getComponent('feed_icon')) {
|
Chris@0
|
136 $build[$id]['feed_icon'] = [
|
Chris@0
|
137 '#theme' => 'feed_icon',
|
Chris@0
|
138 '#url' => $entity->getUrl(),
|
Chris@0
|
139 '#title' => t('@title feed', ['@title' => $entity->label()]),
|
Chris@0
|
140 ];
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 if ($display->getComponent('more_link')) {
|
Chris@0
|
144 $title_stripped = strip_tags($entity->label());
|
Chris@0
|
145 $build[$id]['more_link'] = [
|
Chris@0
|
146 '#type' => 'link',
|
Chris@0
|
147 '#title' => t('More<span class="visually-hidden"> posts about @title</span>', [
|
Chris@0
|
148 '@title' => $title_stripped,
|
Chris@0
|
149 ]),
|
Chris@0
|
150 '#url' => Url::fromRoute('entity.aggregator_feed.canonical', ['aggregator_feed' => $entity->id()]),
|
Chris@0
|
151 '#options' => [
|
Chris@0
|
152 'attributes' => [
|
Chris@0
|
153 'title' => $title_stripped,
|
Chris@0
|
154 ],
|
Chris@0
|
155 ],
|
Chris@0
|
156 ];
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 }
|
Chris@0
|
160 }
|
Chris@0
|
161
|
Chris@0
|
162 }
|