Commit 7cd80db7 authored by catch's avatar catch

Issue #2641682 by gnuget: permalink breaks in comment

parent 8eb8e89a
......@@ -169,6 +169,7 @@ function pager_get_query_parameters() {
* one page.
* - #parameters: An associative array of query string parameters to append
* to the pager links.
* - #route_parameters: An associative array of the route parameters.
* - #quantity: The number of pages in the list.
*/
function template_preprocess_pager(&$variables) {
......@@ -176,6 +177,7 @@ function template_preprocess_pager(&$variables) {
$parameters = $variables['pager']['#parameters'];
$quantity = $variables['pager']['#quantity'];
$route_name = $variables['pager']['#route_name'];
$route_parameters = isset($variables['pager']['#route_parameters']) ? $variables['pager']['#route_parameters'] : [];
global $pager_page_array, $pager_total;
// Nothing to do if there is only one page.
......@@ -218,7 +220,7 @@ function template_preprocess_pager(&$variables) {
$options = array(
'query' => pager_query_add_page($parameters, $element, 0),
);
$items['first']['href'] = \Drupal::url($route_name, [], $options);
$items['first']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[0])) {
$items['first']['text'] = $tags[0];
}
......@@ -227,7 +229,7 @@ function template_preprocess_pager(&$variables) {
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] - 1),
);
$items['previous']['href'] = \Drupal::url($route_name, [], $options);
$items['previous']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[1])) {
$items['previous']['text'] = $tags[1];
}
......@@ -243,7 +245,7 @@ function template_preprocess_pager(&$variables) {
$options = array(
'query' => pager_query_add_page($parameters, $element, $i - 1),
);
$items['pages'][$i]['href'] = \Drupal::url($route_name, [], $options);
$items['pages'][$i]['href'] = \Drupal::url($route_name, $route_parameters, $options);
if ($i == $pager_current) {
$variables['current'] = $i;
}
......@@ -260,7 +262,7 @@ function template_preprocess_pager(&$variables) {
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_page_array[$element] + 1),
);
$items['next']['href'] = \Drupal::url($route_name, [], $options);
$items['next']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[3])) {
$items['next']['text'] = $tags[3];
}
......@@ -269,7 +271,7 @@ function template_preprocess_pager(&$variables) {
$options = array(
'query' => pager_query_add_page($parameters, $element, $pager_max - 1),
);
$items['last']['href'] = \Drupal::url($route_name, [], $options);
$items['last']['href'] = \Drupal::url($route_name, $route_parameters, $options);
if (isset($tags[4])) {
$items['last']['text'] = $tags[4];
}
......
......@@ -137,7 +137,6 @@ public function commentPermalink(Request $request, CommentInterface $comment) {
if ($session = $request->getSession()) {
$redirect_request->setSession($session);
}
// @todo: Convert the pager to use the request object.
$request->query->set('page', $page);
$response = $this->httpKernel->handle($redirect_request, HttpKernelInterface::SUB_REQUEST);
if ($response instanceof CacheableResponseInterface) {
......
......@@ -204,8 +204,7 @@ public function referencedEntities() {
* {@inheritdoc}
*/
public function permalink() {
$entity = $this->getCommentedEntity();
$uri = $entity->urlInfo();
$uri = $this->urlInfo();
$uri->setOption('fragment', 'comment-' . $this->id());
return $uri;
}
......
......@@ -16,6 +16,7 @@
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -79,6 +80,11 @@ public static function defaultSettings() {
*/
protected $entityFormBuilder;
/**
* @param \Drupal\Core\Routing\RouteMatchInterface $routeMatch
*/
protected $routeMatch;
/**
* {@inheritdoc}
*/
......@@ -93,7 +99,8 @@ public static function create(ContainerInterface $container, array $configuratio
$configuration['third_party_settings'],
$container->get('current_user'),
$container->get('entity.manager'),
$container->get('entity.form_builder')
$container->get('entity.form_builder'),
$container->get('current_route_match')
);
}
......@@ -120,14 +127,17 @@ public static function create(ContainerInterface $container, array $configuratio
* The entity manager
* @param \Drupal\Core\Entity\EntityFormBuilderInterface $entity_form_builder
* The entity form builder.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match object.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder) {
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, AccountInterface $current_user, EntityManagerInterface $entity_manager, EntityFormBuilderInterface $entity_form_builder, RouteMatchInterface $route_match) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->viewBuilder = $entity_manager->getViewBuilder('comment');
$this->storage = $entity_manager->getStorage('comment');
$this->currentUser = $current_user;
$this->entityManager = $entity_manager;
$this->entityFormBuilder = $entity_form_builder;
$this->routeMatch = $route_match;
}
/**
......@@ -164,6 +174,12 @@ public function viewElements(FieldItemListInterface $items, $langcode) {
if ($comments) {
$build = $this->viewBuilder->viewMultiple($comments);
$build['pager']['#type'] = 'pager';
// CommentController::commentPermalink() calculates the page number
// where a specific comment appears and does a subrequest pointing to
// that page, we need to pass that subrequest route to our pager to
// keep the pager working.
$build['pager']['#route_name'] = $this->routeMatch->getRouteObject();
$build['pager']['#route_parameters'] = $this->routeMatch->getRawParameters()->all();
if ($this->getSetting('pager_id')) {
$build['pager']['#element'] = $this->getSetting('pager_id');
}
......
......@@ -80,6 +80,12 @@ public function testCommentInterface() {
$this->drupalGet('node/' . $this->node->id());
$this->assertText($subject_text, 'Individual comment subject found.');
$this->assertText($comment_text, 'Individual comment body found.');
$arguments = [
':link' => base_path() . 'comment/' . $comment->id() . '#comment-' . $comment->id(),
];
$pattern_permalink = '//footer[contains(@class,"comment__meta")]/a[contains(@href,:link) and text()="Permalink"]';
$permalink = $this->xpath($pattern_permalink, $arguments);
$this->assertTrue(!empty($permalink), 'Permalink link found.');
// Set comments to have subject and preview to optional.
$this->drupalLogout();
......
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