3 namespace Drupal\aggregator\Plugin\Block;
5 use Drupal\aggregator\FeedStorageInterface;
6 use Drupal\aggregator\ItemStorageInterface;
7 use Drupal\Core\Access\AccessResult;
8 use Drupal\Core\Block\BlockBase;
9 use Drupal\Core\Cache\Cache;
10 use Drupal\Core\Form\FormStateInterface;
11 use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
12 use Drupal\Core\Session\AccountInterface;
13 use Symfony\Component\DependencyInjection\ContainerInterface;
16 * Provides an 'Aggregator feed' block with the latest items from the feed.
19 * id = "aggregator_feed_block",
20 * admin_label = @Translation("Aggregator feed"),
21 * category = @Translation("Lists (Views)")
24 class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInterface {
27 * The entity storage for feeds.
29 * @var \Drupal\aggregator\FeedStorageInterface
31 protected $feedStorage;
34 * The entity storage for items.
36 * @var \Drupal\aggregator\ItemStorageInterface
38 protected $itemStorage;
41 * Constructs an AggregatorFeedBlock object.
43 * @param array $configuration
44 * A configuration array containing information about the plugin instance.
45 * @param string $plugin_id
46 * The plugin_id for the plugin instance.
47 * @param mixed $plugin_definition
48 * The plugin implementation definition.
49 * @param \Drupal\aggregator\FeedStorageInterface $feed_storage
50 * The entity storage for feeds.
51 * @param \Drupal\aggregator\ItemStorageInterface $item_storage
52 * The entity storage for feed items.
54 public function __construct(array $configuration, $plugin_id, $plugin_definition, FeedStorageInterface $feed_storage, ItemStorageInterface $item_storage) {
55 parent::__construct($configuration, $plugin_id, $plugin_definition);
56 $this->feedStorage = $feed_storage;
57 $this->itemStorage = $item_storage;
64 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
69 $container->get('entity_type.manager')->getStorage('aggregator_feed'),
70 $container->get('entity_type.manager')->getStorage('aggregator_item')
78 public function defaultConfiguration() {
79 // By default, the block will contain 10 feed items.
89 protected function blockAccess(AccountInterface $account) {
90 // Only grant access to users with the 'access news feeds' permission.
91 return AccessResult::allowedIfHasPermission($account, 'access news feeds');
97 public function blockForm($form, FormStateInterface $form_state) {
98 $feeds = $this->feedStorage->loadMultiple();
100 foreach ($feeds as $feed) {
101 $options[$feed->id()] = $feed->label();
105 '#title' => $this->t('Select the feed that should be displayed'),
106 '#default_value' => $this->configuration['feed'],
107 '#options' => $options,
109 $range = range(2, 20);
110 $form['block_count'] = [
112 '#title' => $this->t('Number of news items in block'),
113 '#default_value' => $this->configuration['block_count'],
114 '#options' => array_combine($range, $range),
122 public function blockSubmit($form, FormStateInterface $form_state) {
123 $this->configuration['block_count'] = $form_state->getValue('block_count');
124 $this->configuration['feed'] = $form_state->getValue('feed');
130 public function build() {
131 // Load the selected feed.
132 if ($feed = $this->feedStorage->load($this->configuration['feed'])) {
133 $result = $this->itemStorage->getQuery()
134 ->condition('fid', $feed->id())
135 ->range(0, $this->configuration['block_count'])
136 ->sort('timestamp', 'DESC')
137 ->sort('iid', 'DESC')
141 // Only display the block if there are items to show.
142 $items = $this->itemStorage->loadMultiple($result);
145 '#theme' => 'item_list',
148 foreach ($items as $item) {
149 $build['list']['#items'][$item->id()] = [
151 '#url' => $item->urlInfo(),
152 '#title' => $item->label(),
155 $build['more_link'] = [
156 '#type' => 'more_link',
157 '#url' => $feed->urlInfo(),
158 '#attributes' => ['title' => $this->t("View this feed's recent news.")],
168 public function getCacheTags() {
169 $cache_tags = parent::getCacheTags();
170 if ($feed = $this->feedStorage->load($this->configuration['feed'])) {
171 $cache_tags = Cache::mergeTags($cache_tags, $feed->getCacheTags());