diff --git a/core/modules/forum/config/views.view.forum_topic_lists.yml b/core/modules/forum/config/views.view.forum_topic_lists.yml deleted file mode 100644 index 58826b779c7ffc9d7505eb1b4a104a9c37a687c1..0000000000000000000000000000000000000000 --- a/core/modules/forum/config/views.view.forum_topic_lists.yml +++ /dev/null @@ -1,229 +0,0 @@ -base_table: forum_index -core: 8.x -description: 'A list of new or active forum topics' -status: '1' -display: - block_1: - display_plugin: block - id: block_1 - display_title: 'Active forum topics' - position: '1' - display_options: - display_description: 'Displays a list of active forum topics' - sorts: - last_comment_timestamp: - id: last_comment_timestamp - table: forum_index - field: last_comment_timestamp - relationship: none - group_type: group - admin_label: '' - order: DESC - exposed: '0' - expose: - label: '' - granularity: second - plugin_id: date - defaults: - sorts: '0' - title: '0' - block_description: 'Active forum topics' - title: 'Active forum topics' - link_url: /forum - default: - display_plugin: default - id: default - display_title: Master - position: '1' - display_options: - access: - type: none - options: { } - cache: - type: none - options: { } - query: - type: views_query - options: - disable_sql_rewrite: '0' - distinct: '0' - slave: '0' - query_comment: '' - query_tags: { } - exposed_form: - type: basic - options: - submit_button: Apply - reset_button: '0' - reset_button_label: Reset - exposed_sorts_label: 'Sort by' - expose_sort_order: '1' - sort_asc_label: Asc - sort_desc_label: Desc - pager: - type: some - options: - items_per_page: '5' - offset: '0' - style: - type: html_list - options: - grouping: { } - row_class: '' - default_row_class: '1' - row_class_special: '1' - type: ul - wrapper_class: item-list - class: '' - row: - type: fields - fields: - nid: - id: nid - table: forum_index - field: nid - relationship: none - group_type: group - admin_label: '' - label: '' - exclude: '1' - alter: - alter_text: '0' - text: '' - make_link: '0' - path: '' - absolute: '0' - external: '0' - replace_spaces: '0' - path_case: none - trim_whitespace: '0' - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: '0' - max_length: '' - word_boundary: '1' - ellipsis: '1' - more_link: '0' - more_link_text: '' - more_link_path: '' - strip_tags: '0' - trim: '0' - preserve_tags: '' - html: '0' - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: '0' - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: '1' - empty: '' - hide_empty: '0' - empty_zero: '0' - hide_alter_empty: '1' - set_precision: '0' - precision: '0' - decimal: . - separator: ',' - format_plural: '0' - format_plural_singular: '1' - format_plural_plural: '@count' - prefix: '' - suffix: '' - title: - id: title - table: forum_index - field: title - relationship: none - group_type: group - admin_label: '' - label: '' - exclude: '0' - alter: - alter_text: '0' - text: '' - make_link: '1' - path: 'node/[nid]' - absolute: '0' - external: '0' - replace_spaces: '0' - path_case: none - trim_whitespace: '0' - alt: '' - rel: '' - link_class: '' - prefix: '' - suffix: '' - target: '' - nl2br: '0' - max_length: '' - word_boundary: '1' - ellipsis: '1' - more_link: '0' - more_link_text: '' - more_link_path: '' - strip_tags: '0' - trim: '0' - preserve_tags: '' - html: '0' - element_type: '' - element_class: '' - element_label_type: '' - element_label_class: '' - element_label_colon: '0' - element_wrapper_type: '' - element_wrapper_class: '' - element_default_classes: '1' - empty: '' - hide_empty: '0' - empty_zero: '0' - hide_alter_empty: '1' - plugin_id: standard - filters: { } - sorts: { } - title: 'New forum Topics' - header: { } - footer: { } - empty: { } - relationships: { } - arguments: { } - use_more: '1' - use_more_always: '1' - use_more_text: More - link_display: custom_url - block_2: - display_plugin: block - id: block_2 - display_title: 'New forum topics' - position: '1' - display_options: - display_description: 'Displays a list of new forum topics' - sorts: - created: - id: created - table: forum_index - field: created - relationship: none - group_type: group - admin_label: '' - order: DESC - exposed: '0' - expose: - label: '' - granularity: second - plugin_id: date - defaults: - sorts: '0' - block_description: 'New forum topics' - link_url: /forum -label: 'Forum Topic Lists' -module: views -id: forum_topic_lists -tag: 'default' -uuid: e9169074-fc80-4676-9708-8b0ef31c4c69 -langcode: en diff --git a/core/modules/forum/forum.module b/core/modules/forum/forum.module index 92ea2a2979b849f1115225732b2df454e8b133a3..95ebe76ec1b4c1afa2f69a4660a1c95b22e13d62 100644 --- a/core/modules/forum/forum.module +++ b/core/modules/forum/forum.module @@ -552,6 +552,23 @@ function forum_form_node_form_alter(&$form, &$form_state, $form_id) { } } +/** + * Render API callback: Lists nodes based on the element's #query property. + * + * This function can be used as a #pre_render callback. + * + * @see \Drupal\forum\Plugin\block\block\NewTopicsBlock::build() + * @see \Drupal\forum\Plugin\block\block\ActiveTopicsBlock::build() + */ +function forum_block_view_pre_render($elements) { + $result = $elements['#query']->execute(); + if ($node_title_list = node_title_list($result)) { + $elements['forum_list'] = $node_title_list; + $elements['forum_more'] = array('#theme' => 'more_link', '#url' => 'forum', '#title' => t('Read the latest forum topics.')); + } + return $elements; +} + /** * Implements hook_preprocess_HOOK() for block templates. */ diff --git a/core/modules/forum/forum.views.inc b/core/modules/forum/forum.views.inc index 11b706f45ba2aa712ed647baf30004e688fae572..fa7174f0b29a45591425b908fedcfb85b3dc07fa 100644 --- a/core/modules/forum/forum.views.inc +++ b/core/modules/forum/forum.views.inc @@ -15,7 +15,6 @@ function forum_views_data() { 'field' => 'nid', 'title' => t('Forum content'), 'access query tag' => 'node_access', - 'query metadata' => array('base_table' => 'forum_index'), ); $data['forum_index']['nid'] = array( diff --git a/core/modules/forum/lib/Drupal/forum/Plugin/Block/ActiveTopicsBlock.php b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ActiveTopicsBlock.php new file mode 100644 index 0000000000000000000000000000000000000000..3c7aa2a60b6ee5109f8cd74d92aaac9e756c1d89 --- /dev/null +++ b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ActiveTopicsBlock.php @@ -0,0 +1,40 @@ +fields('f') + ->addTag('node_access') + ->addMetaData('base_table', 'forum_index') + ->orderBy('f.last_comment_timestamp', 'DESC') + ->range(0, $this->configuration['block_count']); + + return array( + drupal_render_cache_by_query($query, 'forum_block_view'), + ); + } + +} diff --git a/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php new file mode 100644 index 0000000000000000000000000000000000000000..c2ad994208edee90783fdeecd8cdb0965279eaf5 --- /dev/null +++ b/core/modules/forum/lib/Drupal/forum/Plugin/Block/ForumBlockBase.php @@ -0,0 +1,58 @@ + DRUPAL_CACHE_CUSTOM, + 'properties' => array( + 'administrative' => TRUE, + ), + 'block_count' => 5, + ); + } + + /** + * {@inheritdoc} + */ + public function access(AccountInterface $account) { + return $account->hasPermission('access content'); + } + + /** + * Overrides \Drupal\block\BlockBase::blockForm(). + */ + public function blockForm($form, &$form_state) { + $form['block_count'] = array( + '#type' => 'select', + '#title' => t('Number of topics'), + '#default_value' => $this->configuration['block_count'], + '#options' => drupal_map_assoc(range(2, 20)), + ); + return $form; + } + + /** + * Overrides \Drupal\block\BlockBase::blockSubmit(). + */ + public function blockSubmit($form, &$form_state) { + $this->configuration['block_count'] = $form_state['values']['block_count']; + } + +} diff --git a/core/modules/forum/lib/Drupal/forum/Plugin/Block/NewTopicsBlock.php b/core/modules/forum/lib/Drupal/forum/Plugin/Block/NewTopicsBlock.php new file mode 100644 index 0000000000000000000000000000000000000000..43c044273304246eb31c84eaca888de8656ce5ad --- /dev/null +++ b/core/modules/forum/lib/Drupal/forum/Plugin/Block/NewTopicsBlock.php @@ -0,0 +1,40 @@ +fields('f') + ->addTag('node_access') + ->addMetaData('base_table', 'forum_index') + ->orderBy('f.created', 'DESC') + ->range(0, $this->configuration['block_count']); + + return array( + drupal_render_cache_by_query($query, 'forum_block_view'), + ); + } + +} diff --git a/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php b/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php index 1bb4509858b8dcfde8b19813c03b0e68cc769d27..ab8feb91a88f9ac666cc9441e177bb9ff28a07b9 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumBlockTest.php @@ -20,7 +20,7 @@ class ForumBlockTest extends WebTestBase { * * @var array */ - public static $modules = array('forum', 'block', 'views'); + public static $modules = array('forum', 'block'); /** * A user with various administrative privileges. @@ -59,7 +59,7 @@ public function testNewForumTopicsBlock() { $topics = $this->createForumTopics(); // Enable the new forum topics block. - $block = $this->drupalPlaceBlock('views_block:forum_topic_lists-block_2'); + $block = $this->drupalPlaceBlock('forum_new_block'); $this->drupalGet(''); $this->assertLink(t('More'), 0, 'New forum topics block has a "more"-link.'); @@ -69,6 +69,22 @@ public function testNewForumTopicsBlock() { foreach ($topics as $topic) { $this->assertLink($topic, 0, format_string('Forum topic @topic found in the "New forum topics" block.', array('@topic' => $topic))); } + + // Configure the new forum topics block to only show 2 topics. + $block->getPlugin()->setConfigurationValue('block_count', 2); + $block->save(); + + $this->drupalGet(''); + // We expect only the 2 most recent forum topics to appear in the "New forum + // topics" block. + for ($index = 0; $index < 5; $index++) { + if (in_array($index, array(3, 4))) { + $this->assertLink($topics[$index], 0, format_string('Forum topic @topic found in the "New forum topics" block.', array('@topic' => $topics[$index]))); + } + else { + $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "New forum topics" block.', array('@topic' => $topics[$index]))); + } + } } /** @@ -99,7 +115,7 @@ public function testActiveForumTopicsBlock() { } // Enable the block. - $block = $this->drupalPlaceBlock('views_block:forum_topic_lists-block_1'); + $block = $this->drupalPlaceBlock('forum_active_block'); $this->drupalGet(''); $this->assertLink(t('More'), 0, 'Active forum topics block has a "more"-link.'); $this->assertLinkByHref('forum', 0, 'Active forum topics block has a "more"-link.'); @@ -115,6 +131,23 @@ public function testActiveForumTopicsBlock() { $this->assertNoText($topics[$index], format_string('Forum topic @topic not found in the "Active forum topics" block.', array('@topic' => $topics[$index]))); } } + + // Configure the active forum block to only show 2 topics. + $block->getPlugin()->setConfigurationValue('block_count', 2); + $block->save(); + + $this->drupalGet(''); + + // We expect only the 2 forum topics with most recent comments to appear in + // the "Active forum topics" block. + for ($index = 0; $index < 10; $index++) { + if (in_array($index, array(3, 4))) { + $this->assertLink($topics[$index], 0, 'Forum topic found in the "Active forum topics" block.'); + } + else { + $this->assertNoText($topics[$index], 'Forum topic not found in the "Active forum topics" block.'); + } + } } /** diff --git a/core/modules/forum/lib/Drupal/forum/Tests/ForumNodeAccessTest.php b/core/modules/forum/lib/Drupal/forum/Tests/ForumNodeAccessTest.php index 146c69f4e33fd55219e18735992dd1ba2465828a..99f8b259599e8449b4228b4457b735100dded0d1 100644 --- a/core/modules/forum/lib/Drupal/forum/Tests/ForumNodeAccessTest.php +++ b/core/modules/forum/lib/Drupal/forum/Tests/ForumNodeAccessTest.php @@ -19,7 +19,7 @@ class ForumNodeAccessTest extends WebTestBase { * * @var array */ - public static $modules = array('node', 'comment', 'forum', 'taxonomy', 'tracker', 'node_access_test', 'block', 'views'); + public static $modules = array('node', 'comment', 'forum', 'taxonomy', 'tracker', 'node_access_test', 'block'); public static function getInfo() { return array( @@ -72,8 +72,8 @@ function testForumNodeAccess() { // Enable the new and active forum blocks. - $this->drupalPlaceBlock('views_block:forum_topic_lists-block_1'); - $this->drupalPlaceBlock('views_block:forum_topic_lists-block_2'); + $this->drupalPlaceBlock('forum_active_block'); + $this->drupalPlaceBlock('forum_new_block'); // Test for $access_user. $this->drupalLogin($access_user); diff --git a/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php index a664fab9062a5225b46573c7f5de32b061db8299..820ec66820a55da40bccd5954acc3337c451a567 100644 --- a/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php +++ b/core/modules/views/lib/Drupal/views/Plugin/views/query/Sql.php @@ -1376,12 +1376,6 @@ function execute(ViewExecutable $view) { $query->addTag($access_tag); $count_query->addTag($access_tag); } - - if (isset($base_table_data['table']['base']['query metadata'])) { - foreach ($base_table_data['table']['base']['query metadata'] as $key => $value) - $query->addMetaData($key, $value); - $count_query->addMetaData($key, $value); - } } if ($query) {