class AjaxCommentsController in Open Social 8.8
Same name and namespace in other branches
- 8.9 modules/custom/social_ajax_comments/src/Controller/AjaxCommentsController.php \Drupal\social_ajax_comments\Controller\AjaxCommentsController
- 10.3.x modules/custom/social_ajax_comments/src/Controller/AjaxCommentsController.php \Drupal\social_ajax_comments\Controller\AjaxCommentsController
- 10.0.x modules/custom/social_ajax_comments/src/Controller/AjaxCommentsController.php \Drupal\social_ajax_comments\Controller\AjaxCommentsController
- 10.1.x modules/custom/social_ajax_comments/src/Controller/AjaxCommentsController.php \Drupal\social_ajax_comments\Controller\AjaxCommentsController
- 10.2.x modules/custom/social_ajax_comments/src/Controller/AjaxCommentsController.php \Drupal\social_ajax_comments\Controller\AjaxCommentsController
Controller routines for AJAX comments routes.
Hierarchy
- class \Drupal\Core\Controller\ControllerBase implements ContainerInjectionInterface uses LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\ajax_comments\Controller\AjaxCommentsController
- class \Drupal\social_ajax_comments\Controller\AjaxCommentsController
- class \Drupal\ajax_comments\Controller\AjaxCommentsController
Expanded class hierarchy of AjaxCommentsController
File
- modules/
custom/ social_ajax_comments/ src/ Controller/ AjaxCommentsController.php, line 15
Namespace
Drupal\social_ajax_comments\ControllerView source
class AjaxCommentsController extends ContribController {
/**
* Cancel handler for the cancel form.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
* @param int $cid
* The id of the comment being edited, or 0 if this is a new comment.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
*/
public function socialCancel(Request $request, $cid) {
// This is based on AjaxCommentsController::cancel.
// the only change is we have some more wrappers we need to remove,
// we can't tell this to ajax_comments because we render it in our template
// so instead we will just remove whatever we need.
$response = new AjaxResponse();
// Get the selectors.
$selectors = $this->tempStore
->getSelectors($request, $overwrite = TRUE);
$wrapper_html_id = $selectors['wrapper_html_id'];
$form_html_id = $selectors['form_html_id'];
if ($cid != 0) {
// Show the hidden anchor.
$response
->addCommand(new InvokeCommand('a#comment-' . $cid, 'show', [
200,
'linear',
]));
// Show the hidden comment.
$response
->addCommand(new InvokeCommand(static::getCommentSelectorPrefix() . $cid, 'show', [
200,
'linear',
]));
}
// Replace the # from the form_html_id selector and add .social_ so we know
// that we are sure we are just removing our specific form class.
$social_form_id = str_replace('#', '.social_reply_form_wrapper_', $form_html_id);
// Remove the form, based on $variables['comment_wrapper'] in form.inc.
$response
->addCommand(new RemoveCommand($social_form_id));
// Remove any messages, if applicable.
$response
->addCommand(new RemoveCommand($wrapper_html_id . ' .js-ajax-comments-messages'));
// Clear out the tempStore variables.
$this->tempStore
->deleteAll();
return $response;
}
/**
* Builds ajax response for adding a new comment without a parent comment.
*
* This is copied from AjaxCommentsController::add because a reply on
* a reply is using the add new Form with a mention. While Ajax comments uses
* the save function for a reply. This results in status message not being
* rendered correctly.
* The only change here is the addMessage is placed above the reply.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* The current request object.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity this comment belongs to.
* @param string $field_name
* The field_name to which the comment belongs.
* @param int $pid
* (optional) Some comments are replies to other comments. In those cases,
* $pid is the parent comment's comment ID. Defaults to NULL.
*
* @return \Drupal\Core\Ajax\AjaxResponse
* The Ajax response.
*
* @see \Drupal\comment\Controller\CommentController::getReplyForm()
*/
public function socialAdd(Request $request, EntityInterface $entity, $field_name, $pid = NULL) {
$response = new AjaxResponse();
// Store the selectors from the incoming request, if applicable.
// If the selectors are not in the request, the stored ones will
// not be overwritten.
$this->tempStore
->getSelectors($request, $overwrite = TRUE);
// Check the user's access to reply.
// The user should not have made it this far without proper permission,
// but adding this access check as a fallback.
$this
->replyAccess($request, $response, $entity, $field_name, $pid);
// If $this->replyAccess() added any commands to the AjaxResponse,
// it means that access was denied, so we should NOT submit the form
// and rebuild the comment field. Just return the response with the
// error message.
if (!empty($response
->getCommands())) {
return $response;
}
// Build the comment entity form.
// This approach is very similar to the one taken in
// \Drupal\comment\CommentLazyBuilders::renderForm().
$comment = $this
->entityTypeManager()
->getStorage('comment')
->create([
'entity_id' => $entity
->id(),
'pid' => $pid,
'entity_type' => $entity
->getEntityTypeId(),
'field_name' => $field_name,
]);
// Rebuild the form to trigger form submission.
$form = $this
->entityFormBuilder()
->getForm($comment);
// Check for errors.
if (empty(drupal_get_messages('error', FALSE))) {
// If there are no errors, set the ajax-updated
// selector value for the form.
$this->tempStore
->setSelector('form_html_id', $form['#attributes']['id']);
// Build the updated comment field and insert into a replaceWith
// response.
$response = $this
->buildCommentFieldResponse($request, $response, $entity, $field_name);
}
else {
// Retrieve the selector values for use in building the response.
$selectors = $this->tempStore
->getSelectors($request, $overwrite = TRUE);
$wrapper_html_id = $selectors['wrapper_html_id'];
// If there are errors, remove old messages.
$response
->addCommand(new RemoveCommand($wrapper_html_id . ' .js-ajax-comments-messages'));
}
// This ensures for a reply we will render the comment above the reply.
if ($comment
->isNew()) {
// Retrieve the comment id of the new comment, which was saved in
// AjaxCommentsForm::save() during the previous HTTP request.
$cid = $this->tempStore
->getCid();
// Try to insert the message above the new comment.
if (!empty($cid) && !$errors && \Drupal::currentUser()
->hasPermission('skip comment approval')) {
$selector = static::getCommentSelectorPrefix() . $cid;
$response = $this
->addMessages($request, $response, $selector, 'before');
}
else {
$response = $this
->addMessages($request, $response, static::getCommentSelectorPrefix() . $comment
->get('pid')->target_id, 'after');
}
}
// Clear out the tempStore variables.
$this->tempStore
->deleteAll();
// Remove the libraries from the response, otherwise when
// core/misc/drupal.js is reinserted into the DOM, the following line of
// code will execute, causing Drupal.attachBehaviors() to run on the entire
// document, and reattach behaviors to DOM elements that already have them:
// @code
// // Attach all behaviors.
// domready(function(){Drupal.attachBehaviors(document,drupalSettings);});
// @endcode
$attachments = $response
->getAttachments();
// Need to have only 'core/drupalSettings' in the asset library list.
// If neither 'core/drupalSettings', nor a library with a dependency on it,
// is in the list of libraries, drupalSettings will be stripped out of the
// ajax response by \Drupal\Core\Asset\AssetResolver::getJsAssets().
$attachments['library'] = [
'core/drupalSettings',
];
// We need to keep the drupalSettings in the response, otherwise the
// #ajax properties in the form definition won't be properly attached to
// the rebuilt comment field returned in the ajax response, and subsequent
// ajax interactions will be broken.
$response
->setAttachments($attachments);
return $response;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AjaxCommentsController:: |
public static | property | Class prefix to apply to each comment. | |
AjaxCommentsController:: |
protected | property |
The messenger service. Overrides MessengerTrait:: |
|
AjaxCommentsController:: |
protected | property | Service to turn render arrays into HTML strings. | |
AjaxCommentsController:: |
protected | property | The Router service. | |
AjaxCommentsController:: |
protected | property | The TempStore service. | |
AjaxCommentsController:: |
public | function | Builds ajax response for adding a new comment without a parent comment. | |
AjaxCommentsController:: |
protected | function | Add messages to the ajax response. | |
AjaxCommentsController:: |
protected | function | Create an ajax response to replace the comment field. | |
AjaxCommentsController:: |
public | function | Cancel handler for the comment edit form. | |
AjaxCommentsController:: |
public static | function |
Instantiates a new instance of this class. Overrides ControllerBase:: |
|
AjaxCommentsController:: |
public | function | Builds ajax response for deleting a comment. | |
AjaxCommentsController:: |
public | function | Returns the comment edit form. | |
AjaxCommentsController:: |
public static | function | Get the prefix for a selector class for an individual comment. | |
AjaxCommentsController:: |
protected | function | Build a comment field render array for the ajax response. | |
AjaxCommentsController:: |
public | function | Builds ajax response to display a form to reply to another comment. | |
AjaxCommentsController:: |
public | function | Check the user's permission to post a comment. | |
AjaxCommentsController:: |
public | function | Submit handler for the comment reply and edit forms. | |
AjaxCommentsController:: |
public | function | Builds ajax response to save a submitted reply to another comment. | |
AjaxCommentsController:: |
public | function | Builds ajax response for adding a new comment without a parent comment. | |
AjaxCommentsController:: |
public | function | Cancel handler for the cancel form. | |
AjaxCommentsController:: |
public | function | Constructs a AjaxCommentsController object. | |
ControllerBase:: |
protected | property | The configuration factory. | |
ControllerBase:: |
protected | property | The current user service. | 1 |
ControllerBase:: |
protected | property | The entity form builder. | |
ControllerBase:: |
protected | property | The entity manager. | |
ControllerBase:: |
protected | property | The entity type manager. | |
ControllerBase:: |
protected | property | The form builder. | 2 |
ControllerBase:: |
protected | property | The key-value storage. | 1 |
ControllerBase:: |
protected | property | The language manager. | 1 |
ControllerBase:: |
protected | property | The module handler. | 2 |
ControllerBase:: |
protected | property | The state service. | |
ControllerBase:: |
protected | function | Returns the requested cache bin. | |
ControllerBase:: |
protected | function | Retrieves a configuration object. | |
ControllerBase:: |
private | function | Returns the service container. | |
ControllerBase:: |
protected | function | Returns the current user. | 1 |
ControllerBase:: |
protected | function | Retrieves the entity form builder. | |
ControllerBase:: |
protected | function | Retrieves the entity manager service. | |
ControllerBase:: |
protected | function | Retrieves the entity type manager. | |
ControllerBase:: |
protected | function | Returns the form builder service. | 2 |
ControllerBase:: |
protected | function | Returns a key/value storage collection. | 1 |
ControllerBase:: |
protected | function | Returns the language manager service. | 1 |
ControllerBase:: |
protected | function | Returns the module handler. | 2 |
ControllerBase:: |
protected | function |
Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: |
|
ControllerBase:: |
protected | function | Returns the state storage service. | |
LinkGeneratorTrait:: |
protected | property | The link generator. | 1 |
LinkGeneratorTrait:: |
protected | function | Returns the link generator. | |
LinkGeneratorTrait:: |
protected | function | Renders a link to a route given a route name and its parameters. | |
LinkGeneratorTrait:: |
public | function | Sets the link generator service. | |
LoggerChannelTrait:: |
protected | property | The logger channel factory service. | |
LoggerChannelTrait:: |
protected | function | Gets the logger for a specific channel. | |
LoggerChannelTrait:: |
public | function | Injects the logger channel factory. | |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
RedirectDestinationTrait:: |
protected | property | The redirect destination service. | 1 |
RedirectDestinationTrait:: |
protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
RedirectDestinationTrait:: |
protected | function | Returns the redirect destination service. | |
RedirectDestinationTrait:: |
public | function | Sets the redirect destination service. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
UrlGeneratorTrait:: |
protected | property | The url generator. | |
UrlGeneratorTrait:: |
protected | function | Returns the URL generator service. | |
UrlGeneratorTrait:: |
public | function | Sets the URL generator service. | |
UrlGeneratorTrait:: |
protected | function | Generates a URL or path for a specific route based on the given parameters. |