Commit dc474512 authored by alexpott's avatar alexpott
Browse files

Issue #2456705 by dawehner, adamwhite, larowlan, YesCT: Comment views field...

Issue #2456705 by dawehner, adamwhite, larowlan, YesCT: Comment views field handlers need to be replaced with field/entity aware handlers
parent 561834cd
......@@ -65,7 +65,10 @@ display:
table: comment_field_data
field: subject
relationship: none
plugin_id: comment
type: string
settings:
link_to_entity: true
plugin_id: field
group_type: group
admin_label: ''
label: ''
......@@ -109,8 +112,6 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_comment: true
link_to_entity: false
entity_type: comment
entity_field: subject
changed:
......@@ -215,9 +216,7 @@ display:
admin_label: ''
order: DESC
exposed: false
expose:
label: ''
plugin_id: standard
plugin_id: field
entity_type: comment
entity_field: cid
title: 'Recent comments'
......
......@@ -4,17 +4,6 @@ views.argument.argument_comment_user_uid:
type: views_argument
label: 'Commented user ID'
views.field.comment:
type: views_field
label: 'Comment'
mapping:
link_to_comment:
type: boolean
label: 'Link this field to its comment'
link_to_entity:
type: boolean
label: 'Link field to the entity if there is no comment'
views.field.comment_depth:
type: views_field
label: 'Comment depth'
......
......@@ -27,9 +27,6 @@ public function getViewsData() {
$data['comment_field_data']['subject']['title'] = t('Title');
$data['comment_field_data']['subject']['help'] = t('The title of the comment.');
$data['comment_field_data']['subject']['field']['id'] = 'comment';
$data['comment_field_data']['cid']['field']['id'] = 'comment';
$data['comment_field_data']['name']['title'] = t('Author');
$data['comment_field_data']['name']['help'] = t("The name of the comment's author. Can be rendered as a link to the author's homepage.");
......
<?php
/**
* @file
* Definition of Drupal\comment\Plugin\views\field\Comment.
*/
namespace Drupal\comment\Plugin\views\field;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Drupal\views\ResultRow;
use Drupal\views\ViewExecutable;
use Drupal\views\Plugin\views\display\DisplayPluginBase;
use Drupal\views\Plugin\views\field\FieldPluginBase;
/**
* Field handler to allow linking to a comment.
*
* @ingroup views_field_handlers
*
* @ViewsField("comment")
*/
class Comment extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function usesGroupBy() {
return FALSE;
}
/**
* Overrides \Drupal\views\Plugin\views\field\FieldPluginBase::init().
*
* Provide generic option to link to comment.
*/
public function init(ViewExecutable $view, DisplayPluginBase $display, array &$options = NULL) {
parent::init($view, $display, $options);
if (!empty($this->options['link_to_comment'])) {
$this->additional_fields['cid'] = 'cid';
$this->additional_fields['entity_id'] = array(
'table' => 'comment_field_data',
'field' => 'entity_id'
);
$this->additional_fields['entity_type'] = array(
'table' => 'comment_field_data',
'field' => 'entity_type'
);
}
}
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['link_to_comment'] = array('default' => TRUE);
$options['link_to_entity'] = array('default' => FALSE);
return $options;
}
/**
* Provide link-to-comment option
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
$form['link_to_comment'] = array(
'#title' => $this->t('Link this field to its comment'),
'#description' => $this->t("Enable to override this field's links."),
'#type' => 'checkbox',
'#default_value' => $this->options['link_to_comment'],
);
$form['link_to_entity'] = array(
'#title' => $this->t('Link field to the entity if there is no comment'),
'#type' => 'checkbox',
'#default_value' => $this->options['link_to_entity'],
);
parent::buildOptionsForm($form, $form_state);
}
/**
* Render whatever the data is as a link to the comment or its node.
*
* @param string $data
* The XSS safe string for the link text.
* @param \Drupal\views\ResultRow $values
* The values retrieved from a single row of a view's query result.
*
* @return string
* Returns a string for the link text.
*/
protected function renderLink($data, ResultRow $values) {
if (!empty($this->options['link_to_comment'])) {
$this->options['alter']['make_link'] = TRUE;
$cid = $this->getValue($values, 'cid');
if (!empty($cid)) {
$this->options['alter']['url'] = Url::fromRoute('entity.comment.canonical', ['comment' => $cid]);
$this->options['alter']['fragment'] = "comment-" . $cid;
}
// If there is no comment link to the entity.
elseif ($this->options['link_to_entity']) {
$entity_id = $this->getValue($values, 'entity_id');
$entity_type = $this->getValue($values, 'entity_type');
$entity = entity_load($entity_type, $entity_id);
$this->options['alter']['url'] = $entity->urlInfo();
}
}
return $data;
}
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
return $this->renderLink($this->sanitizeValue($value), $values);
}
}
......@@ -7,7 +7,7 @@
namespace Drupal\comment\Plugin\views\field;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\Plugin\views\field\Field;
use Drupal\views\ResultRow;
/**
......@@ -17,15 +17,20 @@
*
* @ViewsField("comment_depth")
*/
class Depth extends FieldPluginBase {
class Depth extends Field {
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
// Work out the depth of this comment.
$comment_thread = $this->getValue($values);
return count(explode('.', $comment_thread)) - 1;
public function getItems(ResultRow $values) {
$items = parent::getItems($values);
foreach ($items as &$item) {
// Work out the depth of this comment.
$comment_thread = $item['rendered']['#markup'];
$item['rendered']['#markup'] = count(explode('.', $comment_thread)) - 1;
}
return $items;
}
}
<?php
/**
* @file
* Definition of Drupal\comment\Plugin\views\field\NodeComment.
*/
namespace Drupal\comment\Plugin\views\field;
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\views\Plugin\views\field\FieldPluginBase;
use Drupal\views\ResultRow;
/**
* Display node comment status.
*
* @ingroup views_field_handlers
*
* @ViewsField("node_comment")
*/
class NodeComment extends FieldPluginBase {
/**
* {@inheritdoc}
*/
public function render(ResultRow $values) {
$value = $this->getValue($values);
switch ($value) {
case CommentItemInterface::HIDDEN:
default:
return $this->t('Hidden');
case CommentItemInterface::CLOSED:
return $this->t('Closed');
case CommentItemInterface::OPEN:
return $this->t('Open');
}
}
}
......@@ -101,8 +101,9 @@ protected function defaultDisplayOptions() {
$display_options['fields']['subject']['alter']['html'] = 0;
$display_options['fields']['subject']['hide_empty'] = 0;
$display_options['fields']['subject']['empty_zero'] = 0;
$display_options['fields']['subject']['link_to_comment'] = 1;
$display_options['fields']['subject']['plugin_id'] = 'comment';
$display_options['fields']['subject']['plugin_id'] = 'field';
$display_options['fields']['subject']['type'] = 'string';
$display_options['fields']['subject']['settings'] = ['link_to_entity' => TRUE];
return $display_options;
}
......
......@@ -45,6 +45,9 @@ protected function setUp() {
);
$this->comment = entity_create('comment', $comment);
$this->comment->save();
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
}
......
......@@ -62,7 +62,7 @@ protected function setUp($import_test_views = TRUE) {
/* @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role->grantPermission('view comments');
$anonymous_role->grantPermission('access comments');
$anonymous_role->save();
$this->adminUser = User::create([
......@@ -121,6 +121,10 @@ public function testUsername() {
'field' => 'subject',
'id' => 'subject',
'plugin_id' => 'field',
'type' => 'string',
'settings' => [
'link_to_entity' => TRUE,
],
],
],
],
......@@ -153,7 +157,8 @@ public function testUsername() {
// No access to user-profiles, so shouldn't be able to see links.
$this->assertNoLink($this->adminUser->label());
$this->assertNoLink('barry (not verified)');
// Note: External users aren't pointing to drupal user profiles.
$this->assertLink('barry (not verified)');
$this->verbose($this->getRawContent());
$this->assertLink('My comment title');
$this->assertLink('Anonymous comment title');
......
......@@ -64,11 +64,11 @@ public function testCommentFields() {
// @todo Expand the test coverage in https://www.drupal.org/node/2464635
// $this->assertFieldAccess('comment', 'cid', $comment->id());
// $this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
$this->assertFieldAccess('comment', 'cid', $comment->id());
$this->assertFieldAccess('comment', 'cid', $comment_anonymous->id());
$this->assertFieldAccess('comment', 'uuid', $comment->uuid());
// $this->assertFieldAccess('comment', 'subject', 'My comment title');
// $this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
$this->assertFieldAccess('comment', 'subject', 'My comment title');
$this->assertFieldAccess('comment', 'subject', 'Anonymous comment title');
$this->assertFieldAccess('comment', 'name', 'anonymous');
$this->assertFieldAccess('comment', 'mail', 'test@example.com');
$this->assertFieldAccess('comment', 'homepage', 'https://example.com');
......
......@@ -114,19 +114,20 @@ protected function setUp() {
* Tests the block defined by the comments_recent view.
*/
public function testBlockDisplay() {
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
$view = Views::getView('comments_recent');
$view->setDisplay('block_1');
$this->executeView($view);
$map = array(
'comment_field_data_entity_id' => 'entity_id',
'comment_field_data_subject' => 'subject',
'subject' => 'subject',
'cid' => 'cid',
'comment_field_data_created' => 'created'
);
$expected_result = array();
foreach (array_values($this->commentsCreated) as $key => $comment) {
$expected_result[$key]['entity_id'] = $comment->getCommentedEntityId();
$expected_result[$key]['subject'] = $comment->getSubject();
$expected_result[$key]['cid'] = $comment->id();
$expected_result[$key]['created'] = $comment->getCreatedTime();
......
......@@ -78,6 +78,9 @@ public function testCommentWizard() {
$this->drupalPostForm(NULL, $view, t('Save and edit'));
$this->assertUrl('admin/structure/views/view/' . $view['id'], array(), 'Make sure the view saving was successful and the browser got redirected to the edit page.');
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
$view = Views::getView($view['id']);
$view->initHandlers();
$row = $view->display_handler->getOption('row');
......
......@@ -140,9 +140,10 @@ display:
hide_empty: false
empty_zero: false
hide_alter_empty: true
link_to_comment: true
link_to_entity: false
plugin_id: comment
type: string
settings:
link_to_entity: false
plugin_id: field
name:
id: name
table: comment_field_data
......
......@@ -98,7 +98,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_comment: true
relationship: none
group_type: group
admin_label: ''
......@@ -113,8 +112,10 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
link_to_entity: false
plugin_id: comment
type: string
settings:
link_to_entity: true
plugin_id: field
entity_type: comment
entity_field: subject
filters:
......
......@@ -45,7 +45,7 @@ display:
id: subject
table: comment_field_data
field: subject
plugin_id: comment
plugin_id: field
label: ''
alter:
alter_text: false
......@@ -58,7 +58,9 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_comment: true
type: string
settings:
link_to_entity: true
entity_type: comment
entity_field: subject
filters: { }
......
......@@ -83,7 +83,6 @@ display:
html: false
hide_empty: false
empty_zero: false
link_to_comment: true
relationship: none
group_type: group
admin_label: ''
......@@ -98,8 +97,10 @@ display:
element_default_classes: true
empty: ''
hide_alter_empty: true
link_to_entity: false
plugin_id: comment
type: string
settings:
link_to_entity: false
plugin_id: field
entity_type: comment
entity_field: subject
filters:
......
......@@ -108,6 +108,9 @@ function testPageCacheTags() {
'config:user.role.anonymous',
));
// Render the view block adds the languages cache context.
$cache_contexts[] = 'languages:' . LanguageInterface::TYPE_CONTENT;
// Full node page 2.
$this->assertPageCacheContextsAndTags($node_2->urlInfo(), $cache_contexts, array(
'rendered',
......
......@@ -67,6 +67,9 @@ public function testGetEntity() {
));
$comment->save();
$user = $this->drupalCreateUser(['access comments']);
$this->drupalLogin($user);
$view = Views::getView('test_field_get_entity');
$this->executeView($view);
$row = $view->result[0];
......
......@@ -27,7 +27,7 @@ display:
field: cid
id: cid
table: comment_field_data
plugin_id: comment
plugin_id: field
entity_type: comment
entity_field: cid
nid:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment