Issue #1888702 by Berdir: Fixed Use configuration selection instead of derivatives for some blocks.
parent
5c88ea314d
commit
f0ef79ce8c
|
@ -200,13 +200,6 @@ function aggregator_schema() {
|
|||
'default' => 0,
|
||||
'description' => 'When the feed was last modified, as a Unix timestamp.',
|
||||
),
|
||||
'block' => array(
|
||||
'type' => 'int',
|
||||
'not null' => TRUE,
|
||||
'default' => 0,
|
||||
'size' => 'tiny',
|
||||
'description' => "Number of items to display in the feed's block.",
|
||||
)
|
||||
),
|
||||
'primary key' => array('fid'),
|
||||
'indexes' => array(
|
||||
|
|
|
@ -70,7 +70,6 @@ class AggregatorController extends ControllerBase implements ContainerInjectionI
|
|||
$feed = $entity_manager->getStorageController('aggregator_feed')
|
||||
->create(array(
|
||||
'refresh' => 3600,
|
||||
'block' => 5,
|
||||
));
|
||||
return $entity_manager->getForm($feed);
|
||||
}
|
||||
|
@ -174,7 +173,7 @@ class AggregatorController extends ControllerBase implements ContainerInjectionI
|
|||
* A render array as expected by drupal_render().
|
||||
*/
|
||||
public function adminOverview() {
|
||||
$result = $this->database->query('SELECT f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image, f.block, COUNT(i.iid) AS items FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image, f.block ORDER BY f.title');
|
||||
$result = $this->database->query('SELECT f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image, COUNT(i.iid) AS items FROM {aggregator_feed} f LEFT JOIN {aggregator_item} i ON f.fid = i.fid GROUP BY f.fid, f.title, f.url, f.refresh, f.checked, f.link, f.description, f.hash, f.etag, f.modified, f.image ORDER BY f.title');
|
||||
|
||||
$header = array($this->t('Title'), $this->t('Items'), $this->t('Last update'), $this->t('Next update'), $this->t('Operations'));
|
||||
$rows = array();
|
||||
|
|
|
@ -134,13 +134,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
*/
|
||||
public $modified;
|
||||
|
||||
/**
|
||||
* Number of items to display in the feed’s block.
|
||||
*
|
||||
* @var \Drupal\Core\Entity\Field\FieldInterface
|
||||
*/
|
||||
public $block;
|
||||
|
||||
/**
|
||||
* Overrides Drupal\Core\Entity\EntityNG::init().
|
||||
*/
|
||||
|
@ -160,7 +153,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
unset($this->hash);
|
||||
unset($this->etag);
|
||||
unset($this->modified);
|
||||
unset($this->block);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -208,10 +200,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public static function preDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
|
||||
// Invalidate the block cache to update aggregator feed-based derivatives.
|
||||
if (\Drupal::moduleHandler()->moduleExists('block')) {
|
||||
\Drupal::service('plugin.manager.block')->clearCachedDefinitions();
|
||||
}
|
||||
$storage_controller->deleteCategories($entities);
|
||||
foreach ($entities as $entity) {
|
||||
// Notify processors to remove stored items.
|
||||
|
@ -226,14 +214,15 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public static function postDelete(EntityStorageControllerInterface $storage_controller, array $entities) {
|
||||
foreach ($entities as $entity) {
|
||||
// Make sure there is no active block for this feed.
|
||||
$block_configs = config_get_storage_names_with_prefix('plugin.core.block');
|
||||
foreach ($block_configs as $config_id) {
|
||||
$config = \Drupal::config($config_id);
|
||||
if ($config->get('id') == 'aggregator_feed_block:' . $entity->id()) {
|
||||
$config->delete();
|
||||
}
|
||||
if (\Drupal::moduleHandler()->moduleExists('block')) {
|
||||
// Make sure there are no active blocks for these feeds.
|
||||
$ids = \Drupal::entityQuery('block')
|
||||
->condition('plugin', 'aggregator_feed_block')
|
||||
->condition('settings.feed', array_keys($entities))
|
||||
->execute();
|
||||
if ($ids) {
|
||||
$block_storage = \Drupal::entityManager()->getStorageController('block');
|
||||
$block_storage->delete($block_storage->loadMultiple($ids));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -244,7 +233,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
public function preSave(EntityStorageControllerInterface $storage_controller) {
|
||||
parent::preSave($storage_controller);
|
||||
|
||||
$this->clearBlockCacheDefinitions();
|
||||
$storage_controller->deleteCategories(array($this->id() => $this));
|
||||
}
|
||||
|
||||
|
@ -259,15 +247,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidate the block cache to update aggregator feed-based derivatives.
|
||||
*/
|
||||
protected function clearBlockCacheDefinitions() {
|
||||
if ($block_manager = \Drupal::getContainer()->get('plugin.manager.block', Container::NULL_ON_INVALID_REFERENCE)) {
|
||||
$block_manager->clearCachedDefinitions();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
|
@ -338,11 +317,6 @@ class Feed extends EntityNG implements FeedInterface {
|
|||
'description' => t('When the feed was last modified, as a Unix timestamp.'),
|
||||
'type' => 'integer_field',
|
||||
);
|
||||
$fields['block'] = array(
|
||||
'label' => t('Block'),
|
||||
'description' => t('Number of items to display in the feed’s block.'),
|
||||
'type' => 'integer_field',
|
||||
);
|
||||
return $fields;
|
||||
}
|
||||
|
||||
|
|
|
@ -54,12 +54,6 @@ class FeedFormController extends EntityFormControllerNG {
|
|||
'#options' => $period,
|
||||
'#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
|
||||
);
|
||||
$form['block'] = array('#type' => 'select',
|
||||
'#title' => $this->t('News items in block'),
|
||||
'#default_value' => $feed->block->value,
|
||||
'#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
|
||||
'#description' => $this->t("Drupal can make a block with the most recent news items of this feed. You can <a href=\"@block-admin\">configure blocks</a> to be displayed in the sidebar of your page. This setting lets you configure the number of news items to show in this feed's block. If you choose '0' this feed's block will be disabled.", array('@block-admin' => url('admin/structure/block'))),
|
||||
);
|
||||
|
||||
// Handling of categories.
|
||||
$options = array();
|
||||
|
|
|
@ -113,13 +113,6 @@ class OpmlFeedAdd extends FormBase {
|
|||
'#options' => $period,
|
||||
'#description' => $this->t('The length of time between feed updates. Requires a correctly configured <a href="@cron">cron maintenance task</a>.', array('@cron' => url('admin/reports/status'))),
|
||||
);
|
||||
$form['block'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => $this->t('News items in block'),
|
||||
'#default_value' => 5,
|
||||
'#options' => drupal_map_assoc(array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)),
|
||||
'#description' => $this->t("Drupal can make a block with the most recent news items of a feed. You can <a href=\"@block-admin\">configure blocks</a> to be displayed in the sidebar of your page. This setting lets you configure the number of news items to show in a feed's block. If you choose '0' these feeds' blocks will be disabled.", array('@block-admin' => url('admin/structure/block'))),
|
||||
);
|
||||
|
||||
// Handling of categories.
|
||||
$options = array_map('check_plain', $this->database->query("SELECT cid, title FROM {aggregator_category} ORDER BY title")->fetchAllKeyed());
|
||||
|
@ -215,7 +208,6 @@ class OpmlFeedAdd extends FormBase {
|
|||
'title' => $feed['title'],
|
||||
'url' => $feed['url'],
|
||||
'refresh' => $form_state['values']['refresh'],
|
||||
'block' => $form_state['values']['block'],
|
||||
));
|
||||
$new_feed->categories = $form_state['values']['category'];
|
||||
$new_feed->save();
|
||||
|
|
|
@ -7,20 +7,69 @@
|
|||
|
||||
namespace Drupal\aggregator\Plugin\Block;
|
||||
|
||||
use Drupal\aggregator\CategoryStorageControllerInterface;
|
||||
use Drupal\block\BlockBase;
|
||||
use Drupal\block\Annotation\Block;
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides an 'Aggregator category' block for the latest items in a category.
|
||||
*
|
||||
* @Block(
|
||||
* id = "aggregator_category_block",
|
||||
* admin_label = @Translation("Aggregator category"),
|
||||
* derivative = "Drupal\aggregator\Plugin\Derivative\AggregatorCategoryBlock"
|
||||
* admin_label = @Translation("Aggregator category")
|
||||
* )
|
||||
*/
|
||||
class AggregatorCategoryBlock extends BlockBase {
|
||||
class AggregatorCategoryBlock extends BlockBase implements ContainerFactoryPluginInterface {
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The category storage controller.
|
||||
*
|
||||
* @var \Drupal\aggregator\CategoryStorageControllerInterface
|
||||
*/
|
||||
protected $categoryStorageController;
|
||||
|
||||
/**
|
||||
* 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\Database\Connection $connection
|
||||
* The database connection.
|
||||
*/
|
||||
public function __construct(array $configuration, $plugin_id, array $plugin_definition, Connection $connection, CategoryStorageControllerInterface $category_storage_controller) {
|
||||
parent::__construct($configuration, $plugin_id, $plugin_definition);
|
||||
$this->connection = $connection;
|
||||
$this->categoryStorageController = $category_storage_controller;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, array $configuration, $plugin_id, array $plugin_definition) {
|
||||
return new static(
|
||||
$configuration,
|
||||
$plugin_id,
|
||||
$plugin_definition,
|
||||
$container->get('database'),
|
||||
$container->get('aggregator.category.storage')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -28,6 +77,7 @@ class AggregatorCategoryBlock extends BlockBase {
|
|||
public function defaultConfiguration() {
|
||||
// By default, the block will contain 10 feed items.
|
||||
return array(
|
||||
'cid' => 0,
|
||||
'block_count' => 10,
|
||||
);
|
||||
}
|
||||
|
@ -44,6 +94,18 @@ class AggregatorCategoryBlock extends BlockBase {
|
|||
* Overrides \Drupal\block\BlockBase::blockForm().
|
||||
*/
|
||||
public function blockForm($form, &$form_state) {
|
||||
$result = $this->connection->query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
|
||||
$options = array();
|
||||
foreach ($result as $category) {
|
||||
$options[$category->cid] = check_plain($category->title);
|
||||
}
|
||||
|
||||
$form['cid'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Select the category that should be displayed'),
|
||||
'#default_value' => $this->configuration['cid'],
|
||||
'#options' => $options,
|
||||
);
|
||||
$form['block_count'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Number of news items in block'),
|
||||
|
@ -57,6 +119,7 @@ class AggregatorCategoryBlock extends BlockBase {
|
|||
* Overrides \Drupal\block\BlockBase::blockSubmit().
|
||||
*/
|
||||
public function blockSubmit($form, &$form_state) {
|
||||
$this->configuration['cid'] = $form_state['values']['cid'];
|
||||
$this->configuration['block_count'] = $form_state['values']['block_count'];
|
||||
}
|
||||
|
||||
|
@ -64,9 +127,9 @@ class AggregatorCategoryBlock extends BlockBase {
|
|||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
$id = $this->getPluginId();
|
||||
if ($category = db_query('SELECT cid, title, block FROM {aggregator_category} WHERE cid = :cid', array(':cid' => $id))->fetchObject()) {
|
||||
$result = db_query_range('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', 0, $this->configuration['block_count'], array(':cid' => $category->cid));
|
||||
$cid = $this->configuration['cid'];
|
||||
if ($category = $this->categoryStorageController->load($cid)) {
|
||||
$result = $this->connection->queryRange('SELECT i.* FROM {aggregator_category_item} ci LEFT JOIN {aggregator_item} i ON ci.iid = i.iid WHERE ci.cid = :cid ORDER BY i.timestamp DESC, i.iid DESC', 0, $this->configuration['block_count'], array(':cid' => $category->cid));
|
||||
$more_link = array(
|
||||
'#theme' => 'more_link',
|
||||
'#url' => 'aggregator/categories/' . $category->cid,
|
||||
|
|
|
@ -10,17 +10,69 @@ namespace Drupal\aggregator\Plugin\Block;
|
|||
use Drupal\block\BlockBase;
|
||||
use Drupal\block\Annotation\Block;
|
||||
use Drupal\Core\Annotation\Translation;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\Entity\EntityStorageControllerInterface;
|
||||
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides an 'Aggregator feed' block with the latest items from the feed.
|
||||
*
|
||||
* @Block(
|
||||
* id = "aggregator_feed_block",
|
||||
* admin_label = @Translation("Aggregator feed"),
|
||||
* derivative = "Drupal\aggregator\Plugin\Derivative\AggregatorFeedBlock"
|
||||
* admin_label = @Translation("Aggregator feed")
|
||||
* )
|
||||
*/
|
||||
class AggregatorFeedBlock extends BlockBase {
|
||||
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')
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
|
@ -29,6 +81,7 @@ class AggregatorFeedBlock extends BlockBase {
|
|||
// By default, the block will contain 10 feed items.
|
||||
return array(
|
||||
'block_count' => 10,
|
||||
'feed' => NULL,
|
||||
);
|
||||
}
|
||||
|
||||
|
@ -44,6 +97,17 @@ class AggregatorFeedBlock extends BlockBase {
|
|||
* Overrides \Drupal\block\BlockBase::blockForm().
|
||||
*/
|
||||
public function blockForm($form, &$form_state) {
|
||||
$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,
|
||||
);
|
||||
$form['block_count'] = array(
|
||||
'#type' => 'select',
|
||||
'#title' => t('Number of news items in block'),
|
||||
|
@ -58,19 +122,19 @@ class AggregatorFeedBlock extends BlockBase {
|
|||
*/
|
||||
public function blockSubmit($form, &$form_state) {
|
||||
$this->configuration['block_count'] = $form_state['values']['block_count'];
|
||||
$this->configuration['feed'] = $form_state['values']['feed'];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function build() {
|
||||
// Plugin IDs look something like this: aggregator_feed_block:1.
|
||||
list(, $id) = explode(':', $this->getPluginId());
|
||||
if ($feed = db_query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $id))->fetchObject()) {
|
||||
$result = db_query_range("SELECT * FROM {aggregator_item} WHERE fid = :fid ORDER BY timestamp DESC, iid DESC", 0, $this->configuration['block_count'], array(':fid' => $id));
|
||||
// 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()));
|
||||
$more_link = array(
|
||||
'#theme' => 'more_link',
|
||||
'#url' => 'aggregator/sources/' . $feed->fid,
|
||||
'#url' => 'aggregator/sources/' . $feed->id(),
|
||||
'#title' => t("View this feed's recent news."),
|
||||
);
|
||||
$read_more = drupal_render($more_link);
|
||||
|
@ -91,6 +155,7 @@ class AggregatorFeedBlock extends BlockBase {
|
|||
return array(
|
||||
'#children' => drupal_render($item_list) . $read_more,
|
||||
);
|
||||
return $build;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,112 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\aggregator\Plugin\Derivative\AggregatorCategoryBlock.
|
||||
*/
|
||||
|
||||
namespace Drupal\aggregator\Plugin\Derivative;
|
||||
|
||||
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides block plugin definitions for aggregator categories.
|
||||
*
|
||||
* @see \Drupal\aggregator\Plugin\block\block\AggregatorCategoryBlock
|
||||
*/
|
||||
class AggregatorCategoryBlock implements ContainerDerivativeInterface {
|
||||
|
||||
/**
|
||||
* List of derivative definitions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $derivatives = array();
|
||||
|
||||
/**
|
||||
* The base plugin ID this derivative is for.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $basePluginId;
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The translation manager.
|
||||
*
|
||||
* @var \Drupal\Core\StringTranslation\TranslationInterface
|
||||
*/
|
||||
protected $translationManager;
|
||||
|
||||
/**
|
||||
* Constructs a AggregatorCategoryBlock object.
|
||||
*
|
||||
* @param string $base_plugin_id
|
||||
* The base plugin ID.
|
||||
* @param \Drupal\Core\Database\Connection $connection
|
||||
* The database connection.
|
||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
|
||||
* The translation manager.
|
||||
*/
|
||||
public function __construct($base_plugin_id, Connection $connection, TranslationInterface $translation_manager) {
|
||||
$this->basePluginId = $base_plugin_id;
|
||||
$this->connection = $connection;
|
||||
$this->translationManager = $translation_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, $base_plugin_id) {
|
||||
return new static(
|
||||
$base_plugin_id,
|
||||
$container->get('database'),
|
||||
$container->get('string_translation')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinition().
|
||||
*/
|
||||
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
|
||||
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
|
||||
return $this->derivatives[$derivative_id];
|
||||
}
|
||||
$result = $this->connection->query('SELECT cid, title FROM {aggregator_category} ORDER BY title WHERE cid = :cid', array(':cid' => $derivative_id))->fetchObject();
|
||||
$this->derivatives[$derivative_id] = $base_plugin_definition;
|
||||
$this->derivatives[$derivative_id]['admin_label'] = $this->t('@title category latest items', array('@title' => $result->title));
|
||||
return $this->derivatives[$derivative_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements \Drupal\Component\Plugin\Derivative\DerivativeInterface::getDerivativeDefinitions().
|
||||
*/
|
||||
public function getDerivativeDefinitions(array $base_plugin_definition) {
|
||||
// Provide a block plugin definition for each aggregator category.
|
||||
$result = $this->connection->query('SELECT cid, title FROM {aggregator_category} ORDER BY title');
|
||||
foreach ($result as $category) {
|
||||
$this->derivatives[$category->cid] = $base_plugin_definition;
|
||||
$this->derivatives[$category->cid]['admin_label'] = $this->t('@title category latest items', array('@title' => $category->title));
|
||||
}
|
||||
return $this->derivatives;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a string to the current language or to a given language.
|
||||
*
|
||||
* See the t() documentation for details.
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->translationManager->translate($string, $args, $options);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,114 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\aggregator\Plugin\Derivative\AggregatorFeedBlock.
|
||||
*/
|
||||
|
||||
namespace Drupal\aggregator\Plugin\Derivative;
|
||||
|
||||
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
|
||||
use Drupal\Core\Database\Connection;
|
||||
use Drupal\Core\StringTranslation\TranslationInterface;
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
|
||||
/**
|
||||
* Provides block plugin definitions for aggregator feeds.
|
||||
*
|
||||
* @see \Drupal\aggregator\Plugin\block\block\AggregatorFeedBlock
|
||||
*/
|
||||
class AggregatorFeedBlock implements ContainerDerivativeInterface {
|
||||
|
||||
/**
|
||||
* List of derivative definitions.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $derivatives = array();
|
||||
|
||||
/**
|
||||
* The base plugin ID this derivative is for.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $basePluginId;
|
||||
|
||||
/**
|
||||
* The database connection.
|
||||
*
|
||||
* @var \Drupal\Core\Database\Connection
|
||||
*/
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* The translation manager
|
||||
*
|
||||
* @var \Drupal\Core\StringTranslation\TranslationInterface
|
||||
*/
|
||||
protected $translationManager;
|
||||
|
||||
/**
|
||||
* Constructs a AggregatorFeedBlock object.
|
||||
*
|
||||
* @param string $base_plugin_id
|
||||
* The base plugin ID.
|
||||
* @param \Drupal\Core\Database\Connection $connection
|
||||
* The database connection.
|
||||
* @param \Drupal\Core\StringTranslation\TranslationInterface $translation_manager
|
||||
* The translation manager.
|
||||
*/
|
||||
public function __construct($base_plugin_id, Connection $connection, TranslationInterface $translation_manager) {
|
||||
$this->basePluginId = $base_plugin_id;
|
||||
$this->connection = $connection;
|
||||
$this->translationManager = $translation_manager;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function create(ContainerInterface $container, $base_plugin_id) {
|
||||
return new static(
|
||||
$base_plugin_id,
|
||||
$container->get('database'),
|
||||
$container->get('string_translation')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinition($derivative_id, array $base_plugin_definition) {
|
||||
if (!empty($this->derivatives) && !empty($this->derivatives[$derivative_id])) {
|
||||
return $this->derivatives[$derivative_id];
|
||||
}
|
||||
$result = $this->connection->query('SELECT fid, title, block FROM {aggregator_feed} WHERE block <> 0 AND fid = :fid', array(':fid' => $derivative_id))->fetchObject();
|
||||
$this->derivatives[$derivative_id] = $base_plugin_definition;
|
||||
$this->derivatives[$derivative_id]['delta'] = $result->fid;
|
||||
$this->derivatives[$derivative_id]['admin_label'] = $this->t('@title feed latest items', array('@title' => $result->title));
|
||||
return $this->derivatives[$derivative_id];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getDerivativeDefinitions(array $base_plugin_definition) {
|
||||
// Add a block plugin definition for each feed.
|
||||
$result = $this->connection->query('SELECT fid, title FROM {aggregator_feed} WHERE block <> 0 ORDER BY fid');
|
||||
foreach ($result as $feed) {
|
||||
$this->derivatives[$feed->fid] = $base_plugin_definition;
|
||||
$this->derivatives[$feed->fid]['delta'] = $feed->fid;
|
||||
$this->derivatives[$feed->fid]['admin_label'] = $this->t('@title feed latest items', array('@title' => $feed->title));
|
||||
}
|
||||
return $this->derivatives;
|
||||
}
|
||||
|
||||
/**
|
||||
* Translates a string to the current language or to a given language.
|
||||
*
|
||||
* See the t() documentation for details.
|
||||
*/
|
||||
protected function t($string, array $args = array(), array $options = array()) {
|
||||
return $this->translationManager->translate($string, $args, $options);
|
||||
}
|
||||
|
||||
}
|
|
@ -40,9 +40,6 @@ class AggregatorRenderingTest extends AggregatorTestBase {
|
|||
$feed = $this->createFeed();
|
||||
$this->updateFeedItems($feed, $this->getDefaultFeedItemCount());
|
||||
|
||||
// Clear the block cache to load the new block definitions.
|
||||
$this->container->get('plugin.manager.block')->clearCachedDefinitions();
|
||||
|
||||
// Need admin user to be able to access block admin.
|
||||
$admin_user = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
|
@ -52,7 +49,12 @@ class AggregatorRenderingTest extends AggregatorTestBase {
|
|||
));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
$block = $this->drupalPlaceBlock("aggregator_feed_block:{$feed->id()}", array('label' => 'feed-' . $feed->label(), 'block_count' => 2));
|
||||
$block = $this->drupalPlaceBlock("aggregator_feed_block", array('label' => 'feed-' . $feed->label()));
|
||||
|
||||
// Configure the feed that should be displayed.
|
||||
$block->getPlugin()->setConfigurationValue('feed', $feed->id());
|
||||
$block->getPlugin()->setConfigurationValue('block_count', 2);
|
||||
$block->save();
|
||||
|
||||
// Confirm that the block is now being displayed on pages.
|
||||
$this->drupalGet('test-page');
|
||||
|
@ -70,8 +72,8 @@ class AggregatorRenderingTest extends AggregatorTestBase {
|
|||
|
||||
// Set the number of news items to 0 to test that the block does not show
|
||||
// up.
|
||||
$feed->block = 0;
|
||||
$feed->save();
|
||||
$block->getPlugin()->setConfigurationValue('block_count', 0);
|
||||
$block->save();
|
||||
// Check that the block is no longer displayed.
|
||||
$this->drupalGet('test-page');
|
||||
$this->assertNoText($block->label(), 'Feed block is not displayed on the page when number of items is set to 0.');
|
||||
|
|
|
@ -11,6 +11,14 @@ namespace Drupal\aggregator\Tests;
|
|||
* Tests categorization functionality in the Aggregator module.
|
||||
*/
|
||||
class CategorizeFeedItemTest extends AggregatorTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Categorize feed item functionality',
|
||||
|
@ -54,7 +62,7 @@ class CategorizeFeedItemTest extends AggregatorTestBase {
|
|||
|
||||
// For each category of a feed, ensure feed items have that category, too.
|
||||
if (!empty($feed->categories) && !empty($feed->items)) {
|
||||
foreach ($feed->categories as $category) {
|
||||
foreach ($feed->categories as $cid) {
|
||||
$categorized_count = db_select('aggregator_category_item')
|
||||
->condition('iid', $feed->items, 'IN')
|
||||
->countQuery()
|
||||
|
@ -65,6 +73,20 @@ class CategorizeFeedItemTest extends AggregatorTestBase {
|
|||
}
|
||||
}
|
||||
|
||||
// Place a category block.
|
||||
$block = $this->drupalPlaceBlock("aggregator_category_block", array('label' => 'category-' . $category->title));
|
||||
|
||||
// Configure the feed that should be displayed.
|
||||
$block->getPlugin()->setConfigurationValue('cid', $category->cid);
|
||||
$block->save();
|
||||
|
||||
// Visit the frontpage, assert that the block and the feeds are displayed.
|
||||
$this->drupalGet('');
|
||||
$this->assertText('category-' . $category->title);
|
||||
foreach (\Drupal::entityManager()->getStorageController('aggregator_item')->loadMultiple() as $item) {
|
||||
$this->assertText($item->label());
|
||||
}
|
||||
|
||||
// Delete feed.
|
||||
$this->deleteFeed($feed);
|
||||
}
|
||||
|
|
|
@ -37,7 +37,7 @@ class CategorizeFeedTest extends AggregatorTestBase {
|
|||
$categories = $this->getCategories();
|
||||
|
||||
// Create a feed and assign 2 categories to it.
|
||||
$feed = $this->getFeedEditObject(NULL, array('block' => 5));
|
||||
$feed = $this->getFeedEditObject();
|
||||
foreach ($categories as $cid => $category) {
|
||||
$feed->categories[$cid] = $cid;
|
||||
}
|
||||
|
|
|
@ -11,6 +11,14 @@ namespace Drupal\aggregator\Tests;
|
|||
* Tests functionality for removing feeds in the Aggregator module.
|
||||
*/
|
||||
class RemoveFeedTest extends AggregatorTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block');
|
||||
|
||||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Remove feed functionality',
|
||||
|
@ -23,17 +31,31 @@ class RemoveFeedTest extends AggregatorTestBase {
|
|||
* Removes a feed and ensures that all of its services are removed.
|
||||
*/
|
||||
function testRemoveFeed() {
|
||||
$feed = $this->createFeed();
|
||||
$feed1 = $this->createFeed();
|
||||
$feed2 = $this->createFeed();
|
||||
|
||||
// Place a block for both feeds.
|
||||
$block = $this->drupalPlaceBlock('aggregator_feed_block');
|
||||
$block->getPlugin()->setConfigurationValue('feed', $feed1->id());
|
||||
$block->save();
|
||||
$block2 = $this->drupalPlaceBlock('aggregator_feed_block');
|
||||
$block2->getPlugin()->setConfigurationValue('feed', $feed2->id());
|
||||
$block2->save();
|
||||
|
||||
// Delete feed.
|
||||
$this->deleteFeed($feed);
|
||||
$this->deleteFeed($feed1);
|
||||
$this->assertText($feed2->label());
|
||||
$block_storage = $this->container->get('entity.manager')->getStorageController('block');
|
||||
$this->assertNull($block_storage->load($block->id()), 'Block for the deleted feed was deleted.');
|
||||
$this->assertEqual($block2->id(), $block_storage->load($block2->id())->id(), 'Block for not deleted feed still exists.');
|
||||
|
||||
// Check feed source.
|
||||
$this->drupalGet('aggregator/sources/' . $feed->id());
|
||||
$this->drupalGet('aggregator/sources/' . $feed1->id());
|
||||
$this->assertResponse(404, 'Deleted feed source does not exists.');
|
||||
|
||||
// Check database for feed.
|
||||
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed->label(), ':url' => $feed->url->value))->fetchField();
|
||||
$result = db_query("SELECT COUNT(*) FROM {aggregator_feed} WHERE title = :title AND url = :url", array(':title' => $feed1->label(), ':url' => $feed1->url->value))->fetchField();
|
||||
$this->assertFalse($result, 'Feed not found in database');
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -330,6 +330,12 @@ function _block_rehash($theme = NULL) {
|
|||
$regions = system_region_list($theme);
|
||||
$blocks = entity_load_multiple_by_properties('block', array('theme' => $theme));
|
||||
foreach ($blocks as $block_id => $block) {
|
||||
// Remove any invalid block from the list.
|
||||
// @todo Remove this check as part of https://drupal.org/node/1776830.
|
||||
if (!$block->getPlugin()) {
|
||||
unset($blocks[$block_id]);
|
||||
continue;
|
||||
}
|
||||
$region = $block->get('region');
|
||||
$status = $block->status();
|
||||
// Disable blocks in invalid regions.
|
||||
|
@ -412,7 +418,11 @@ function block_list($region) {
|
|||
global $theme;
|
||||
$blocks = array();
|
||||
foreach (entity_load_multiple_by_properties('block', array('theme' => $theme)) as $block_id => $block) {
|
||||
$blocks[$block->get('region')][$block_id] = $block;
|
||||
// Onlye include valid blocks in the list.
|
||||
// @todo Remove this check as part of https://drupal.org/node/1776830.
|
||||
if ($block->getPlugin()) {
|
||||
$blocks[$block->get('region')][$block_id] = $block;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,29 +0,0 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\block\BlockStorageController.
|
||||
*/
|
||||
|
||||
namespace Drupal\block;
|
||||
|
||||
use Drupal\Core\Config\Entity\ConfigStorageController;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
|
||||
/**
|
||||
* Defines the storage controller class for Block entities.
|
||||
*/
|
||||
class BlockStorageController extends ConfigStorageController {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadMultiple(array $ids = NULL) {
|
||||
$entities = parent::loadMultiple($ids);
|
||||
// Only blocks with a valid plugin should be loaded.
|
||||
return array_filter($entities, function ($entity) {
|
||||
return $entity->getPlugin();
|
||||
});
|
||||
}
|
||||
|
||||
}
|
|
@ -22,7 +22,7 @@ use Drupal\Core\Entity\EntityStorageControllerInterface;
|
|||
* label = @Translation("Block"),
|
||||
* module = "block",
|
||||
* controllers = {
|
||||
* "storage" = "Drupal\block\BlockStorageController",
|
||||
* "storage" = "Drupal\Core\Config\Entity\ConfigStorageController",
|
||||
* "access" = "Drupal\block\BlockAccessController",
|
||||
* "render" = "Drupal\block\BlockRenderController",
|
||||
* "list" = "Drupal\block\BlockListController",
|
||||
|
|
|
@ -7,20 +7,21 @@
|
|||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Component\Utility\Unicode;
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\aggregator\Tests\AggregatorTestBase;
|
||||
use Drupal\simpletest\WebTestBase;
|
||||
|
||||
/**
|
||||
* Tests multilingual block definition caching.
|
||||
*/
|
||||
class BlockLanguageCacheTest extends AggregatorTestBase {
|
||||
class BlockLanguageCacheTest extends WebTestBase {
|
||||
|
||||
/**
|
||||
* Modules to enable.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
public static $modules = array('block', 'language');
|
||||
public static $modules = array('block', 'language', 'menu');
|
||||
|
||||
/**
|
||||
* List of langcodes.
|
||||
|
@ -32,7 +33,7 @@ class BlockLanguageCacheTest extends AggregatorTestBase {
|
|||
public static function getInfo() {
|
||||
return array(
|
||||
'name' => 'Multilingual blocks',
|
||||
'description' => 'Checks display of aggregator blocks with multiple languages.',
|
||||
'description' => 'Checks display of menu blocks with multiple languages.',
|
||||
'group' => 'Block',
|
||||
);
|
||||
}
|
||||
|
@ -60,10 +61,7 @@ class BlockLanguageCacheTest extends AggregatorTestBase {
|
|||
$admin_user = $this->drupalCreateUser(array(
|
||||
'administer blocks',
|
||||
'access administration pages',
|
||||
'administer news feeds',
|
||||
'access news feeds',
|
||||
'create article content',
|
||||
'administer languages',
|
||||
'administer menu',
|
||||
));
|
||||
$this->drupalLogin($admin_user);
|
||||
|
||||
|
@ -72,14 +70,16 @@ class BlockLanguageCacheTest extends AggregatorTestBase {
|
|||
$this->drupalGet('admin/structure/block', array('language' => $langcode));
|
||||
}
|
||||
|
||||
// Create a feed in the default language.
|
||||
$this->createSampleNodes();
|
||||
$feed = $this->createFeed();
|
||||
// Create a menu in the default language.
|
||||
$edit['label'] = $this->randomName();
|
||||
$edit['id'] = Unicode::strtolower($edit['label']);
|
||||
$this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));
|
||||
$this->assertText(t('Menu @label has been added.', array('@label' => $edit['label'])));
|
||||
|
||||
// Check that the block is listed for all languages.
|
||||
foreach ($this->langcodes as $langcode) {
|
||||
$this->drupalGet('admin/structure/block', array('language' => $langcode));
|
||||
$this->assertText($feed->label());
|
||||
$this->assertText($edit['label']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,19 +7,15 @@
|
|||
|
||||
namespace Drupal\block\Tests;
|
||||
|
||||
use Drupal\Core\Language\Language;
|
||||
use Drupal\Core\Config\Entity\ConfigStorageController;
|
||||
use Drupal\simpletest\DrupalUnitTestBase;
|
||||
use Drupal\block_test\Plugin\Block\TestHtmlIdBlock;
|
||||
use Drupal\Component\Plugin\Exception\PluginException;
|
||||
use Drupal\block\BlockStorageController;
|
||||
use Drupal\Core\Entity\EntityMalformedException;
|
||||
use Drupal\block\Entity\Block;
|
||||
use Drupal\block\BlockInterface;
|
||||
|
||||
/**
|
||||
* Tests the storage of blocks.
|
||||
*
|
||||
* @see \Drupal\block\BlockStorageController
|
||||
*/
|
||||
class BlockStorageUnitTest extends DrupalUnitTestBase {
|
||||
|
||||
|
@ -33,7 +29,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
|
|||
/**
|
||||
* The block storage controller.
|
||||
*
|
||||
* @var \Drupal\block\BlockStorageController.
|
||||
* @var \Drupal\Core\Config\Entity\ConfigStorageController.
|
||||
*/
|
||||
protected $controller;
|
||||
|
||||
|
@ -55,7 +51,7 @@ class BlockStorageUnitTest extends DrupalUnitTestBase {
|
|||
* Tests CRUD operations.
|
||||
*/
|
||||
public function testBlockCRUD() {
|
||||
$this->assertTrue($this->controller instanceof BlockStorageController, 'The block storage controller is loaded.');
|
||||
$this->assertTrue($this->controller instanceof ConfigStorageController, 'The block storage controller is loaded.');
|
||||
|
||||
// Run each test method in the same installation.
|
||||
$this->createTests();
|
||||
|
|
|
@ -36,7 +36,7 @@ class BlockFormControllerTest extends UnitTestCase {
|
|||
* @see \Drupal\block\BlockFormController::getUniqueMachineName()
|
||||
*/
|
||||
public function testGetUniqueMachineName() {
|
||||
$block_storage = $this->getMockBuilder('Drupal\block\BlockStorageController')
|
||||
$block_storage = $this->getMockBuilder('Drupal\Core\Config\Entity\ConfigStorageController')
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$blocks = array();
|
||||
|
|
Loading…
Reference in New Issue