Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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    * {@inheritdoc}
62    */
63   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
64     return new static(
65       $configuration,
66       $plugin_id,
67       $plugin_definition,
68       $container->get('entity_type.manager')->getStorage('aggregator_feed'),
69       $container->get('entity_type.manager')->getStorage('aggregator_item')
70     );
71   }
72
73   /**
74    * {@inheritdoc}
75    */
76   public function defaultConfiguration() {
77     // By default, the block will contain 10 feed items.
78     return [
79       'block_count' => 10,
80       'feed' => NULL,
81     ];
82   }
83
84   /**
85    * {@inheritdoc}
86    */
87   protected function blockAccess(AccountInterface $account) {
88     // Only grant access to users with the 'access news feeds' permission.
89     return AccessResult::allowedIfHasPermission($account, 'access news feeds');
90   }
91
92   /**
93    * {@inheritdoc}
94    */
95   public function blockForm($form, FormStateInterface $form_state) {
96     $feeds = $this->feedStorage->loadMultiple();
97     $options = [];
98     foreach ($feeds as $feed) {
99       $options[$feed->id()] = $feed->label();
100     }
101     $form['feed'] = [
102       '#type' => 'select',
103       '#title' => $this->t('Select the feed that should be displayed'),
104       '#default_value' => $this->configuration['feed'],
105       '#options' => $options,
106     ];
107     $range = range(2, 20);
108     $form['block_count'] = [
109       '#type' => 'select',
110       '#title' => $this->t('Number of news items in block'),
111       '#default_value' => $this->configuration['block_count'],
112       '#options' => array_combine($range, $range),
113     ];
114     return $form;
115   }
116
117   /**
118    * {@inheritdoc}
119    */
120   public function blockSubmit($form, FormStateInterface $form_state) {
121     $this->configuration['block_count'] = $form_state->getValue('block_count');
122     $this->configuration['feed'] = $form_state->getValue('feed');
123   }
124
125   /**
126    * {@inheritdoc}
127    */
128   public function build() {
129     // Load the selected feed.
130     if ($feed = $this->feedStorage->load($this->configuration['feed'])) {
131       $result = $this->itemStorage->getQuery()
132         ->condition('fid', $feed->id())
133         ->range(0, $this->configuration['block_count'])
134         ->sort('timestamp', 'DESC')
135         ->sort('iid', 'DESC')
136         ->execute();
137
138       if ($result) {
139         // Only display the block if there are items to show.
140         $items = $this->itemStorage->loadMultiple($result);
141
142         $build['list'] = [
143           '#theme' => 'item_list',
144           '#items' => [],
145         ];
146         foreach ($items as $item) {
147           $build['list']['#items'][$item->id()] = [
148             '#type' => 'link',
149             '#url' => $item->urlInfo(),
150             '#title' => $item->label(),
151           ];
152         }
153         $build['more_link'] = [
154           '#type' => 'more_link',
155           '#url' => $feed->urlInfo(),
156           '#attributes' => ['title' => $this->t("View this feed's recent news.")],
157         ];
158         return $build;
159       }
160     }
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function getCacheTags() {
167     $cache_tags = parent::getCacheTags();
168     if ($feed = $this->feedStorage->load($this->configuration['feed'])) {
169       $cache_tags = Cache::mergeTags($cache_tags, $feed->getCacheTags());
170     }
171     return $cache_tags;
172   }
173
174 }