diff --git a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php index aad6ae2..7a39a98 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageController.php @@ -105,4 +105,24 @@ public function loadByItem($item_id) { return $this->database->query('SELECT c.cid, c.title, ci.iid FROM {aggregator_category} c LEFT JOIN {aggregator_category_item} ci ON c.cid = ci.cid AND ci.iid = :iid', array(':iid' => $item_id)); } + /** + * {@inheritdoc} + */ + public function updateCategoriesForItem($iid, array $cids) { + // Remove all existing category items. + $this->database->delete('aggregator_category_item') + ->condition('iid', $iid) + ->execute(); + + // Insert new category items. + if (!empty($cids)) { + $insert = $this->database->insert('aggregator_category_item') + ->fields(array('iid', 'cid')); + foreach ($cids as $cid) { + $insert->values(array('iid' => $iid, 'cid' => $cid)); + } + $insert->execute(); + } + } + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php index 90fb48f..cd52660 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/CategoryStorageControllerInterface.php @@ -77,5 +77,15 @@ public function isUnique($title, $cid = NULL); */ public function loadByItem($item_id); + /** + * Updates the categories for an aggregator item. + * + * @param int $iid + * The aggregator item ID. + * @param array $cids + * The category IDs. + */ + public function updateCategoriesForItem($iid, array $cids); + } diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php index e73f684..eaf4e9f 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategorizeFormBase.php @@ -12,7 +12,6 @@ use Drupal\aggregator\ItemStorageControllerInterface; use Drupal\Component\Utility\String; use Drupal\Core\Config\Config; -use Drupal\Core\Database\Connection; use Drupal\Core\Entity\EntityRenderControllerInterface; use Drupal\Core\Form\FormBase; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -37,13 +36,6 @@ protected $config; /** - * The database connection. - * - * @var \Drupal\Core\Database\Connection; - */ - protected $database; - - /** * The aggregator item storage controller. * * @var \Drupal\aggregator\ItemStorageControllerInterface @@ -69,16 +61,15 @@ * * @param \Drupal\Core\Entity\EntityRenderControllerInterface $aggregator_item_renderer * The item render controller. - * @param \Drupal\Core\Database\Connection $database - * The database connection. * @param \Drupal\Core\Config\Config $config * The aggregator config. * @param \Drupal\aggregator\ItemStorageControllerInterface $aggregator_item_storage * The aggregator item storage controller. + * @param \Drupal\aggregator\CategoryStorageControllerInterface $category_storage + * The category storage controller. */ - public function __construct(EntityRenderControllerInterface $aggregator_item_renderer, Connection $database, Config $config, ItemStorageControllerInterface $aggregator_item_storage, CategoryStorageControllerInterface $category_storage) { + public function __construct(EntityRenderControllerInterface $aggregator_item_renderer, Config $config, ItemStorageControllerInterface $aggregator_item_storage, CategoryStorageControllerInterface $category_storage) { $this->aggregatorItemRenderer = $aggregator_item_renderer; - $this->database = $database; $this->config = $config; $this->aggregatorItemStorage = $aggregator_item_storage; $this->categoryStorage = $category_storage; @@ -90,7 +81,6 @@ public function __construct(EntityRenderControllerInterface $aggregator_item_ren public static function create(ContainerInterface $container) { return new static( $container->get('plugin.manager.entity')->getRenderController('aggregator_item'), - $container->get('database'), $container->get('config.factory')->get('aggregator.settings'), $container->get('plugin.manager.entity')->getStorageController('aggregator_item'), $container->get('aggregator.category.storage') @@ -154,25 +144,8 @@ public function buildForm(array $form, array &$form_state, array $items = NULL) */ public function submitForm(array &$form, array &$form_state) { if (!empty($form_state['values']['categories'])) { - foreach ($form_state['values']['categories'] as $iid => $selection) { - $this->database->delete('aggregator_category_item') - ->condition('iid', $iid) - ->execute(); - $insert = $this->database->insert('aggregator_category_item') - ->fields(array('iid', 'cid')); - $has_values = FALSE; - foreach ($selection as $cid) { - if ($cid && $iid) { - $has_values = TRUE; - $insert->values(array( - 'iid' => $iid, - 'cid' => $cid, - )); - } - } - if ($has_values) { - $insert->execute(); - } + foreach ($form_state['values']['categories'] as $iid => $cids) { + $this->categoryStorage->updateCategoriesForItem($iid, $cids); } } drupal_set_message($this->t('The categories have been saved.'));