Commit 4462e204 authored by webchick's avatar webchick

Issue #1938062 by dawehner, tstoeckler, slashrsm, tim.plunkett, olli, pcambra,...

Issue #1938062 by dawehner, tstoeckler, slashrsm, tim.plunkett, olli, pcambra, YesCT, damiankloip: Convert the recent_comments block to a view.
parent be70033e
......@@ -701,6 +701,13 @@ function comment_update_8008(&$sandbox) {
db_drop_field('node_revision', 'comment');
}
/**
* Remove the comment_block_count variable.
*/
function comment_update_8009() {
variable_del('comment_block_count');
}
/**
* @} End of "addtogroup updates-7.x-to-8.x".
* The next series of updates should start at 9000.
......
......@@ -163,9 +163,6 @@ function comment_field_extra_fields() {
*/
function comment_theme() {
return array(
'comment_block' => array(
'variables' => array('number' => NULL),
),
'comment_preview' => array(
'variables' => array('comment' => NULL),
),
......@@ -419,30 +416,6 @@ function comment_new_page_count($num_comments, $new_replies, EntityInterface $en
return $pagenum;
}
/**
* Returns HTML for a list of recent comments.
*
* @ingroup themeable
*/
function theme_comment_block($variables) {
$items = array();
$number = $variables['number'];
foreach (comment_get_recent($number) as $comment) {
$items[] = l($comment->subject, 'comment/' . $comment->cid, array('fragment' => 'comment-' . $comment->cid)) . '&nbsp;<span>' . t('@time ago', array('@time' => format_interval(REQUEST_TIME - $comment->changed))) . '</span>';
}
if ($items) {
$item_list = array(
'#theme' => 'item_list',
'#items' => $items,
);
return drupal_render($item_list);
}
else {
return t('No comments available.');
}
}
/**
* Implements hook_entity_view().
*/
......
base_field: cid
base_table: comment
core: '8'
core: 8.x
description: 'Recent comments.'
status: false
status: true
display:
block_1:
display_plugin: block
id: block_1
display_title: Block
position: 0
display_options:
block_description: 'Recent comments'
block_category: Lists (Views)
default:
display_plugin: default
id: default
display_title: Master
display_plugin: default
position: 1
display_options:
query:
type: views_query
options:
query_comment: false
disable_sql_rewrite: false
distinct: false
slave: false
query_tags: { }
title: 'Recent comments'
use_more: '1'
access:
type: none
options: { }
type: perm
options:
perm: 'access comments'
cache:
type: none
options: { }
query:
type: views_query
exposed_form:
type: basic
options:
submit_button: Apply
reset_button: false
reset_button_label: Reset
exposed_sorts_label: 'Sort by'
expose_sort_order: true
sort_asc_label: Asc
sort_desc_label: Desc
pager:
type: some
options:
items_per_page: 5
items_per_page: 10
offset: 0
style:
type: html_list
options:
grouping: { }
row_class: ''
default_row_class: true
row_class_special: true
type: ul
wrapper_class: item-list
class: ''
row:
type: fields
options:
default_field_elements: true
inline:
subject: subject
changed: changed
separator: ' '
hide_empty: false
relationships:
node:
field: node
id: node
required: false
table: comment
required: true
plugin_id: standard
relationship: none
group_type: group
admin_label: Content
provider: views
fields:
subject:
id: subject
table: comment
field: subject
label: ''
link_to_comment: '1'
plugin_id: comment
relationship: none
plugin_id: comment
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
......@@ -82,8 +88,8 @@ display:
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
word_boundary: false
ellipsis: false
more_link: false
more_link_text: ''
more_link_path: ''
......@@ -95,7 +101,7 @@ display:
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
......@@ -103,18 +109,17 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_node: '0'
provider: comment
timestamp:
id: timestamp
link_to_comment: true
link_to_node: false
changed:
id: changed
table: comment
field: changed
label: ''
date_format: 'time ago'
plugin_id: date
relationship: none
plugin_id: date
group_type: group
admin_label: ''
label: ''
exclude: false
alter:
alter_text: false
......@@ -147,7 +152,7 @@ display:
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_label_colon: false
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
......@@ -155,312 +160,71 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
date_format: 'time ago'
custom_date_format: ''
timezone: ''
provider: views
filters:
status:
value: true
table: comment
field: status
id: status
plugin_id: boolean
expose:
operator: false
group: 1
status_node:
value: true
table: node_field_data
field: status
relationship: node
id: status_node
plugin_id: boolean
expose:
operator: false
group: 1
sorts:
changed:
id: changed
created:
id: created
table: comment
field: changed
order: DESC
plugin_id: date
field: created
relationship: none
group_type: group
admin_label: ''
order: DESC
exposed: false
expose:
label: ''
granularity: second
provider: views
filters:
status_extra:
id: status_extra
table: node_field_data
field: status_extra
relationship: node
group: '0'
plugin_id: node_status
provider: node
style:
type: html_list
options:
grouping: { }
row_class: ''
default_row_class: true
row_class_special: true
uses_fields: false
type: ul
class: ''
wrapper_class: item-list
row:
type: fields
options:
inline: { }
separator: ''
hide_empty: false
default_field_elements: '1'
header: { }
footer: { }
empty: { }
arguments: { }
page_1:
id: page_1
display_title: Page
display_plugin: page
position: 2
display_options:
query:
type: views_query
options: { }
defaults:
style_plugin: '0'
style_options: '0'
row_plugin: '0'
row_options: '0'
fields: false
fields:
title:
id: title
table: node_field_data
field: title
relationship: node
label: 'Reply to'
link_to_node: '1'
plugin_id: node
group_type: group
admin_label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
provider: node
timestamp:
id: timestamp
table: comment
field: changed
label: ''
date_format: 'time ago'
plugin_id: date
relationship: none
group_type: group
admin_label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
custom_date_format: ''
timezone: ''
provider: views
subject:
id: subject
cid:
id: cid
table: comment
field: subject
label: ''
link_to_comment: '1'
plugin_id: comment
field: cid
relationship: none
group_type: group
admin_label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_node: '0'
provider: comment
comment:
id: comment
table: comment__comment_body
field: comment_body
label: ''
plugin_id: field
order: DESC
exposed: false
expose:
label: ''
plugin_id: standard
title: 'Recent comments'
empty:
area_text_custom:
id: area_text_custom
table: views
field: area_text_custom
relationship: none
group_type: group
admin_label: ''
exclude: false
alter:
alter_text: false
text: ''
make_link: false
path: ''
absolute: false
external: false
replace_spaces: false
path_case: none
trim_whitespace: false
alt: ''
rel: ''
link_class: ''
prefix: ''
suffix: ''
target: ''
nl2br: false
max_length: ''
word_boundary: true
ellipsis: true
more_link: false
more_link_text: ''
more_link_path: ''
strip_tags: false
trim: false
preserve_tags: ''
html: false
element_type: ''
element_class: ''
element_label_type: ''
element_label_class: ''
element_label_colon: true
element_wrapper_type: ''
element_wrapper_class: ''
element_default_classes: true
empty: ''
hide_empty: false
empty_zero: false
hide_alter_empty: true
click_sort_column: value
type: text_default
settings: { }
group_column: value
group_columns: { }
group_rows: '1'
delta_limit: all
delta_offset: '0'
delta_reversed: '0'
delta_first_last: '0'
multi_type: separator
separator: ', '
field_api_classes: '0'
provider: field
path: comments/recent
style:
type: html_list
row:
type: fields
options:
inline:
title: title
timestamp: timestamp
separator: '&nbsp;'
block_1:
id: block_1
display_title: Block
display_plugin: block
position: 3
display_options:
query:
type: views_query
options: { }
label: ''
empty: true
content: 'No comments available.'
tokenize: false
plugin_id: text_custom
label: 'Recent comments'
module: comment
module: views
id: comments_recent
tag: default
uuid: 67212880-6a63-453b-a902-2d13580f7d1c
langcode: en
uuid: 67212880-6a63-453b-a902-2d13580f7d1c
<?php
/**
* @file
* Contains \Drupal\comment\Plugin\Block\RecentCommentsBlock.
*/
namespace Drupal\comment\Plugin\Block;
use Drupal\block\BlockBase;
use Drupal\block\Annotation\Block;
use Drupal\Core\Annotation\Translation;
use Drupal\Core\Session\AccountInterface;
/**
* Provides a 'Recent comments' block.
*
* @Block(
* id = "recent_comments",
* admin_label = @Translation("Recent comments"),
* category = @Translation("Lists (Views)")
* )
*/
class RecentCommentsBlock extends BlockBase {
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
return array(
'block_count' => 10,
);
}
/**
* {@inheritdoc}
*/
public function access(AccountInterface $account) {
return $account->hasPermission('access comments');
}
/**
* Overrides \Drupal\block\BlockBase::blockForm().
*/
public function blockForm($form, &$form_state) {
$form['block_count'] = array(
'#type' => 'select',
'#title' => t('Number of recent comments'),
'#default_value' => $this->configuration['block_count'],
'#options' => drupal_map_assoc(array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 25, 30)),
);
return $form;
}
/**
* Overrides \Drupal\block\BlockBase::blockSubmit().
*/
public function blockSubmit($form, &$form_state) {
$this->configuration['block_count'] = $form_state['values']['block_count'];
}
/**
* {@inheritdoc}
*/
public function build() {
return array(
'#theme' => 'comment_block',
'#number' => $this->configuration['block_count'],
);
}
}
......@@ -6,6 +6,7 @@
*/
namespace Drupal\comment\Tests;
use Drupal\Component\Utility\String;
/**
* Tests the Comment module blocks.
......@@ -17,7 +18,7 @@ class CommentBlockTest extends CommentTestBase {
*
* @var array
*/
public static $modules = array('block');
public static $modules = array('block', 'views');
function setUp() {
parent::setUp();
......@@ -46,12 +47,18 @@ public static function getInfo() {
*/
function testRecentCommentBlock() {
$this->drupalLogin($this->admin_user);
$block = $this->drupalPlaceBlock('recent_comments', array('block_count' => 2));
// Add some test comments, one without a subject.
$comment1 = $this->postComment($this->node, $this->randomName(), $this->randomName());
$comment2 = $this->postComment($this->node, $this->randomName(), $this->randomName());
$comment3 = $this->postComment($this->node, $this->randomName());
$block = $this->drupalPlaceBlock('views_block:comments_recent-block_1');
// Add some test comments, with and without subjects. Because the 10 newest
// comments should be shown by the block, we create 11 to test that behavior
// below.
$timestamp = REQUEST_TIME;
for ($i = 0; $i < 11; ++$i) {
$subject = ($i % 2) ? $this->randomName() : '';
$comments[$i] = $this->postComment($this->node, $this->randomName(), $subject);
$comments[$i]->created->value = $timestamp--;
$comments[$i]->save();
}
// Test that a user without the 'access comments' permission cannot see the
// block.
......@@ -61,48 +68,35 @@ function testRecentCommentBlock() {
// posting a node from a node form.
cache_invalidate_tags(array('content' => TRUE));
$this->drupalGet('');
$label = $block->label();
$this->assertNoText($label, 'Block was not found.');
$this->assertNoText(t('Recent comments'));
user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array('access comments'));
// Test that a user with the 'access comments' permission can see the
// block.
$this->drupalLogin($this->web_user);
$this->drupalGet('');
$this->assertText($label, 'Block was found.');
// Test the only the 2 latest comments are shown and in the proper order.
$this->assertNoText($comment1->subject->value, 'Comment not found in block.');
$this->assertText($comment2->subject->value, 'Comment found in block.');
$this->assertText($comment3->comment_body->value, 'Comment found in block.');
$this->assertTrue(strpos($this->drupalGetContent(), $comment3->comment_body->value) < strpos($this->drupalGetContent(), $comment2->subject->value), 'Comments were ordered correctly in block.');
// Set the number of recent comments to show to 10.
$block->getPlugin()->setConfigurationValue('block_count', 10);
$block->save();