diff --git a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php index 299113c..c693e36 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php @@ -98,4 +98,11 @@ public function isUnique($title, $cid = NULL) { return (empty($rows)); } + /** + * {@inheritdoc} + */ + public function loadAllCategories() { + 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 a4ab4db..ecfe0f2 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php @@ -66,5 +66,10 @@ public function delete($cid); */ public function isUnique($title, $cid = NULL); + /** + * Loads all categories. + */ + public function loadAllCategories(); + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php b/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php index ef25376..58efa59 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/FeedFormController.php @@ -7,13 +7,52 @@ namespace Drupal\aggregator; +use Drupal\Core\Entity\EntityFormControllerInterface; use Drupal\Core\Entity\EntityFormControllerNG; +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. */ -class FeedFormController extends EntityFormControllerNG { +class FeedFormController extends EntityFormControllerNG implements EntityFormControllerInterface { + + /** + * The feed storage. + * + * @var \Drupal\Core\Entity\EntityStorageControllerInterface + */ + protected $feedStorage; + + /** + * 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->feedStorage = $feed_storage; + $this->categoryStorageController = $category_storage_controller; + } + + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity')->getStorageController('aggregator_feed'), + $container->get('aggregator.category.storage') + ); + } /** * Overrides Drupal\Core\Entity\EntityFormController::form(). @@ -63,11 +102,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] = check_plain($category->title); - if (!empty($feed->categories) && in_array($category->cid, array_keys($feed->categories))) { - $values[] = $category->cid; + $categories = $this->categoryStorageController->loadAllCategories(); + foreach ($categories as $cid => $title) { + $options[$cid] = check_plain($title); + if (!empty($feed->categories) && in_array($cid, array_keys($feed->categories))) { + $values[] = $cid; } } @@ -90,13 +129,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->feedStorage->getFeedDuplicates($feed); foreach ($result as $item) { if (strcasecmp($item->title, $feed->label()) == 0) { form_set_error('title', 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 7b6be0a..6ced8ba 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageController.php @@ -8,11 +8,10 @@ namespace Drupal\aggregator; use Drupal\Core\Entity\DatabaseStorageControllerNG; -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. @@ -117,7 +116,7 @@ public function loadCategories(array $feeds) { /** * {@inheritdoc} */ - public function saveCategories(Feed $feed, array $categories) { + public function saveCategories(EntityInterface $feed, array $categories) { foreach ($categories as $cid => $value) { if ($value) { $this->database->insert('aggregator_category_feed') @@ -140,4 +139,18 @@ public function deleteCategories(array $feeds) { ->execute(); } + /** + * {@inheritdoc} + */ + public function getFeedDuplicates(EntityInterface $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; + } + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php b/core/modules/aggregator/lib/Drupal/aggregator/FeedStorageControllerInterface.php index c991b0f..cd8162c 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\Core\Entity\EntityInterface; 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\Core\Entity\EntityInterface $feed * The feed entity. * @param array $categories * The array of categories. */ - public function saveCategories(Feed $feed, array $categories); + public function saveCategories(EntityInterface $feed, array $categories); /** * Deletes the categories of a feed. @@ -42,4 +42,11 @@ public function saveCategories(Feed $feed, array $categories); */ public function deleteCategories(array $feeds); + /** + * Provides a list of duplicate feeds. + * + * @param \Drupal\Core\Entity\EntityInterface $feed + * The feed entity. + */ + public function getFeedDuplicates(EntityInterface $feed); } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php index 0959fb0..0048bbd 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/OpmlFeedAdd.php @@ -12,6 +12,7 @@ use Drupal\Core\Entity\EntityManager; use Drupal\Core\Entity\Query\QueryFactory; use Drupal\Core\Form\FormInterface; +use Drupal\aggregator\CategoryStorageControllerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Guzzle\Http\Exception\RequestException; use Guzzle\Http\Exception\BadResponseException; @@ -51,22 +52,32 @@ class OpmlFeedAdd implements ControllerInterface, FormInterface { protected $httpClient; /** + * The category storage controller. + * + * @var \Drupal\aggregator\CategoryStorageControllerInterface + */ + protected $categoryStorageController; + + /** * Constructs a database object. * - * @param \Drupal\Core\Database\Connection; $database + * @param \Drupal\Core\Database\Connection $database * The database object. * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query object. * @param \Drupal\Core\Entity\EntityManager $entity_manager * The entity manager. - * @param \Guzzle\Http\Client + * @param \Guzzle\Http\Client $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, EntityManager $entity_manager, Client $http_client) { + public function __construct(Connection $database, QueryFactory $query_factory, EntityManager $entity_manager, Client $http_client, CategoryStorageControllerInterface $category_storage_controller) { $this->database = $database; $this->queryFactory = $query_factory; $this->entityManager = $entity_manager; $this->httpClient = $http_client; + $this->categoryStorageController = $category_storage_controller; } /** @@ -77,7 +88,8 @@ public static function create(ContainerInterface $container) { $container->get('database'), $container->get('entity.query'), $container->get('plugin.manager.entity'), - $container->get('http_default_client') + $container->get('http_default_client'), + $container->get('aggregator.category.storage') ); } @@ -122,7 +134,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->loadAllCategories()); if ($options) { $form['category'] = array( '#type' => 'checkboxes',