Loading ajax_comments.module +39 −0 Changes for ajax_comments.module: 39 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -292,6 +292,45 @@ function ajax_comments_preprocess_comment(&$variables) { $variables['attributes']['class'][] = AjaxCommentsController::$commentClassPrefix . $comment->id(); } /** * Implements template_preprocess_pager(). */ function ajax_comments_preprocess_pager(&$variables) { // Query parameters are added in // core/includes/pager.inc in the template_preprocess_pager(), // where pager_query_add_page() calls pager_get_query_parameters(), // which will pick up the ajax wrapper format from the ajax request, which breaks // the pager. // Unfortunately there is no way to remove this parameter before it is rendered to text, // so this preprocess function removes the parameter with string replacement. $items = []; // Remove ajax wrapper format from first, previous. if (isset($variables['items']['first'])) { $items['first'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['first']['href'])]; } if (isset($variables['items']['previous'])) { $items['previous'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['previous']['href'])]; } // Remove ajax wrapper format from specific page links. if (isset($variables['items']['pages'])) { foreach($variables['items']['pages'] as $key => $value) { $items['pages'][$key] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $value['href'])]; } } // Remove ajax wrapper format from next, last. if (isset($variables['items']['next'])) { $items['next'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['next']['href'])]; } if (isset($variables['items']['last'])) { $items['last'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['last']['href'])]; } $variables['items'] = $items; } /** * Implements template_preprocess_status_messages(). */ Loading src/Controller/AjaxCommentsController.php +34 −2 Changes for src/Controller/AjaxCommentsController.php: 34 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ namespace Drupal\ajax_comments\Controller; use Drupal\ajax_comments\Ajax\ajaxCommentsScrollToElementCommand; use Drupal\ajax_comments\TempStore; use Drupal\ajax_comments\Utility; use Drupal\comment\CommentInterface; Loading @@ -25,6 +24,7 @@ use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; /** * Controller routines for AJAX comments routes. Loading @@ -46,6 +46,15 @@ class AjaxCommentsController extends ControllerBase { */ protected $renderer; /** * The Router service. * * A router class for Drupal. * * @var \Symfony\Component\Routing\RouterInterface */ protected $router; /** * The TempStore service. * Loading @@ -64,13 +73,16 @@ class AjaxCommentsController extends ControllerBase { * The current user. * @param \Drupal\Core\Render\RendererInterface $renderer * The render service. * @param \Symfony\Component\Routing\RouterInterface $router * The Router service. * @param \Drupal\ajax_comments\TempStore $temp_store * The TempStore service. */ public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, TempStore $temp_store) { public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, RouterInterface $router, TempStore $temp_store) { $this->entityTypeManager = $entity_type_manager; $this->currentUser = $current_user; $this->renderer = $renderer; $this->router = $router; $this->tempStore = $temp_store; } Loading @@ -82,6 +94,7 @@ class AjaxCommentsController extends ControllerBase { $container->get('entity_type.manager'), $container->get('current_user'), $container->get('renderer'), $container->get('router.no_access_checks'), $container->get('ajax_comments.temp_store') ); } Loading Loading @@ -122,6 +135,25 @@ class AjaxCommentsController extends ControllerBase { // ajax responses, unset them here. unset($comment_display['#theme_wrappers']); // Remove unneeded route parameters. unset($comment_display[0]['comments']['pager']['#route_parameters']['entity_type']); unset($comment_display[0]['comments']['pager']['#route_parameters']['entity']); unset($comment_display[0]['comments']['pager']['#route_parameters']['field_name']); unset($comment_display[0]['comments']['pager']['#route_parameters']['pid']); $entity_type = $entity->getEntityType(); $entity_type_id = $entity_type->id(); $handler = $this->entityTypeManager()->getRouteProviders($entity_type_id)['html']; $route_collection = $handler->getRoutes($entity_type); $name = 'entity.' . $entity_type->get('id') . '.canonical'; $route = $route_collection->get($name); // Override the ajax route object with the actual entity route. $entity_url = $entity->toURL(); if ($route) { $comment_display[0]['comments']['pager']['#route_name'] = $route; $comment_display[0]['comments']['pager']['#route_parameters'] = $entity_url->getRouteParameters(); } return $comment_display; } Loading tests/src/FunctionalJavascript/AjaxCommentsFunctionalTest.php +22 −3 Changes for tests/src/FunctionalJavascript/AjaxCommentsFunctionalTest.php: 22 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -38,19 +38,31 @@ class AjaxCommentsFunctionalTest extends JavascriptTestBase { ]; /** * Tests that comments can be posted and edited over ajax without errors. * {@inheritdoc} */ public function testCommentPosting() { protected function setUp() { parent::setUp(); $this->entityTypeManager = $this->container->get('entity_type.manager'); // Ensure an `article` node type exists. $this->createContentType(['type' => 'article']); $this->addDefaultCommentField('node', 'article'); $comment_field = $this->entityTypeManager->getStorage('field_config')->load('node.article.comment'); $comment_field->setSetting('per_page', 10); $comment_field->save(); // Enable ajax comments on the comment field. $entity_view_display = EntityViewDisplay::load('node.article.default'); $renderer = $entity_view_display->getRenderer('comment'); $renderer->setThirdPartySetting('ajax_comments', 'enable_ajax_comments', TRUE); $entity_view_display->save(); } /** * Tests that comments can be posted and edited over ajax without errors. */ public function testCommentPosting() { // Enable CKEditor. $format = $this->randomMachineName(); FilterFormat::create([ Loading Loading @@ -111,7 +123,7 @@ JS; $page = $this->getSession()->getPage(); // Post comments through ajax. for ($i = 0; $i < 2; $i++) { for ($i = 0; $i < 11; $i++) { $comment_body_id = $page ->findField('comment_body[0][value]') ->getAttribute('id'); Loading @@ -135,6 +147,13 @@ JS; $this->assertSession()->pageTextContains('New comment 1'); $this->assertSession()->pageTextContains('New comment 2'); $current_url = $this->getSession()->getCurrentUrl(); $parts = parse_url($current_url); $path = empty($parts['path']) ? '/' : $parts['path']; $current_path = preg_replace('/^\\/[^\\.\\/]+\\.php\\//', '/', $path); $this->assertSession()->linkByHrefExists($current_path . '?page=1'); $javascript_assertion = <<<JS (function () { return window.jsErrors.length === 0; Loading Loading
ajax_comments.module +39 −0 Changes for ajax_comments.module: 39 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -292,6 +292,45 @@ function ajax_comments_preprocess_comment(&$variables) { $variables['attributes']['class'][] = AjaxCommentsController::$commentClassPrefix . $comment->id(); } /** * Implements template_preprocess_pager(). */ function ajax_comments_preprocess_pager(&$variables) { // Query parameters are added in // core/includes/pager.inc in the template_preprocess_pager(), // where pager_query_add_page() calls pager_get_query_parameters(), // which will pick up the ajax wrapper format from the ajax request, which breaks // the pager. // Unfortunately there is no way to remove this parameter before it is rendered to text, // so this preprocess function removes the parameter with string replacement. $items = []; // Remove ajax wrapper format from first, previous. if (isset($variables['items']['first'])) { $items['first'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['first']['href'])]; } if (isset($variables['items']['previous'])) { $items['previous'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['previous']['href'])]; } // Remove ajax wrapper format from specific page links. if (isset($variables['items']['pages'])) { foreach($variables['items']['pages'] as $key => $value) { $items['pages'][$key] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $value['href'])]; } } // Remove ajax wrapper format from next, last. if (isset($variables['items']['next'])) { $items['next'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['next']['href'])]; } if (isset($variables['items']['last'])) { $items['last'] = ['href' => str_replace('_wrapper_format=drupal_ajax&', '', $variables['items']['last']['href'])]; } $variables['items'] = $items; } /** * Implements template_preprocess_status_messages(). */ Loading
src/Controller/AjaxCommentsController.php +34 −2 Changes for src/Controller/AjaxCommentsController.php: 34 added lines, 2 removed lines. Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ namespace Drupal\ajax_comments\Controller; use Drupal\ajax_comments\Ajax\ajaxCommentsScrollToElementCommand; use Drupal\ajax_comments\TempStore; use Drupal\ajax_comments\Utility; use Drupal\comment\CommentInterface; Loading @@ -25,6 +24,7 @@ use Drupal\Core\Url; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\Routing\RouterInterface; /** * Controller routines for AJAX comments routes. Loading @@ -46,6 +46,15 @@ class AjaxCommentsController extends ControllerBase { */ protected $renderer; /** * The Router service. * * A router class for Drupal. * * @var \Symfony\Component\Routing\RouterInterface */ protected $router; /** * The TempStore service. * Loading @@ -64,13 +73,16 @@ class AjaxCommentsController extends ControllerBase { * The current user. * @param \Drupal\Core\Render\RendererInterface $renderer * The render service. * @param \Symfony\Component\Routing\RouterInterface $router * The Router service. * @param \Drupal\ajax_comments\TempStore $temp_store * The TempStore service. */ public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, TempStore $temp_store) { public function __construct(EntityTypeManagerInterface $entity_type_manager, AccountInterface $current_user, RendererInterface $renderer, RouterInterface $router, TempStore $temp_store) { $this->entityTypeManager = $entity_type_manager; $this->currentUser = $current_user; $this->renderer = $renderer; $this->router = $router; $this->tempStore = $temp_store; } Loading @@ -82,6 +94,7 @@ class AjaxCommentsController extends ControllerBase { $container->get('entity_type.manager'), $container->get('current_user'), $container->get('renderer'), $container->get('router.no_access_checks'), $container->get('ajax_comments.temp_store') ); } Loading Loading @@ -122,6 +135,25 @@ class AjaxCommentsController extends ControllerBase { // ajax responses, unset them here. unset($comment_display['#theme_wrappers']); // Remove unneeded route parameters. unset($comment_display[0]['comments']['pager']['#route_parameters']['entity_type']); unset($comment_display[0]['comments']['pager']['#route_parameters']['entity']); unset($comment_display[0]['comments']['pager']['#route_parameters']['field_name']); unset($comment_display[0]['comments']['pager']['#route_parameters']['pid']); $entity_type = $entity->getEntityType(); $entity_type_id = $entity_type->id(); $handler = $this->entityTypeManager()->getRouteProviders($entity_type_id)['html']; $route_collection = $handler->getRoutes($entity_type); $name = 'entity.' . $entity_type->get('id') . '.canonical'; $route = $route_collection->get($name); // Override the ajax route object with the actual entity route. $entity_url = $entity->toURL(); if ($route) { $comment_display[0]['comments']['pager']['#route_name'] = $route; $comment_display[0]['comments']['pager']['#route_parameters'] = $entity_url->getRouteParameters(); } return $comment_display; } Loading
tests/src/FunctionalJavascript/AjaxCommentsFunctionalTest.php +22 −3 Changes for tests/src/FunctionalJavascript/AjaxCommentsFunctionalTest.php: 22 added lines, 3 removed lines. Original line number Diff line number Diff line Loading @@ -38,19 +38,31 @@ class AjaxCommentsFunctionalTest extends JavascriptTestBase { ]; /** * Tests that comments can be posted and edited over ajax without errors. * {@inheritdoc} */ public function testCommentPosting() { protected function setUp() { parent::setUp(); $this->entityTypeManager = $this->container->get('entity_type.manager'); // Ensure an `article` node type exists. $this->createContentType(['type' => 'article']); $this->addDefaultCommentField('node', 'article'); $comment_field = $this->entityTypeManager->getStorage('field_config')->load('node.article.comment'); $comment_field->setSetting('per_page', 10); $comment_field->save(); // Enable ajax comments on the comment field. $entity_view_display = EntityViewDisplay::load('node.article.default'); $renderer = $entity_view_display->getRenderer('comment'); $renderer->setThirdPartySetting('ajax_comments', 'enable_ajax_comments', TRUE); $entity_view_display->save(); } /** * Tests that comments can be posted and edited over ajax without errors. */ public function testCommentPosting() { // Enable CKEditor. $format = $this->randomMachineName(); FilterFormat::create([ Loading Loading @@ -111,7 +123,7 @@ JS; $page = $this->getSession()->getPage(); // Post comments through ajax. for ($i = 0; $i < 2; $i++) { for ($i = 0; $i < 11; $i++) { $comment_body_id = $page ->findField('comment_body[0][value]') ->getAttribute('id'); Loading @@ -135,6 +147,13 @@ JS; $this->assertSession()->pageTextContains('New comment 1'); $this->assertSession()->pageTextContains('New comment 2'); $current_url = $this->getSession()->getCurrentUrl(); $parts = parse_url($current_url); $path = empty($parts['path']) ? '/' : $parts['path']; $current_path = preg_replace('/^\\/[^\\.\\/]+\\.php\\//', '/', $path); $this->assertSession()->linkByHrefExists($current_path . '?page=1'); $javascript_assertion = <<<JS (function () { return window.jsErrors.length === 0; Loading