Issue #2043581 by larowlan, kim.pepper: Move aggregator_load_feed_items() to the ItemStorageController, retain procedural wrapper.
parent
28cf8aa9f0
commit
91bd01a852
|
@ -88,13 +88,13 @@ function aggregator_page_category_form($form, $form_state, $category) {
|
|||
/**
|
||||
* Loads and optionally filters feed items.
|
||||
*
|
||||
* @param $type
|
||||
* @param string $type
|
||||
* The type of filter for the items. Possible values are:
|
||||
* - sum: No filtering.
|
||||
* - source: Filter the feed items, limiting the result to items from a
|
||||
* single source.
|
||||
* - category: Filter the feed items by category.
|
||||
* @param $data
|
||||
* @param mixed $data
|
||||
* Feed or category data used for filtering. The type and value of $data
|
||||
* depends on $type:
|
||||
* - source: $data is an object with $data->fid identifying the feed used to
|
||||
|
@ -102,43 +102,29 @@ function aggregator_page_category_form($form, $form_state, $category) {
|
|||
* - category: $data is an array with $data['cid'] being the category id to
|
||||
* filter on.
|
||||
* The $data parameter is not used when $type is 'sum'.
|
||||
* @param int $limit
|
||||
* (optional) The number of records to return. Defaults to 20.
|
||||
*
|
||||
* @return
|
||||
* @deprecated Use \Drupal\aggregator\ItemStorageController::loadAll() for
|
||||
* loading all feed items, \Drupal\aggregator\ItemStorageController::loadByFeed()
|
||||
* for loading feed items filtered by the source feed, and \Drupal\aggregator\ItemStorageController::loadByCategory()
|
||||
* for loading feed items filtered by the feed category.
|
||||
*
|
||||
* @return \Drupal\aggregator\ItemInterface[]
|
||||
* An array of the feed items.
|
||||
*/
|
||||
function aggregator_load_feed_items($type, $data = NULL, $limit = 20) {
|
||||
$items = array();
|
||||
$storage_controller = Drupal::entityManager()->getStorageController('aggregator_item');
|
||||
switch ($type) {
|
||||
case 'sum':
|
||||
$query = db_select('aggregator_item', 'i');
|
||||
$query->join('aggregator_feed', 'f', 'i.fid = f.fid');
|
||||
$query->fields('i', array('iid'));
|
||||
break;
|
||||
return $storage_controller->loadAll($limit);
|
||||
|
||||
case 'source':
|
||||
$query = db_select('aggregator_item', 'i');
|
||||
$query
|
||||
->fields('i', array('iid'))
|
||||
->condition('i.fid', $data->id());
|
||||
break;
|
||||
return $storage_controller->loadByFeed($data->id(), $limit);
|
||||
|
||||
case 'category':
|
||||
$query = db_select('aggregator_category_item', 'c');
|
||||
$query->leftJoin('aggregator_item', 'i', 'c.iid = i.iid');
|
||||
$query->leftJoin('aggregator_feed', 'f', 'i.fid = f.fid');
|
||||
$query
|
||||
->fields('i', array('iid'))
|
||||
->condition('cid', $data->cid);
|
||||
break;
|
||||
return $storage_controller->loadByCategory($data->cid, $limit);
|
||||
}
|
||||
|
||||
$result = $query
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->limit($limit)
|
||||
->orderBy('i.timestamp', 'DESC')
|
||||
->orderBy('i.iid', 'DESC')
|
||||
->execute();
|
||||
|
||||
$items = entity_load_multiple('aggregator_item', $result->fetchCol());
|
||||
return $items;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -7,9 +7,10 @@
|
|||
|
||||
namespace Drupal\aggregator;
|
||||
|
||||
use Drupal\Core\Entity\DatabaseStorageControllerNG;
|
||||
use Drupal\aggregator\Plugin\Core\Entity\Item;
|
||||
use Drupal\Core\Entity\EntityInterface;
|
||||
use Drupal\Core\Database\Query\PagerSelectExtender;
|
||||
use Drupal\Core\Database\Query\SelectInterface;
|
||||
use Drupal\Core\Entity\DatabaseStorageControllerNG;
|
||||
|
||||
/**
|
||||
* Controller class for aggregators items.
|
||||
|
@ -112,4 +113,61 @@ class ItemStorageController extends DatabaseStorageControllerNG implements ItemS
|
|||
->execute();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadAll($limit = 20) {
|
||||
$query = $this->database->select('aggregator_item', 'i');
|
||||
$query->join('aggregator_feed', 'f', 'i.fid = f.fid');
|
||||
$query->fields('i', array('iid'));
|
||||
return $this->executeFeedItemQuery($query, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadByFeed($fid, $limit = 20) {
|
||||
$query = $this->database->select('aggregator_item', 'i');
|
||||
$query
|
||||
->fields('i', array('iid'))
|
||||
->condition('i.fid', $fid);
|
||||
return $this->executeFeedItemQuery($query, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadByCategory($cid, $limit = 20) {
|
||||
$query = $this->database->select('aggregator_category_item', 'c');
|
||||
$query->leftJoin('aggregator_item', 'i', 'c.iid = i.iid');
|
||||
$query->leftJoin('aggregator_feed', 'f', 'i.fid = f.fid');
|
||||
$query
|
||||
->fields('i', array('iid'))
|
||||
->condition('cid', $cid);
|
||||
return $this->executeFeedItemQuery($query, $limit);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to execute an item query.
|
||||
*
|
||||
* @param SelectInterface $query
|
||||
* The query to execute.
|
||||
* @param int $limit
|
||||
* (optional) The number of items to return. Defaults to 20.
|
||||
*
|
||||
* @return \Drupal\aggregator\ItemInterface[]
|
||||
* An array of the feed items.
|
||||
*/
|
||||
protected function executeFeedItemQuery(SelectInterface $query, $limit) {
|
||||
$result = $query
|
||||
->extend('Drupal\Core\Database\Query\PagerSelectExtender')
|
||||
->limit($limit)
|
||||
->orderBy('i.timestamp', 'DESC')
|
||||
->orderBy('i.iid', 'DESC')
|
||||
->execute();
|
||||
|
||||
return $this->loadMultiple($result->fetchCol());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -41,4 +41,42 @@ interface ItemStorageControllerInterface extends EntityStorageControllerInterfac
|
|||
* The storage backend should save the categories of this item.
|
||||
*/
|
||||
public function saveCategories(Item $item);
|
||||
|
||||
/**
|
||||
* Loads feed items from all feeds.
|
||||
*
|
||||
* @param int $limit
|
||||
* (optional) The number of items to return. Defaults to 20.
|
||||
*
|
||||
* @return \Drupal\aggregator\ItemInterface[]
|
||||
* An array of the feed items.
|
||||
*/
|
||||
public function loadAll($limit = 20);
|
||||
|
||||
/**
|
||||
* Loads feed items filtered by a feed.
|
||||
*
|
||||
* @param int $fid
|
||||
* The feed ID to filter by.
|
||||
* @param int $limit
|
||||
* (optional) The number of items to return. Defaults to 20.
|
||||
*
|
||||
* @return \Drupal\aggregator\ItemInterface[]
|
||||
* An array of the feed items.
|
||||
*/
|
||||
public function loadByFeed($fid, $limit = 20);
|
||||
|
||||
/**
|
||||
* Loads feed items from all feeds.
|
||||
*
|
||||
* @param int $cid
|
||||
* The category ID to filter by.
|
||||
* @param int $limit
|
||||
* (optional) The number of items to return. Defaults to 20.
|
||||
*
|
||||
* @return \Drupal\aggregator\ItemInterface[]
|
||||
* An array of the feed items.
|
||||
*/
|
||||
public function loadByCategory($cid, $limit = 20);
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue