annotate core/modules/aggregator/src/FeedViewBuilder.php @ 0:c75dbcec494b

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