Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\aggregator\Plugin\Block\AggregatorFeedBlock.
Dries Buytaert
committed
*/
namespace Drupal\aggregator\Plugin\Block;
Dries Buytaert
committed
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
Dries Buytaert
committed
use Drupal\Core\Annotation\Translation;
catch
committed
use Drupal\Core\Database\Connection;
use Drupal\Core\Entity\EntityStorageControllerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Session\AccountInterface;
catch
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
Dries Buytaert
committed
/**
* Provides an 'Aggregator feed' block with the latest items from the feed.
*
* @Block(
Dries Buytaert
committed
* id = "aggregator_feed_block",
* admin_label = @Translation("Aggregator feed"),
* category = @Translation("Lists (Views)")
Dries Buytaert
committed
* )
*/
catch
committed
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
class AggregatorFeedBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The entity storage controller for feeds.
*
* @var \Drupal\Core\Entity\EntityStorageControllerInterface
*/
protected $storageController;
/**
* The database connection.
*
* @var \Drupal\Core\Database\Connection
*/
protected $connection;
/**
* 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.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Entity\EntityStorageControllerInterface $storage_controller
* The entity storage controller for feeds.
* @param \Drupal\Core\Database\Connection $connection
* The database connection.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, EntityStorageControllerInterface $storage_controller, Connection $connection) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->storageController = $storage_controller;
$this->connection = $connection;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('plugin.manager.entity')->getStorageController('aggregator_feed'),
$container->get('database')
);
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function defaultConfiguration() {
Dries Buytaert
committed
// By default, the block will contain 10 feed items.
return array(
'block_count' => 10,
catch
committed
'feed' => NULL,
Dries Buytaert
committed
);
}
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function access(AccountInterface $account) {
Dries Buytaert
committed
// Only grant access to users with the 'access news feeds' permission.
return $account->hasPermission('access news feeds');
Dries Buytaert
committed
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state) {
catch
committed
$feeds = $this->storageController->loadMultiple();
$options = array();
foreach ($feeds as $feed) {
$options[$feed->id()] = $feed->label();
}
$form['feed'] = array(
'#type' => 'select',
'#title' => t('Select the feed that should be displayed'),
'#default_value' => $this->configuration['feed'],
'#options' => $options,
);
Dries Buytaert
committed
$form['block_count'] = array(
'#type' => 'select',
'#title' => t('Number of news items in block'),
'#default_value' => $this->configuration['block_count'],
'#options' => drupal_map_assoc(range(2, 20)),
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['block_count'] = $form_state['values']['block_count'];
catch
committed
$this->configuration['feed'] = $form_state['values']['feed'];
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function build() {
catch
committed
// Load the selected feed.
if ($feed = $this->storageController->load($this->configuration['feed'])) {
$result = $this->connection->queryRange("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", 0, $this->configuration['block_count'], array(':fid' => $feed->id()));
Alex Pott
committed
$more_link = array(
'#theme' => 'more_link',
catch
committed
'#url' => 'aggregator/sources/' . $feed->id(),
Alex Pott
committed
'#title' => t("View this feed's recent news."),
);
$read_more = drupal_render($more_link);
Dries Buytaert
committed
$items = array();
foreach ($result as $item) {
Alex Pott
committed
$aggregator_block_item = array(
'#theme' => 'aggregator_block_item',
'#item' => $item,
);
$items[] = drupal_render($aggregator_block_item);
Dries Buytaert
committed
}
// Only display the block if there are items to show.
if (count($items) > 0) {
Alex Pott
committed
$item_list = array(
'#theme' => 'item_list',
'#items' => $items,
);
Dries Buytaert
committed
return array(
Alex Pott
committed
'#children' => drupal_render($item_list) . $read_more,
Dries Buytaert
committed
);
}
}
}
}