FeedViewBuilder.php 4.46 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\FeedViewBuilder.
6 7 8 9
 */

namespace Drupal\aggregator;

10 11
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeInterface;
12
use Drupal\Core\Entity\EntityViewBuilder;
13 14
use Drupal\Core\Config\Config;
use Drupal\Core\Language\LanguageManagerInterface;
15
use Drupal\Core\Url;
16
use Symfony\Component\DependencyInjection\ContainerInterface;
17 18 19 20

/**
 * Render controller for aggregator feed items.
 */
21
class FeedViewBuilder extends EntityViewBuilder {
22

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
  /**
   * Constructs a new FeedViewBuilder.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type definition.
   * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
   *   The entity manager service.
   * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
   *   The language manager.
   * @param \Drupal\Core\Config\Config $config
   *   The 'aggregator.settings' config.
   */
  public function __construct(EntityTypeInterface $entity_type, EntityManagerInterface $entity_manager, LanguageManagerInterface $language_manager, Config $config) {
    parent::__construct($entity_type, $entity_manager, $language_manager);
    $this->config = $config;
  }

  /**
   * {@inheritdoc}
   */
  public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
    return new static(
      $entity_type,
      $container->get('entity.manager'),
      $container->get('language_manager'),
      $container->get('config.factory')->get('aggregator.settings')
    );
  }

52
  /**
53
   * {@inheritdoc}
54
   */
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
  public function buildComponents(array &$build, array $entities, array $displays, $view_mode, $langcode = NULL) {
    parent::buildComponents($build, $entities, $displays, $view_mode, $langcode);

    foreach ($entities as $id => $entity) {
      $bundle = $entity->bundle();
      $display = $displays[$bundle];

      if ($display->getComponent('items')) {
        // When in summary view mode, respect the list_max setting.
        $limit = $view_mode == 'summary' ? $this->config->get('source.list_max') : 20;
        // Retrieve the items attached to this feed.
        $items = $this->entityManager
          ->getStorage('aggregator_item')
          ->loadByFeed($entity->id(), $limit);

        $build[$id]['items'] = $this->entityManager
          ->getViewBuilder('aggregator_item')
          ->viewMultiple($items, $view_mode, $langcode);

        if ($view_mode == 'full') {
          // Also add the pager.
76
          $build[$id]['pager'] = array('#type' => 'pager');
77 78 79 80 81
        }
      }

      if ($display->getComponent('description')) {
        $build[$id]['description'] = array(
82 83
          '#markup' => $entity->getDescription(),
          '#allowed_tags' => _aggregator_allowed_tags(),
84 85 86 87 88 89 90 91 92 93 94 95 96 97
          '#prefix' => '<div class="feed-description">',
          '#suffix' => '</div>',
        );
      }

      if ($display->getComponent('image')) {
        $image_link = array();
        // Render the image as link if it is available.
        $image = $entity->getImage();
        $label = $entity->label();
        $link_href = $entity->getWebsiteUrl();
        if ($image && $label && $link_href) {
          $link_title = array(
            '#theme' => 'image',
98
            '#uri' => $image,
99 100 101 102 103
            '#alt' => $label,
          );
          $image_link = array(
            '#type' => 'link',
            '#title' => $link_title,
104
            '#url' => Url::fromUri($link_href),
105 106 107 108 109 110 111 112 113 114 115 116
            '#options' => array(
              'attributes' => array('class' => array('feed-image')),
            ),
          );
        }
        $build[$id]['image'] = $image_link;
      }

      if ($display->getComponent('feed_icon')) {
        $build[$id]['feed_icon'] = array(
          '#theme' => 'feed_icon',
          '#url' => $entity->getUrl(),
117
          '#title' => t('@title feed', array('@title' => $entity->label())),
118 119 120 121 122 123 124
        );
      }

      if ($display->getComponent('more_link')) {
        $title_stripped = strip_tags($entity->label());
        $build[$id]['more_link'] = array(
          '#type' => 'link',
125 126 127
          '#title' => t('More<span class="visually-hidden"> posts about @title</span>', array(
            '@title' => $title_stripped,
          )),
128
          '#url' => Url::fromRoute('entity.aggregator_feed.canonical', ['aggregator_feed' => $entity->id()]),
129 130 131 132 133 134 135 136 137
          '#options' => array(
            'attributes' => array(
              'title' => $title_stripped,
            ),
          ),
        );
      }

    }
138 139 140
  }

}