Skip to content
Snippets Groups Projects
Commit 05212a97 authored by Dries Buytaert's avatar Dries Buytaert
Browse files

Issue #2271349 by Berdir, Arla, blueminds, larowlan, Dave Reid: Node and...

Issue #2271349 by Berdir, Arla, blueminds, larowlan, Dave Reid: Node and Comment ops links should be display components (which can be disabled).
parent 0ede95dd
Branches
Tags
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
Showing
with 129 additions and 190 deletions
...@@ -111,6 +111,12 @@ function comment_entity_extra_field_info() { ...@@ -111,6 +111,12 @@ function comment_entity_extra_field_info() {
), ),
), ),
); );
$return['comment'][$comment_type->id()]['display']['links'] = array(
'label' => t('Links'),
'description' => t('Comment operation links'),
'weight' => 100,
'visible' => TRUE,
);
} }
return $return; return $return;
......
...@@ -111,6 +111,9 @@ public function buildComponents(array &$build, array $entities, array $displays, ...@@ -111,6 +111,9 @@ public function buildComponents(array &$build, array $entities, array $displays,
} }
$build[$id]['#entity'] = $entity; $build[$id]['#entity'] = $entity;
$build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle(); $build[$id]['#theme'] = 'comment__' . $entity->getFieldName() . '__' . $commented_entity->bundle();
$display = $displays[$entity->bundle()];
if ($display->getComponent('links')) {
$callback = '\Drupal\comment\CommentViewBuilder::renderLinks'; $callback = '\Drupal\comment\CommentViewBuilder::renderLinks';
$context = array( $context = array(
'comment_entity_id' => $entity->id(), 'comment_entity_id' => $entity->id(),
...@@ -129,6 +132,7 @@ public function buildComponents(array &$build, array $entities, array $displays, ...@@ -129,6 +132,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
), ),
'#markup' => $placeholder, '#markup' => $placeholder,
); );
}
$account = comment_prepare_author($entity); $account = comment_prepare_author($entity);
if (\Drupal::config('user.settings')->get('signatures') && $account->getSignature()) { if (\Drupal::config('user.settings')->get('signatures') && $account->getSignature()) {
......
<?php
/**
* @file
* Contains \Drupal\comment\Plugin\views\row\CommentRow.
*/
namespace Drupal\comment\Plugin\views\row;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\row\EntityRow;
/**
* Plugin which performs a comment_view on the resulting object.
*
* @ViewsRow(
* id = "entity:comment",
* )
*/
class CommentRow extends EntityRow {
/**
* {@inheritdoc}
*/
protected function defineOptions() {
$options = parent::defineOptions();
$options['links'] = array('default' => TRUE);
$options['view_mode']['default'] = 'full';
return $options;
}
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['links'] = array(
'#type' => 'checkbox',
'#title' => t('Display links'),
'#default_value' => $this->options['links'],
);
}
/**
* {@inheritdoc}
*/
public function render($row) {
$build = parent::render($row);
if (!$this->options['links']) {
unset($build['links']);
}
return $build;
}
}
...@@ -32,7 +32,6 @@ protected function defineOptions() { ...@@ -32,7 +32,6 @@ protected function defineOptions() {
$options = parent::defineOptions(); $options = parent::defineOptions();
$options['view_mode'] = array('default' => 'default'); $options['view_mode'] = array('default' => 'default');
$options['links'] = array('default' => FALSE, 'bool' => TRUE);
return $options; return $options;
} }
...@@ -46,11 +45,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ...@@ -46,11 +45,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#options' => $this->options_form_summary_options(), '#options' => $this->options_form_summary_options(),
'#default_value' => $this->options['view_mode'], '#default_value' => $this->options['view_mode'],
); );
$form['links'] = array(
'#type' => 'checkbox',
'#title' => t('Display links'),
'#default_value' => $this->options['links'],
);
} }
public function preRender($result) { public function preRender($result) {
...@@ -134,14 +128,8 @@ public function render($row) { ...@@ -134,14 +128,8 @@ public function render($row) {
$this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $comment->rss_namespaces); $this->view->style_plugin->namespaces = array_merge($this->view->style_plugin->namespaces, $comment->rss_namespaces);
} }
// Hide the links if desired.
if (!$this->options['links']) {
hide($build['links']);
}
if ($view_mode != 'title') { if ($view_mode != 'title') {
// We render comment contents and force links to be last. // We render comment contents.
$build['links']['#weight'] = 1000;
$item_text .= drupal_render($build); $item_text .= drupal_render($build);
} }
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface; use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\entity\Entity\EntityViewDisplay;
/** /**
* Basic comment links tests to ensure markup present. * Basic comment links tests to ensure markup present.
...@@ -42,16 +43,7 @@ class CommentLinksTest extends CommentTestBase { ...@@ -42,16 +43,7 @@ class CommentLinksTest extends CommentTestBase {
public static $modules = array('views'); public static $modules = array('views');
/** /**
* Tests comment links. * Tests that comment links are output and can be hidden.
*
* The output of comment links depends on various environment conditions:
* - Various Comment module configuration settings, user registration
* settings, and user access permissions.
* - Whether the user is authenticated or not, and whether any comments exist.
*
* To account for all possible cases, this test creates permutations of all
* possible conditions and tests the expected appearance of comment links in
* each environment.
*/ */
public function testCommentLinks() { public function testCommentLinks() {
// Bartik theme alters comment links, so use a different theme. // Bartik theme alters comment links, so use a different theme.
...@@ -112,6 +104,27 @@ public function testCommentLinks() { ...@@ -112,6 +104,27 @@ public function testCommentLinks() {
} }
$this->assertLink('Add new comment'); $this->assertLink('Add new comment');
} }
// Make sure we can hide node links.
entity_get_display('node', $this->node->bundle(), 'default')
->removeComponent('links')
->save();
$this->drupalGet($this->node->url());
$this->assertNoLink('1 comment');
$this->assertNoLink('Add new comment');
// Visit the full node, make sure there are links for the comment.
$this->drupalGet('node/' . $this->node->id());
$this->assertText($comment->getSubject());
$this->assertLink('Reply');
// Make sure we can hide comment links.
entity_get_display('comment', 'comment', 'default')
->removeComponent('links')
->save();
$this->drupalGet('node/' . $this->node->id());
$this->assertText($comment->getSubject());
$this->assertNoLink('Reply');
} }
} }
...@@ -107,5 +107,7 @@ ...@@ -107,5 +107,7 @@
</div> </div>
{% endif %} {% endif %}
</div> </div>
{% if content.links %}
{{ content.links }} {{ content.links }}
{% endif %}
</article> </article>
...@@ -68,7 +68,6 @@ display: ...@@ -68,7 +68,6 @@ display:
row: row:
type: 'entity:comment' type: 'entity:comment'
options: options:
links: true
view_mode: full view_mode: full
relationships: relationships:
node: node:
......
...@@ -63,8 +63,7 @@ display: ...@@ -63,8 +63,7 @@ display:
row: row:
type: 'entity:comment' type: 'entity:comment'
options: options:
links: true view_mode: default
view_mode: full
rendering_language: translation_language_renderer rendering_language: translation_language_renderer
relationships: relationships:
node: node:
......
...@@ -48,12 +48,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) { ...@@ -48,12 +48,6 @@ public function settingsForm(array $form, FormStateInterface $form_state) {
'#required' => TRUE, '#required' => TRUE,
); );
$elements['links'] = array(
'#type' => 'checkbox',
'#title' => t('Show links'),
'#default_value' => $this->getSetting('links'),
);
return $elements; return $elements;
} }
...@@ -66,7 +60,6 @@ public function settingsSummary() { ...@@ -66,7 +60,6 @@ public function settingsSummary() {
$view_modes = \Drupal::entityManager()->getViewModeOptions($this->getFieldSetting('target_type')); $view_modes = \Drupal::entityManager()->getViewModeOptions($this->getFieldSetting('target_type'));
$view_mode = $this->getSetting('view_mode'); $view_mode = $this->getSetting('view_mode');
$summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode)); $summary[] = t('Rendered as @mode', array('@mode' => isset($view_modes[$view_mode]) ? $view_modes[$view_mode] : $view_mode));
$summary[] = $this->getSetting('links') ? t('Display links') : t('Do not display links');
return $summary; return $summary;
} }
...@@ -76,9 +69,6 @@ public function settingsSummary() { ...@@ -76,9 +69,6 @@ public function settingsSummary() {
*/ */
public function viewElements(FieldItemListInterface $items) { public function viewElements(FieldItemListInterface $items) {
$view_mode = $this->getSetting('view_mode'); $view_mode = $this->getSetting('view_mode');
$links = $this->getSetting('links');
$target_type = $this->getFieldSetting('target_type');
$elements = array(); $elements = array();
...@@ -97,10 +87,6 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -97,10 +87,6 @@ public function viewElements(FieldItemListInterface $items) {
if (!empty($item->target_id)) { if (!empty($item->target_id)) {
$elements[$delta] = entity_view($item->entity, $view_mode, $item->getLangcode()); $elements[$delta] = entity_view($item->entity, $view_mode, $item->getLangcode());
if (empty($links) && isset($result[$delta][$target_type][$item->target_id]['links'])) {
// Hide the element links.
$elements[$delta][$target_type][$item->target_id]['links']['#access'] = FALSE;
}
// Add a resource attribute to set the mapping property's value to the // Add a resource attribute to set the mapping property's value to the
// entity's url. Since we don't know what the markup of the entity will // entity's url. Since we don't know what the markup of the entity will
// be, we shouldn't rely on it for structured data such as RDFa. // be, we shouldn't rely on it for structured data such as RDFa.
......
...@@ -116,7 +116,6 @@ display: ...@@ -116,7 +116,6 @@ display:
type: 'entity:node' type: 'entity:node'
options: options:
view_mode: teaser view_mode: teaser
links: true
comments: false comments: false
provider: node provider: node
header: { } header: { }
......
...@@ -196,7 +196,6 @@ display: ...@@ -196,7 +196,6 @@ display:
options: options:
build_mode: teaser build_mode: teaser
comments: false comments: false
links: true
view_mode: teaser view_mode: teaser
provider: views provider: views
sorts: sorts:
...@@ -284,7 +283,6 @@ display: ...@@ -284,7 +283,6 @@ display:
options: options:
relationship: none relationship: none
view_mode: rss view_mode: rss
links: false
provider: views provider: views
field_langcode: '***LANGUAGE_language_content***' field_langcode: '***LANGUAGE_language_content***'
field_langcode_add_to_query: null field_langcode_add_to_query: null
...@@ -4,9 +4,6 @@ ...@@ -4,9 +4,6 @@
type: views_entity_row type: views_entity_row
label: 'Entity options' label: 'Entity options'
mapping: mapping:
links:
type: boolean
label: 'Display links'
comments: comments:
type: boolean type: boolean
label: 'Show comments' label: 'Show comments'
...@@ -253,6 +250,3 @@ views.row.node_rss: ...@@ -253,6 +250,3 @@ views.row.node_rss:
view_mode: view_mode:
type: string type: string
label: 'Display type' label: 'Display type'
links:
type: boolean
label: 'Display links'
...@@ -432,6 +432,12 @@ function node_entity_extra_field_info() { ...@@ -432,6 +432,12 @@ function node_entity_extra_field_info() {
'weight' => 0, 'weight' => 0,
'visible' => FALSE, 'visible' => FALSE,
); );
$extra['node'][$bundle->type]['display']['links'] = array(
'label' => t('Links'),
'description' => $description,
'weight' => 100,
'visible' => TRUE,
);
} }
return $extra; return $extra;
......
...@@ -34,6 +34,7 @@ public function buildComponents(array &$build, array $entities, array $displays, ...@@ -34,6 +34,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
$bundle = $entity->bundle(); $bundle = $entity->bundle();
$display = $displays[$bundle]; $display = $displays[$bundle];
if ($display->getComponent('links')) {
$callback = '\Drupal\node\NodeViewBuilder::renderLinks'; $callback = '\Drupal\node\NodeViewBuilder::renderLinks';
$context = array( $context = array(
'node_entity_id' => $entity->id(), 'node_entity_id' => $entity->id(),
...@@ -50,7 +51,7 @@ public function buildComponents(array &$build, array $entities, array $displays, ...@@ -50,7 +51,7 @@ public function buildComponents(array &$build, array $entities, array $displays,
), ),
'#markup' => $placeholder, '#markup' => $placeholder,
); );
}
// Add Language field text element to node render array. // Add Language field text element to node render array.
if ($display->getComponent('langcode')) { if ($display->getComponent('langcode')) {
......
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
namespace Drupal\node\Plugin\views\row; namespace Drupal\node\Plugin\views\row;
use Drupal\Core\Form\FormStateInterface;
use Drupal\views\Plugin\views\row\EntityRow; use Drupal\views\Plugin\views\row\EntityRow;
/** /**
...@@ -31,22 +30,7 @@ protected function defineOptions() { ...@@ -31,22 +30,7 @@ protected function defineOptions() {
$options['view_mode']['default'] = 'teaser'; $options['view_mode']['default'] = 'teaser';
$options['links'] = array('default' => TRUE, 'bool' => TRUE);
return $options; return $options;
} }
/**
* {@inheritdoc}
*/
public function buildOptionsForm(&$form, FormStateInterface $form_state) {
parent::buildOptionsForm($form, $form_state);
$form['links'] = array(
'#type' => 'checkbox',
'#title' => t('Display links'),
'#default_value' => $this->options['links'],
);
}
} }
...@@ -40,7 +40,6 @@ protected function defineOptions() { ...@@ -40,7 +40,6 @@ protected function defineOptions() {
$options = parent::defineOptions(); $options = parent::defineOptions();
$options['view_mode'] = array('default' => 'default'); $options['view_mode'] = array('default' => 'default');
$options['links'] = array('default' => FALSE, 'bool' => TRUE);
return $options; return $options;
} }
...@@ -54,11 +53,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) { ...@@ -54,11 +53,6 @@ public function buildOptionsForm(&$form, FormStateInterface $form_state) {
'#options' => $this->buildOptionsForm_summary_options(), '#options' => $this->buildOptionsForm_summary_options(),
'#default_value' => $this->options['view_mode'], '#default_value' => $this->options['view_mode'],
); );
$form['links'] = array(
'#type' => 'checkbox',
'#title' => t('Display links'),
'#default_value' => $this->options['links'],
);
} }
/** /**
...@@ -152,14 +146,8 @@ public function render($row) { ...@@ -152,14 +146,8 @@ public function render($row) {
$this->view->style_plugin->namespaces += $xml_rdf_namespaces; $this->view->style_plugin->namespaces += $xml_rdf_namespaces;
} }
// Hide the links if desired.
if (!$this->options['links']) {
hide($build['links']);
}
if ($display_mode != 'title') { if ($display_mode != 'title') {
// We render node contents and force links to be last. // We render node contents.
$build['links']['#weight'] = 1000;
$item_text .= drupal_render($build); $item_text .= drupal_render($build);
} }
......
<?php
/**
* @file
* Contains \Drupal\node\Tests\NodeLinksTest
*/
namespace Drupal\node\Tests;
/**
* Tests the output of node links (read more, add new comment, etc).
*
* @group node
*/
class NodeLinksTest extends NodeTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('views');
/**
* Tests that the links can be hidden in the view display settings.
*/
public function testHideLinks() {
$node = $this->drupalCreateNode(array(
'type' => 'article',
'promote' => NODE_PROMOTED,
));
// Links are displayed by default.
$this->drupalGet('node');
$this->assertText($node->getTitle());
$this->assertLink('Read more');
// Hide links.
entity_get_display('node', 'article', 'teaser')
->removeComponent('links')
->save();
$this->drupalGet('node');
$this->assertText($node->getTitle());
$this->assertNoLink('Read more');
}
}
...@@ -112,6 +112,9 @@ public function testFilters() { ...@@ -112,6 +112,9 @@ public function testFilters() {
* Message suffix to display. * Message suffix to display.
*/ */
protected function assertPageCounts($path, $counts, $message) { protected function assertPageCounts($path, $counts, $message) {
// Disable read more links.
entity_get_display('node', 'page', 'teaser')->removeComponent('links')->save();
// Get the text of the page. // Get the text of the page.
$this->drupalGet($path); $this->drupalGet($path);
$text = $this->getTextContent(); $text = $this->getTextContent();
......
...@@ -125,27 +125,6 @@ public function testRowPlugin() { ...@@ -125,27 +125,6 @@ public function testRowPlugin() {
$this->assertTrue(strpos($output, $node->body->summary) !== FALSE, 'Make sure the teaser appears in the output of the view.'); $this->assertTrue(strpos($output, $node->body->summary) !== FALSE, 'Make sure the teaser appears in the output of the view.');
$this->assertFalse(strpos($output, $node->body->value) !== FALSE, 'Make sure the full text does not appears in the output of the view if teaser is set as viewmode.'); $this->assertFalse(strpos($output, $node->body->value) !== FALSE, 'Make sure the full text does not appears in the output of the view if teaser is set as viewmode.');
} }
// Test with links disabled.
$view->rowPlugin->options['links'] = FALSE;
\Drupal::entityManager()->getViewBuilder('node')->resetCache();
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output);
foreach ($this->nodes as $node) {
$this->assertFalse($this->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(':class' => 'node-readmore', ':href' => "node/{$node->id()}")), 'Make sure no readmore link appears.');
}
// Test with links enabled.
$view->rowPlugin->options['links'] = TRUE;
\Drupal::entityManager()->getViewBuilder('node')->resetCache();
$output = $view->preview();
$output = drupal_render($output);
$this->drupalSetContent($output);
foreach ($this->nodes as $node) {
$this->assertTrue($this->xpath('//li[contains(@class, :class)]/a[contains(@href, :href)]', array(':class' => 'node-readmore', ':href' => "node/{$node->id()}")), 'Make sure no readmore link appears.');
}
} }
} }
...@@ -47,7 +47,6 @@ display: ...@@ -47,7 +47,6 @@ display:
options: options:
build_mode: teaser build_mode: teaser
comments: false comments: false
links: true
view_mode: teaser view_mode: teaser
style: style:
type: default type: default
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment