Mercurial > hg > cmmr2012-drupal-site
diff core/modules/aggregator/src/ItemStorage.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/modules/aggregator/src/ItemStorage.php Thu Jul 05 14:24:15 2018 +0000 @@ -0,0 +1,65 @@ +<?php + +namespace Drupal\aggregator; + +use Drupal\Core\Entity\Query\QueryInterface; +use Drupal\Core\Entity\Sql\SqlContentEntityStorage; + +/** + * Controller class for aggregators items. + * + * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class, adding + * required special handling for feed item entities. + */ +class ItemStorage extends SqlContentEntityStorage implements ItemStorageInterface { + + /** + * {@inheritdoc} + */ + public function getItemCount(FeedInterface $feed) { + $query = \Drupal::entityQuery('aggregator_item') + ->condition('fid', $feed->id()) + ->count(); + + return $query->execute(); + } + + /** + * {@inheritdoc} + */ + public function loadAll($limit = NULL) { + $query = \Drupal::entityQuery('aggregator_item'); + return $this->executeFeedItemQuery($query, $limit); + } + + /** + * {@inheritdoc} + */ + public function loadByFeed($fid, $limit = NULL) { + $query = \Drupal::entityQuery('aggregator_item') + ->condition('fid', $fid); + return $this->executeFeedItemQuery($query, $limit); + } + + /** + * Helper method to execute an item query. + * + * @param \Drupal\Core\Entity\Query\QueryInterface $query + * The query to execute. + * @param int $limit + * (optional) The number of items to return. + * + * @return \Drupal\aggregator\ItemInterface[] + * An array of the feed items. + */ + protected function executeFeedItemQuery(QueryInterface $query, $limit) { + $query->sort('timestamp', 'DESC') + ->sort('iid', 'DESC'); + if (!empty($limit)) { + $query->pager($limit); + } + + return $this->loadMultiple($query->execute()); + } + +}