3 namespace Drupal\aggregator;
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;
11 use Symfony\Component\DependencyInjection\ContainerInterface;
14 * View builder handler for aggregator feeds.
16 class FeedViewBuilder extends EntityViewBuilder {
19 * Constructs a new FeedViewBuilder.
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.
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;
38 public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
41 $container->get('entity.manager'),
42 $container->get('language_manager'),
43 $container->get('config.factory')->get('aggregator.settings')
50 public function buildComponents(array &$build, array $entities, array $displays, $view_mode) {
51 parent::buildComponents($build, $entities, $displays, $view_mode);
53 foreach ($entities as $id => $entity) {
54 $bundle = $entity->bundle();
55 $display = $displays[$bundle];
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);
65 $build[$id]['items'] = $this->entityManager
66 ->getViewBuilder('aggregator_item')
67 ->viewMultiple($items, $view_mode, $entity->language()->getId());
69 if ($view_mode == 'full') {
70 // Also add the pager.
71 $build[$id]['pager'] = ['#type' => 'pager'];
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>',
84 if ($display->getComponent('image')) {
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) {
98 '#title' => $link_title,
99 '#url' => Url::fromUri($link_href),
101 'attributes' => ['class' => ['feed-image']],
105 $build[$id]['image'] = $image_link;
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()]),
116 if ($display->getComponent('more_link')) {
117 $title_stripped = strip_tags($entity->label());
118 $build[$id]['more_link'] = [
120 '#title' => t('More<span class="visually-hidden"> posts about @title</span>', [
121 '@title' => $title_stripped,
123 '#url' => Url::fromRoute('entity.aggregator_feed.canonical', ['aggregator_feed' => $entity->id()]),
126 'title' => $title_stripped,