diff --git a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php index 0922531..e13ea8d 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php @@ -125,4 +125,11 @@ public function updateItem($iid, array $cids) { } } + /** + * {@inheritdoc} + */ + public function loadAllKeyed() { + return $this->database->query('SELECT c.cid, c.title FROM {aggregator_category} c ORDER BY title')->fetchAllKeyed(); + } + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php index 0494415..63393bd 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php @@ -87,5 +87,13 @@ public function loadByItem($item_id); */ public function updateItem($iid, array $cids); + /** + * Loads all categories. + * + * @return array + * An array keyed on cid listing all available categories. + */ + public function loadAllKeyed(); + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php b/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php index d640880..0ca5bef 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php @@ -9,7 +9,10 @@ use Drupal\Component\Utility\String; use Drupal\Core\Entity\ContentEntityFormController; +use Drupal\Core\Entity\EntityStorageControllerInterface; use Drupal\Core\Language\Language; +use Drupal\aggregator\CategoryStorageControllerInterface; +use Symfony\Component\DependencyInjection\ContainerInterface; /** * Form controller for the aggregator feed edit forms. @@ -17,6 +20,43 @@ class FeedFormController extends ContentEntityFormController { /** + * The feed storage. + * + * @var \Drupal\Core\Entity\EntityStorageControllerInterface + */ + protected $feedStorageController; + + /** + * The category storage controller. + * + * @var \Drupal\aggregator\CategoryStorageControllerInterface + */ + protected $categoryStorageController; + + /** + * Constructs a FeedForm object. + * + * @param \Drupal\Core\Entity\EntityStorageControllerInterface $feed_storage + * The feed storage. + * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller + * The category storage controller. + */ + public function __construct(EntityStorageControllerInterface $feed_storage, CategoryStorageControllerInterface $category_storage_controller) { + $this->feedStorageController = $feed_storage; + $this->categoryStorageController = $category_storage_controller; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity')->getStorageController('aggregator_feed'), + $container->get('aggregator.category.storage') + ); + } + + /** * {@inheritdoc} */ public function form(array $form, array &$form_state) { @@ -58,11 +98,11 @@ public function form(array $form, array &$form_state) { // Handling of categories. $options = array(); $values = array(); - $categories = db_query('SELECT c.cid, c.title FROM {aggregator_category} c ORDER BY title'); - foreach ($categories as $category) { - $options[$category->cid] = String::checkPlain($category->title); - if (!empty($feed->categories) && in_array($category->cid, array_keys($feed->categories))) { - $values[] = $category->cid; + $categories = $this->categoryStorageController->loadAllKeyed(); + foreach ($categories as $cid => $title) { + $options[$cid] = String::checkPlain($title); + if (!empty($feed->categories) && in_array($cid, array_keys($feed->categories))) { + $values[] = $cid; } } @@ -85,13 +125,7 @@ public function form(array $form, array &$form_state) { public function validate(array $form, array &$form_state) { $feed = $this->buildEntity($form, $form_state); // Check for duplicate titles. - if ($feed->id()) { - $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $feed->label(), ':url' => $feed->url->value, ':fid' => $feed->id())); - } - else { - $result = db_query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed->label(), ':url' => $feed->url->value)); - } - + $result = $this->feedStorageController->getFeedDuplicates($feed); foreach ($result as $item) { if (strcasecmp($item->title, $feed->label()) == 0) { form_set_error('title', $this->t('A feed named %feed already exists. Enter a unique title.', array('%feed' => $feed->label()))); diff --git a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php index 17ea1e2..8f8fd71 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php @@ -7,12 +7,11 @@ namespace Drupal\aggregator; +use Drupal\aggregator\FeedInterface; use Drupal\Core\Entity\FieldableDatabaseStorageController; -use Drupal\aggregator\Entity\Feed; -use Drupal\Core\Entity\EntityInterface; /** - * Controller class for aggregators feeds. + * Controller class for aggregator's feeds. * * This extends the Drupal\Core\Entity\DatabaseStorageController class, adding * required special handling for feed entities. @@ -39,7 +38,7 @@ public function loadCategories(array $feeds) { /** * {@inheritdoc} */ - public function saveCategories(Feed $feed, array $categories) { + public function saveCategories(FeedInterface $feed, array $categories) { foreach ($categories as $cid => $value) { if ($value) { $this->database->insert('aggregator_category_feed') @@ -62,4 +61,18 @@ public function deleteCategories(array $feeds) { ->execute(); } + /** + * {@inheritdoc} + */ + public function getFeedDuplicates(FeedInterface $feed) { + if ($feed->id()) { + $query = $this->database->query("SELECT title, url FROM {aggregator_feed} WHERE (title = :title OR url = :url) AND fid <> :fid", array(':title' => $feed->label(), ':url' => $feed->url->value, ':fid' => $feed->id())); + } + else { + $query = $this->database->query("SELECT title, url FROM {aggregator_feed} WHERE title = :title OR url = :url", array(':title' => $feed->label(), ':url' => $feed->url->value)); + } + + return $query->fetchAll(); + } + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php index c991b0f..ab33938 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php @@ -7,7 +7,7 @@ namespace Drupal\aggregator; -use Drupal\aggregator\Entity\Feed; +use Drupal\aggregator\FeedInterface; use Drupal\Core\Entity\EntityStorageControllerInterface; /** @@ -18,7 +18,7 @@ /** * Loads the categories of a feed. * - * @param array $entities + * @param array $feeds * A list of feed entities keyed by feed id. Each entity will get a * categories property added. */ @@ -27,12 +27,12 @@ public function loadCategories(array $feeds); /** * Saves the categories of a feed. * - * @param Feed $feed + * @param \Drupal\aggregator\Entity\FeedInterface $feed * The feed entity. * @param array $categories * The array of categories. */ - public function saveCategories(Feed $feed, array $categories); + public function saveCategories(FeedInterface $feed, array $categories); /** * Deletes the categories of a feed. @@ -42,4 +42,15 @@ public function saveCategories(Feed $feed, array $categories); */ public function deleteCategories(array $feeds); + /** + * Provides a list of duplicate feeds. + * + * @param \Drupal\aggregator\Entity\FeedInterface $feed + * The feed entity. + * + * @return + * An array with the list of duplicated feeds. + */ + public function getFeedDuplicates(FeedInterface $feed); + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php index 5727cf6..abf1cad 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php @@ -7,15 +7,15 @@ namespace Drupal\aggregator\Form; +use Drupal\aggregator\CategoryStorageControllerInterface; use Drupal\aggregator\FeedStorageControllerInterface; use Drupal\Component\Utility\Url; -use Drupal\Core\Database\Connection; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Form\FormBase; use Symfony\Component\DependencyInjection\ContainerInterface; use Guzzle\Http\Exception\RequestException; use Guzzle\Http\Exception\BadResponseException; -use Guzzle\Http\Client; +use Guzzle\Http\ClientInterface; /** * Imports feeds from OPML. @@ -23,13 +23,6 @@ class OpmlFeedAdd extends FormBase { /** - * The database connection object. - * - * @var \Drupal\Core\Database\Connection - */ - protected $database; - - /** * The entity query factory object. * * @var \Drupal\Core\Entity\Query\QueryFactory @@ -41,32 +34,39 @@ class OpmlFeedAdd extends FormBase { * * @var \Drupal\aggregator\FeedStorageControllerInterface */ - protected $feedStorage; + protected $feedStorageController; /** * The HTTP client to fetch the feed data with. * - * @var \Guzzle\Http\Client + * @var \Guzzle\Http\ClientInterface */ protected $httpClient; /** + * The category storage controller. + * + * @var \Drupal\aggregator\CategoryStorageControllerInterface + */ + protected $categoryStorageController; + + /** * Constructs a database object. * - * @param \Drupal\Core\Database\Connection $database - * The database object. * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query object. * @param \Drupal\aggregator\FeedStorageControllerInterface $feed_storage * The feed storage. - * @param \Guzzle\Http\Client $http_client + * @param \Guzzle\Http\ClientInterface $http_client * The Guzzle HTTP client. + * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage_controller + * The category storage controller. */ - public function __construct(Connection $database, QueryFactory $query_factory, FeedStorageControllerInterface $feed_storage, Client $http_client) { - $this->database = $database; + public function __construct(QueryFactory $query_factory, FeedStorageControllerInterface $feed_storage, ClientInterface $http_client, CategoryStorageControllerInterface $category_storage_controller) { $this->queryFactory = $query_factory; - $this->feedStorage = $feed_storage; + $this->feedStorageController = $feed_storage; $this->httpClient = $http_client; + $this->categoryStorageController = $category_storage_controller; } /** @@ -74,10 +74,10 @@ public function __construct(Connection $database, QueryFactory $query_factory, F */ public static function create(ContainerInterface $container) { return new static( - $container->get('database'), $container->get('entity.query'), $container->get('entity.manager')->getStorageController('aggregator_feed'), - $container->get('http_default_client') + $container->get('http_default_client'), + $container->get('aggregator.category.storage') ); } @@ -115,7 +115,7 @@ public function buildForm(array $form, array &$form_state) { ); // Handling of categories. - $options = array_map('check_plain', $this->database->query("SELECT cid, title FROM {aggregator_category} ORDER BY title")->fetchAllKeyed()); + $options = array_map('check_plain', $this->categoryStorageController->loadAllKeyed()); if ($options) { $form['category'] = array( '#type' => 'checkboxes', @@ -192,7 +192,7 @@ public function submitForm(array &$form, array &$form_state) { $ids = $query ->condition($condition) ->execute(); - $result = $this->feedStorage->loadMultiple($ids); + $result = $this->feedStorageController->loadMultiple($ids); foreach ($result as $old) { if (strcasecmp($old->label(), $feed['title']) == 0) { drupal_set_message($this->t('A feed named %title already exists.', array('%title' => $old->label())), 'warning'); @@ -204,7 +204,7 @@ public function submitForm(array &$form, array &$form_state) { } } - $new_feed = $this->feedStorage->create(array( + $new_feed = $this->feedStorageController->create(array( 'title' => $feed['title'], 'url' => $feed['url'], 'refresh' => $form_state['values']['refresh'],