Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / aggregator / src / Plugin / Block / AggregatorFeedBlock.php
1 <?php
2
3 namespace Drupal\aggregator\Plugin\Block;
4
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;
14
15 /**
16  * Provides an 'Aggregator feed' block with the latest items from the feed.
17  *
18  * @Block(
19  *   id = "aggregator_feed_block",
20  *   admin_label = @Translation("Aggregator feed"),
21  *   category = @Translation("Lists (Views)")
22  * )
23  */
24 class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInterface {
25
26   /**
27    * The entity storage for feeds.
28    *
29    * @var \Drupal\aggregator\FeedStorageInterface
30    */
31   protected $feedStorage;
32
33   /**
34    * The entity storage for items.
35    *
36    * @var \Drupal\aggregator\ItemStorageInterface
37    */
38   protected $itemStorage;
39
40   /**
41    * Constructs an AggregatorFeedBlock object.
42    *
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.
53    */
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;
58   }
59
60
61   /**
62    * {@inheritdoc}
63    */
64   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
65     return new static(
66       $configuration,
67       $plugin_id,
68       $plugin_definition,
69       $container->get('entity_type.manager')->getStorage('aggregator_feed'),
70       $container->get('entity_type.manager')->getStorage('aggregator_item')
71     );
72   }
73
74
75   /**
76    * {@inheritdoc}
77    */
78   public function defaultConfiguration() {
79     // By default, the block will contain 10 feed items.
80     return [
81       'block_count' => 10,
82       'feed' => NULL,
83     ];
84   }
85
86   /**
87    * {@inheritdoc}
88    */
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');
92   }
93
94   /**
95    * {@inheritdoc}
96    */
97   public function blockForm($form, FormStateInterface $form_state) {
98     $feeds = $this->feedStorage->loadMultiple();
99     $options = [];
100     foreach ($feeds as $feed) {
101       $options[$feed->id()] = $feed->label();
102     }
103     $form['feed'] = [
104       '#type' => 'select',
105       '#title' => $this->t('Select the feed that should be displayed'),
106       '#default_value' => $this->configuration['feed'],
107       '#options' => $options,
108     ];
109     $range = range(2, 20);
110     $form['block_count'] = [
111       '#type' => 'select',
112       '#title' => $this->t('Number of news items in block'),
113       '#default_value' => $this->configuration['block_count'],
114       '#options' => array_combine($range, $range),
115     ];
116     return $form;
117   }
118
119   /**
120    * {@inheritdoc}
121    */
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');
125   }
126
127   /**
128    * {@inheritdoc}
129    */
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')
138         ->execute();
139
140       if ($result) {
141         // Only display the block if there are items to show.
142         $items = $this->itemStorage->loadMultiple($result);
143
144         $build['list'] = [
145           '#theme' => 'item_list',
146           '#items' => [],
147         ];
148         foreach ($items as $item) {
149           $build['list']['#items'][$item->id()] = [
150             '#type' => 'link',
151             '#url' => $item->urlInfo(),
152             '#title' => $item->label(),
153           ];
154         }
155         $build['more_link'] = [
156           '#type' => 'more_link',
157           '#url' => $feed->urlInfo(),
158           '#attributes' => ['title' => $this->t("View this feed's recent news.")],
159         ];
160         return $build;
161       }
162     }
163   }
164
165   /**
166    * {@inheritdoc}
167    */
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());
172     }
173     return $cache_tags;
174   }
175
176 }