protected function AjaxCommentsController::addMessages in AJAX Comments 8
Add messages to the ajax response.
Parameters
\Symfony\Component\HttpFoundation\Request $request: The current request object.
\Drupal\Core\Ajax\AjaxResponse $response: The response object being built.
string $selector: The DOM selector used to insert status messages.
string $position: Indicates whether to use PrependCommand, BeforeCommand, AppendCommand, or AfterCommand.
Return value
\Drupal\Core\Ajax\AjaxResponse The modified ajax response.
4 calls to AjaxCommentsController::addMessages()
- AjaxCommentsController::add in src/
Controller/ AjaxCommentsController.php - Builds ajax response for adding a new comment without a parent comment.
- AjaxCommentsController::delete in src/
Controller/ AjaxCommentsController.php - Builds ajax response for deleting a comment.
- AjaxCommentsController::replyAccess in src/
Controller/ AjaxCommentsController.php - Check the user's permission to post a comment.
- AjaxCommentsController::save in src/
Controller/ AjaxCommentsController.php - Submit handler for the comment reply and edit forms.
File
- src/
Controller/ AjaxCommentsController.php, line 230
Class
- AjaxCommentsController
- Controller routines for AJAX comments routes.
Namespace
Drupal\ajax_comments\ControllerCode
protected function addMessages(Request $request, AjaxResponse $response, $selector = '', $position = 'prepend') {
$settings = \Drupal::config('ajax_comments.settings');
$notify = $settings
->get('notify');
if ($notify || !empty($this->messenger
->messagesByType(MessengerInterface::TYPE_ERROR))) {
if (empty($selector)) {
// Use the first id found in the ajax replacement markup to be
// inserted into the page as the selector, if none was provided.
foreach ($response
->getCommands() as $command) {
if ($command['command'] === 'insert' && $command['method'] === 'replaceWith') {
$markup = $command['data']
->__toString();
if (preg_match('/\\sid="(.*)"/', $markup, $matches)) {
$selector = '#' . $matches[1];
break;
}
}
}
}
// Add any status messages.
$status_messages = [
'#type' => 'status_messages',
];
switch ($position) {
case 'replace':
$command = new ReplaceCommand($selector, $this->renderer
->renderRoot($status_messages));
break;
case 'after':
$command = new AfterCommand($selector, $this->renderer
->renderRoot($status_messages));
break;
case 'before':
$command = new BeforeCommand($selector, $this->renderer
->renderRoot($status_messages));
break;
case 'append':
$command = new AppendCommand($selector, $this->renderer
->renderRoot($status_messages));
break;
case 'prepend':
default:
$command = new PrependCommand($selector, $this->renderer
->renderRoot($status_messages));
}
$response
->addCommand($command);
}
else {
// Render messages to avoid display them when reloading the page.
$status_messages = [
'#type' => 'status_messages',
];
$this->renderer
->renderRoot($status_messages);
}
return $response;
}