diff --git a/core/modules/aggregator/aggregator.module b/core/modules/aggregator/aggregator.module index 416571d..6753f28 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_categorize_category_form', ); $items['aggregator/categories/%aggregator_category/configure'] = array( 'title' => 'Configure', @@ -195,11 +192,8 @@ function aggregator_menu() { ); $items['aggregator/sources/%aggregator_feed/categorize'] = array( 'title' => 'Categorize', - 'page callback' => 'drupal_get_form', - 'page arguments' => array('aggregator_page_source_form', 2), - 'access arguments' => array('administer news feeds'), + 'route_name' => 'aggregator_categorize_feed_form', 'type' => MENU_LOCAL_TASK, - 'file' => 'aggregator.pages.inc', ); $items['aggregator/sources/%aggregator_feed/configure'] = array( 'title' => 'Configure', diff --git a/core/modules/aggregator/aggregator.pages.inc b/core/modules/aggregator/aggregator.pages.inc index 0fa2bd8..20b145f 100644 --- a/core/modules/aggregator/aggregator.pages.inc +++ b/core/modules/aggregator/aggregator.pages.inc @@ -30,23 +30,6 @@ function aggregator_page_source(Feed $feed) { } /** - * Form constructor to show all items captured from a feed. - * - * @param $feed - * The feed 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_source() - * @ingroup forms - */ -function aggregator_page_source_form($form, $form_state, $feed) { - return aggregator_page_source($feed); -} - -/** * Form constructor to list items aggregated in a category. * * @param $category @@ -69,23 +52,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 @@ -144,111 +110,17 @@ function aggregator_load_feed_items($type, $data = NULL, $limit = 20) { * The rendered list of items for the feed. */ function _aggregator_page_list($items, $op, $feed_source = '') { - if (user_access('administer news feeds') && ($op == 'categorize')) { - // Get form data. - $build = aggregator_categorize_items($items, $feed_source); - } - else { - // Assemble output. - $build = array( - '#type' => 'container', - '#attributes' => array('class' => array('aggregator-wrapper')), - ); - $build['feed_source'] = is_array($feed_source) ? $feed_source : array('#markup' => $feed_source); - if ($items) { - $build['items'] = entity_view_multiple($items, 'default'); - $build['pager'] = array('#theme' => 'pager'); - } - } - - return $build; -} - -/** - * Form constructor to build the page list form. - * - * @param array $items - * An array of the feed items. - * @param string $feed_source - * (optional) The feed source URL. Defaults to an empty string. - * - * @return array - * An array of FAPI elements. - * - * @see aggregator_categorize_items_submit() - * @see theme_aggregator_categorize_items() - * @ingroup forms - */ -function aggregator_categorize_items($items, $feed_source = '') { - $form['#submit'][] = 'aggregator_categorize_items_submit'; - $form['feed_source'] = array( - '#value' => $feed_source, - ); - $categories = array(); - $done = FALSE; - - $form['items'] = array( - '#type' => 'table', - '#header' => array('', t('Categorize')), + // Assemble output. + $build = array( + '#type' => 'container', + '#attributes' => array('class' => array('aggregator-wrapper')), ); - if ($items && $form_items = entity_view_multiple($items, 'default')) { - foreach (element_children($form_items) as $iid) { - $categories_result = db_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] = check_plain($category->title); - } - if ($category->iid) { - $selected[] = $category->cid; - } - } - $done = TRUE; - $form['items'][$iid]['item'] = $form_items[$iid]; - $form['items'][$iid]['categories'] = array( - '#type' => config('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; -} - -/** - * Form submission handler for aggregator_categorize_items(). - */ -function aggregator_categorize_items_submit($form, &$form_state) { - if (!empty($form_state['values']['categories'])) { - foreach ($form_state['values']['categories'] as $iid => $selection) { - db_delete('aggregator_category_item') - ->condition('iid', $iid) - ->execute(); - $insert = db_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(); - } - } + $build['feed_source'] = is_array($feed_source) ? $feed_source : array('#markup' => $feed_source); + if ($items) { + $build['items'] = entity_view_multiple($items, 'default'); + $build['pager'] = array('#theme' => 'pager'); } - drupal_set_message(t('The categories have been saved.')); + return $build; } /** diff --git a/core/modules/aggregator/aggregator.routing.yml b/core/modules/aggregator/aggregator.routing.yml index 2c69045..76f534e 100644 --- a/core/modules/aggregator/aggregator.routing.yml +++ b/core/modules/aggregator/aggregator.routing.yml @@ -67,3 +67,18 @@ aggregator_categories: _content: '\Drupal\aggregator\Controller\AggregatorController::categories' requirements: _access_aggregator_categories: 'TRUE' + +aggregator_categorize_category_form: + pattern: '/aggregator/categories/{cid}/categorize' + defaults: + _form: '\Drupal\aggregator\Form\CategorizeCategoryForm' + requirements: + _permission: 'administer news feeds' + +aggregator_categorize_feed_form: + pattern: '/aggregator/sources/{aggregator_feed}/categorize' + defaults: + _form: '\Drupal\aggregator\Form\CategorizeFeedForm' + entity_type: 'aggregator_feed' + requirements: + _permission: 'administer news feeds' diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeCategoryForm.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeCategoryForm.php new file mode 100644 index 0000000..4bc7c9e --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeCategoryForm.php @@ -0,0 +1,30 @@ +storageController->loadByCategory($cid); + return $this->prepareFormItems($items); + } + +} diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFeedForm.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFeedForm.php new file mode 100644 index 0000000..6f29b60 --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFeedForm.php @@ -0,0 +1,32 @@ +storageController->loadByFeed($aggregator_feed->id()); + return $this->prepareFormItems($items, $aggregator_feed); + } + +} diff --git a/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFormBase.php b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFormBase.php new file mode 100644 index 0000000..59fe4a0 --- /dev/null +++ b/core/modules/aggregator/lib/Drupal/aggregator/Form/CategorizeFormBase.php @@ -0,0 +1,179 @@ +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') + ); + } + + /** + * Prepares a list of items to be displayed in a form. + * + * @param \Drupal\aggregator\ItemInterface[] $items + * An array of items. + * @param \Drupal\aggregator\FeedInterface|null $feed + * (optional) The feed we are displaying. + * + * @return array + * A form builder array. + */ + protected function prepareFormItems($items, $feed = NULL) { + + $form['feed_source'] = array( + '#value' => $feed, + ); + $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 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.')); + } + + /** + * {@inheritdoc} + */ + public function validateForm(array &$form, array &$form_state) { + } + +}