FeedViewBuilder.php 4.3 KB

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