ItemStorage.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. namespace Drupal\aggregator;
  3. use Drupal\Core\Entity\Query\QueryInterface;
  4. use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
  5. /**
  6. * Controller class for aggregators items.
  7. *
  8. * This extends the Drupal\Core\Entity\Sql\SqlContentEntityStorage class, adding
  9. * required special handling for feed item entities.
  10. */
  11. class ItemStorage extends SqlContentEntityStorage implements ItemStorageInterface {
  12. /**
  13. * {@inheritdoc}
  14. */
  15. public function getItemCount(FeedInterface $feed) {
  16. $query = \Drupal::entityQuery('aggregator_item')
  17. ->condition('fid', $feed->id())
  18. ->count();
  19. return $query->execute();
  20. }
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function loadAll($limit = NULL) {
  25. $query = \Drupal::entityQuery('aggregator_item');
  26. return $this->executeFeedItemQuery($query, $limit);
  27. }
  28. /**
  29. * {@inheritdoc}
  30. */
  31. public function loadByFeed($fid, $limit = NULL) {
  32. $query = \Drupal::entityQuery('aggregator_item')
  33. ->condition('fid', $fid);
  34. return $this->executeFeedItemQuery($query, $limit);
  35. }
  36. /**
  37. * Helper method to execute an item query.
  38. *
  39. * @param \Drupal\Core\Entity\Query\QueryInterface $query
  40. * The query to execute.
  41. * @param int $limit
  42. * (optional) The number of items to return.
  43. *
  44. * @return \Drupal\aggregator\ItemInterface[]
  45. * An array of the feed items.
  46. */
  47. protected function executeFeedItemQuery(QueryInterface $query, $limit) {
  48. $query->sort('timestamp', 'DESC')
  49. ->sort('iid', 'DESC');
  50. if (!empty($limit)) {
  51. $query->pager($limit);
  52. }
  53. return $this->loadMultiple($query->execute());
  54. }
  55. }