comparison core/modules/aggregator/src/FeedViewBuilder.php @ 0:4c8ae668cc8c

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