diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index 416571d..081768d 100644 --- a/core/modules/aggregator/aggregator.module +++ b/core/modules/aggregator/aggregator.module @@ -166,11 +166,8 @@ function aggregator_menu() { ); $items['aggregator/categories/%aggregator_category/categorize'] = array( 'title' => 'Categorize', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('aggregator_page_category_form', 2), - 'access arguments' => array('administer news feeds'), 'type' => MENU_LOCAL_TASK, - 'file' => 'aggregator.pages.inc', + 'route_name' => 'aggregator_page_category_form', ); $items['aggregator/categories/%aggregator_category/configure'] = array( 'title' => 'Configure', diff --git a/core/modules/aggregator/aggregator.pages.inc b/core/modules/aggregator/aggregator.pages.inc index 98dc665..1f36ca6 100644 --- a/core/modules/aggregator/aggregator.pages.inc +++ b/core/modules/aggregator/aggregator.pages.inc @@ -69,23 +69,6 @@ function aggregator_page_category($category) { } /** - * Form constructor to list items aggregated in a category. - * - * @param $category - * The category for which to list all of the aggregated items. - * - * @return string - * The rendered list of items for the feed. - * - * @see aggregator_menu() - * @see aggregator_page_category() - * @ingroup forms - */ -function aggregator_page_category_form($form, $form_state, $category) { - return aggregator_page_category($category); -} - -/** * Loads and optionally filters feed items. * * @param string $type diff --git a/core/modules/aggregator/aggregator.routing.yml b/core/modules/aggregator/aggregator.routing.yml index 2c69045..6612a2a 100644 --- a/core/modules/aggregator/aggregator.routing.yml +++ b/core/modules/aggregator/aggregator.routing.yml @@ -67,3 +67,10 @@ aggregator_categories: _content: '\Drupal\aggregator\Controller\AggregatorController::categories' requirements: _access_aggregator_categories: 'TRUE' + +aggregator_page_category_form: + pattern: '/aggregator/categories/{category}/categorize' + defaults: + _form: '\Drupal\aggregator\Form\AggregatorCategoryForm' + requirements: + _permission: 'administer news feeds' diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategoryForm.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategoryForm.php new file mode 100644 index 0000000..c98066f --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategoryForm.php @@ -0,0 +1,177 @@ +renderController = $render_controller; + $this->database = $database; + $this->config = $config; + $this->storageController = $storage_controller; + } + + /** + * {@inheritdoc} + */ + 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') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'aggregator_category_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, $category = NULL) { + $items = $this->storageController->loadByCategory($category); + $form['feed_source'] = array( + '#value' => '', + ); + $categories = array(); + $done = FALSE; + + $form['items'] = array( + '#type' => 'table', + '#header' => array('', t('Categorize')), + ); + if ($items && ($form_items = $this->renderController->viewMultiple($items, 'default'))) { + foreach (element_children($form_items) as $iid) { + $categories_result = $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' => $iid)); + + $selected = array(); + foreach ($categories_result as $category) { + if (!$done) { + $categories[$category->cid] = String::checkPlain($category->title); + } + if ($category->iid) { + $selected[] = $category->cid; + } + } + $done = TRUE; + $form['items'][$iid]['item'] = $form_items[$iid]; + $form['items'][$iid]['categories'] = array( + '#type' => $this->config->get('source.category_selector'), + '#default_value' => $selected, + '#options' => $categories, + '#size' => 10, + '#multiple' => TRUE, + '#parents' => array('categories', $iid), + ); + } + } + + $form['actions'] = array('#type' => 'actions'); + $form['actions']['submit'] = array( + '#type' => 'submit', + '#value' => t('Save categories'), + ); + $form['pager'] = array('#theme' => 'pager'); + + return $form; + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + + /** + * {@inheritdoc} + */ + 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(); + } + } + } + drupal_set_message(t('The categories have been saved.')); + } + +}