AggregatorFeedBlock.php 5.63 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\aggregator\Plugin\Block\AggregatorFeedBlock.
6 7
 */

8
namespace Drupal\aggregator\Plugin\Block;
9

10 11
use Drupal\aggregator\FeedStorageInterface;
use Drupal\aggregator\ItemStorageInterface;
12
use Drupal\Core\Block\BlockBase;
13
use Drupal\Core\Cache\Cache;
14
use Drupal\Core\Entity\Query\QueryInterface;
15
use Drupal\Core\Form\FormStateInterface;
16
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
17
use Drupal\Core\Session\AccountInterface;
18
use Symfony\Component\DependencyInjection\ContainerInterface;
19 20 21 22

/**
 * Provides an 'Aggregator feed' block with the latest items from the feed.
 *
23
 * @Block(
24
 *   id = "aggregator_feed_block",
25 26
 *   admin_label = @Translation("Aggregator feed"),
 *   category = @Translation("Lists (Views)")
27 28
 * )
 */
29 30 31
class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInterface {

  /**
32
   * The entity storage for feeds.
33
   *
34
   * @var \Drupal\aggregator\FeedStorageInterface
35
   */
36
  protected $feedStorage;
37 38

  /**
39
   * The entity storage for items.
40
   *
41
   * @var \Drupal\aggregator\ItemStorageInterface
42
   */
43 44 45 46 47 48 49 50
  protected $itemStorage;

  /**
   * The entity query object for feed items.
   *
   * @var \Drupal\Core\Entity\Query\QueryInterface
   */
  protected $itemQuery;
51 52 53 54 55 56 57 58

  /**
   * Constructs an AggregatorFeedBlock object.
   *
   * @param array $configuration
   *   A configuration array containing information about the plugin instance.
   * @param string $plugin_id
   *   The plugin_id for the plugin instance.
59
   * @param mixed $plugin_definition
60
   *   The plugin implementation definition.
61 62 63 64
   * @param \Drupal\aggregator\FeedStorageInterface $feed_storage
   *   The entity storage for feeds.
   * @param \Drupal\aggregator\ItemStorageInterface $item_storage
   *   The entity storage for feed items.
65 66
   * @param \Drupal\Core\Entity\Query\QueryInterface $item_query
   *   The entity query object for feed items.
67
   */
68
  public function __construct(array $configuration, $plugin_id, $plugin_definition, FeedStorageInterface $feed_storage, ItemStorageInterface $item_storage, QueryInterface $item_query) {
69
    parent::__construct($configuration, $plugin_id, $plugin_definition);
70 71 72
    $this->feedStorage = $feed_storage;
    $this->itemStorage = $item_storage;
    $this->itemQuery = $item_query;
73 74 75 76 77 78
  }


  /**
   * {@inheritdoc}
   */
79
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
80 81 82 83
    return new static(
      $configuration,
      $plugin_id,
      $plugin_definition,
84 85
      $container->get('entity.manager')->getStorage('aggregator_feed'),
      $container->get('entity.manager')->getStorage('aggregator_item'),
86
      $container->get('entity.query')->get('aggregator_item')
87 88 89
    );
  }

90 91

  /**
92
   * {@inheritdoc}
93
   */
94
  public function defaultConfiguration() {
95 96 97
    // By default, the block will contain 10 feed items.
    return array(
      'block_count' => 10,
98
      'feed' => NULL,
99 100 101 102
    );
  }

  /**
103
   * {@inheritdoc}
104
   */
105
  protected function blockAccess(AccountInterface $account) {
106
    // Only grant access to users with the 'access news feeds' permission.
107
    return $account->hasPermission('access news feeds');
108 109 110
  }

  /**
111
   * {@inheritdoc}
112
   */
113
  public function blockForm($form, FormStateInterface $form_state) {
114
    $feeds = $this->feedStorage->loadMultiple();
115 116 117 118 119 120
    $options = array();
    foreach ($feeds as $feed) {
      $options[$feed->id()] = $feed->label();
    }
    $form['feed'] = array(
      '#type' => 'select',
121
      '#title' => $this->t('Select the feed that should be displayed'),
122 123 124
      '#default_value' => $this->configuration['feed'],
      '#options' => $options,
    );
125
    $range = range(2, 20);
126 127
    $form['block_count'] = array(
      '#type' => 'select',
128
      '#title' => $this->t('Number of news items in block'),
129
      '#default_value' => $this->configuration['block_count'],
130
      '#options' => array_combine($range, $range),
131 132 133 134 135
    );
    return $form;
  }

  /**
136
   * {@inheritdoc}
137
   */
138
  public function blockSubmit($form, FormStateInterface $form_state) {
139 140
    $this->configuration['block_count'] = $form_state->getValue('block_count');
    $this->configuration['feed'] = $form_state->getValue('feed');
141 142 143
  }

  /**
144
   * {@inheritdoc}
145
   */
146
  public function build() {
147
    // Load the selected feed.
148 149 150 151 152 153 154 155 156 157
    if ($feed = $this->feedStorage->load($this->configuration['feed'])) {
      $result = $this->itemQuery
        ->condition('fid', $feed->id())
        ->range(0, $this->configuration['block_count'])
        ->sort('timestamp', 'DESC')
        ->sort('iid', 'DESC')
        ->execute();

      $items = $this->itemStorage->loadMultiple($result);

158
      $more_link = array(
159
        '#type' => 'more_link',
160
        '#url' => $feed->urlInfo(),
161
        '#attributes' => array('title' => $this->t("View this feed's recent news.")),
162 163
      );
      $read_more = drupal_render($more_link);
164 165
      $rendered_items = array();
      foreach ($items as $item) {
166
        $aggregator_block_item = array(
167 168 169
          '#type' => 'link',
          '#href' => $item->getLink(),
          '#title' => $item->label(),
170
        );
171
        $rendered_items[] = drupal_render($aggregator_block_item);
172 173
      }
      // Only display the block if there are items to show.
174
      if (count($rendered_items) > 0) {
175 176
        $item_list = array(
          '#theme' => 'item_list',
177
          '#items' => $rendered_items,
178
        );
179
        return array(
180
          '#children' => drupal_render($item_list) . $read_more,
181 182 183 184 185
        );
      }
    }
  }

186 187 188 189 190 191
  /**
   * {@inheritdoc}
   */
  public function getCacheTags() {
    $cache_tags = parent::getCacheTags();
    $feed = $this->feedStorage->load($this->configuration['feed']);
192
    return Cache::mergeTags($cache_tags, $feed->getCacheTags());
193 194
  }

195
}