-
-
Save bangpound/4db5ca35f45503edafebbb103ed056e5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/ajax_comments.module b/ajax_comments.module | |
index 6f5adba..5252a07 100644 | |
--- a/ajax_comments.module | |
+++ b/ajax_comments.module | |
@@ -15,6 +15,7 @@ use Drupal\Component\Utility\Html; | |
use Drupal\Core\Ajax; | |
use Drupal\Core\Ajax\AjaxResponse; | |
use Drupal\Core\Ajax\AlertCommand; | |
+use Drupal\Core\Ajax\RemoveCommand; | |
use Drupal\Core\Ajax\CloseDialogCommand; | |
use Drupal\Core\Ajax\HtmlCommand; | |
use Drupal\Core\Entity\ContentEntityInterface; | |
@@ -282,6 +283,25 @@ function _ajax_comments_preview_js($form, FormStateInterface $form_state) { | |
} | |
/** | |
+ * Ajax form callback: Cancel comment add. | |
+ */ | |
+function _ajax_comments_add_cancel_js($form, FormStateInterface $form_state) { | |
+ $response = new AjaxResponse(); | |
+ $response->addCommand(new RemoveCommand('.ajax-comments-form-reply')); | |
+ return $response; | |
+} | |
+ | |
+/** | |
+ * Ajax form callback: Cancel comment edit. | |
+ */ | |
+function _ajax_comments_edit_cancel_js($form, FormStateInterface $form_state) { | |
+ $message = 'Comment edit cancel'; | |
+ $response = new AjaxResponse(); | |
+ $response->addCommand(new AlertCommand($message)); | |
+ return $response; | |
+} | |
+ | |
+/** | |
* Implements template_preprocess_comment(). | |
*/ | |
function ajax_comments_preprocess_comment(&$variables) { | |
diff --git a/src/Controller/AjaxCommentsController.php b/src/Controller/AjaxCommentsController.php | |
index 9e5441a..cd31919 100644 | |
--- a/src/Controller/AjaxCommentsController.php | |
+++ b/src/Controller/AjaxCommentsController.php | |
@@ -19,6 +19,7 @@ use Drupal\Core\Ajax\ReplaceCommand; | |
use Drupal\Core\Controller\ControllerBase; | |
use Drupal\Core\Entity\EntityInterface; | |
use Drupal\Core\Entity\EntityTypeManagerInterface; | |
+use Drupal\Core\Render\Element; | |
use Drupal\Core\Render\RendererInterface; | |
use Drupal\Core\Session\AccountInterface; | |
use Drupal\Core\Url; | |
@@ -125,6 +126,32 @@ class AjaxCommentsController extends ControllerBase { | |
return $comment_display; | |
} | |
+ protected function buildReplyFieldResponse(Request $request, AjaxResponse $response, EntityInterface $entity, $field_name, $pid = null) { | |
+ // Build a comment field render array for the ajax response. | |
+ $comment_display = $this->renderCommentField($entity, $field_name); | |
+ | |
+ // Build a comment field render array for the ajax response. | |
+ $ctrl = CommentController::create(\Drupal::getContainer()); | |
+ $reply_form = $ctrl->getReplyForm($request, $entity, $field_name, $pid); | |
+ | |
+ unset($comment_display[0]['comment_form']); | |
+ foreach (Element::children($comment_display[0]['comments']) as $key) { | |
+ if (isset($comment_display[0]['comments'][$key]['#comment']) && $comment_display[0]['comments'][$key]['#comment']->id() === $pid) { | |
+ $comment_display[0]['comments'][$key] = $reply_form; | |
+ } | |
+ } | |
+ | |
+ // Get the wrapper HTML id selector. | |
+ $selectors = $this->tempStore->getSelectors($request); | |
+ $wrapper_html_id = $selectors['wrapper_html_id']; | |
+ | |
+ // Rendering the comment form below (as part of comment_display) triggers | |
+ // form processing. | |
+ $response->addCommand(new ReplaceCommand($wrapper_html_id, $comment_display)); | |
+ | |
+ return $response; | |
+ } | |
+ | |
/** | |
* Create an ajax response to replace the comment field. | |
* | |
@@ -184,7 +211,7 @@ class AjaxCommentsController extends ControllerBase { | |
foreach ($response->getCommands() as $command) { | |
if ($command['command'] === 'insert' && $command['method'] === 'replaceWith') { | |
$markup = $command['data']->__toString(); | |
- if (preg_match('/\sid="(.*)"/', $markup, $matches)) { | |
+ if (preg_match('/\sid="([^\\s]*)"/', $markup, $matches)) { | |
$selector = '#' . $matches[1]; | |
break; | |
} | |
@@ -532,14 +559,25 @@ class AjaxCommentsController extends ControllerBase { | |
// but adding this access check as a fallback. | |
$this->replyAccess($request, $response, $entity, $field_name, $pid); | |
- // Build the updated comment field and insert into a replaceWith response. | |
- // Also prepend any status messages in the response. | |
- $response = $this->buildCommentFieldResponse( | |
- $request, | |
- $response, | |
- $entity, | |
- $field_name | |
- ); | |
+ if ($request->getMethod() === 'POST') { | |
+ // Build the updated comment field and insert into a replaceWith response. | |
+ // Also prepend any status messages in the response. | |
+ $response = $this->buildReplyFieldResponse( | |
+ $request, | |
+ $response, | |
+ $entity, | |
+ $field_name, | |
+ $pid | |
+ ); | |
+ } | |
+ else { | |
+ $response = $this->buildCommentFieldResponse( | |
+ $request, | |
+ $response, | |
+ $entity, | |
+ $field_name | |
+ ); | |
+ } | |
// The form_html_id should have been updated by the form constructor when | |
// $this->buildCommentFieldResponse() was called, so retrieve the updated |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment