diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index 416571d..df061d2 100644 --- a/core/modules/aggregator/aggregator.module +++ b/core/modules/aggregator/aggregator.module @@ -155,10 +155,7 @@ function aggregator_menu() { $items['aggregator/categories/%aggregator_category'] = array( 'title callback' => '_aggregator_category_title', 'title arguments' => array(2), - 'page callback' => 'aggregator_page_category', - 'page arguments' => array(2), - 'access arguments' => array('access news feeds'), - 'file' => 'aggregator.pages.inc', + 'route_name' => 'aggregator_page_category', ); $items['aggregator/categories/%aggregator_category/view'] = array( 'title' => 'View', @@ -166,11 +163,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', @@ -242,6 +236,8 @@ function aggregator_menu() { * A string with the aggregator category title. */ function _aggregator_category_title($category) { + // @todo improve how the $category is handled, should be an object already + $category = aggregator_category_load($category); return $category->title; } diff --git a/core/modules/aggregator/aggregator.pages.inc b/core/modules/aggregator/aggregator.pages.inc index ca8ecad..69f3034 100644 --- a/core/modules/aggregator/aggregator.pages.inc +++ b/core/modules/aggregator/aggregator.pages.inc @@ -47,45 +47,6 @@ function aggregator_page_source_form($form, $form_state, $feed) { } /** - * 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() - * @ingroup forms - */ -function aggregator_page_category($category) { - drupal_add_feed('aggregator/rss/' . $category->cid, config('system.site')->get('name') . ' ' . t('aggregator - @title', array('@title' => $category->title))); - - // It is safe to include the cid in the query because it's loaded from the - // database by aggregator_category_load(). - $items = aggregator_load_feed_items('category', $category); - - return _aggregator_page_list($items, arg(3)); -} - -/** - * 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 $type diff --git a/core/modules/aggregator/aggregator.routing.yml b/core/modules/aggregator/aggregator.routing.yml index 2c69045..a9764c1 100644 --- a/core/modules/aggregator/aggregator.routing.yml +++ b/core/modules/aggregator/aggregator.routing.yml @@ -67,3 +67,17 @@ aggregator_categories: _content: '\Drupal\aggregator\Controller\AggregatorController::categories' requirements: _access_aggregator_categories: 'TRUE' + +aggregator_page_category: + pattern: '/aggregator/categories/{category}' + defaults: + _content: '\Drupal\aggregator\Controller\AggregatorController::pageCategory' + requirements: + _permission: 'access news feeds' + +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/Controller/AggregatorController.php b/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php index a8b1d80..bc2705f 100644 --- a/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php +++ b/core/modules/aggregator/lib/Drupal/aggregator/Controller/AggregatorController.php @@ -265,8 +265,7 @@ public function categories() { public function pageLast() { drupal_add_feed('aggregator/rss', $this->configFactory->get('system.site')->get('name') . ' ' . t('aggregator')); - // @todo Refactor this function once after all controller conversions are - // done. + // @todo Refactor this once all controller conversions are complete. $this->moduleHandler->loadInclude('aggregator', 'inc', 'aggregator.pages'); $items = aggregator_load_feed_items('sum'); @@ -323,4 +322,31 @@ public function sources() { return $build; } + /** + * 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() + * @ingroup forms + */ + public function pageCategory($category) { + // @todo Refactor this once all controller conversions are complete. + $this->moduleHandler->loadInclude('aggregator', 'inc', 'aggregator.pages'); + + // @todo improve how the $category is handled, should be an object already + $category = aggregator_category_load($category); + + drupal_add_feed('aggregator/rss/' . $category->cid, config('system.site')->get('name') . ' ' . t('aggregator - @title', array('@title' => $category->title))); + + // It is safe to include the cid in the query because it's loaded from the + // database by aggregator_category_load(). + $items = aggregator_load_feed_items('category', $category); + + return _aggregator_page_list($items, arg(3)); + } } 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..51b66f7 --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/AggregatorCategoryForm.php @@ -0,0 +1,182 @@ +entityManager = $entity_manager; + $this->database = $database; + $this->configFactory = $config_factory; + $this->moduleHandler = $module_handler; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.entity'), + $container->get('database'), + $container->get('config.factory'), + $container->get('module_handler') + ); + } + + /** + * {@inheritdoc} + */ + public function getFormID() { + return 'aggregator_category_form'; + } + + /** + * {@inheritdoc} + */ + public function buildForm(array $form, array &$form_state, $category = NULL) { + $this->moduleHandler->loadInclude('aggregator', 'inc', 'aggregator.pages'); + // @todo Remove once categories are taxonomy terms - see + // https://drupal.org/node/15266. + $category = aggregator_category_load($category); + // @todo Remove once aggregator_load_feed_items() is a method on the + // \Drupal\aggregator\ItemStorageController, see + // https://drupal.org/node/2043581 + $items = aggregator_load_feed_items('category', $category); + $form['feed_source'] = array( + '#value' => '', + ); + $categories = array(); + $done = FALSE; + + $form['items'] = array( + '#type' => 'table', + '#header' => array('', t('Categorize')), + ); + if ($items && ($form_items = $this->entityManager->getRenderController('aggregator_item')->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->configFactory->get('aggregator.settings')->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.')); + } + +}