public function AjaxCommentsDeleteForm::buildForm in AJAX Comments 8
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ContentEntityDeleteForm::buildForm
File
- src/
Form/ AjaxCommentsDeleteForm.php, line 64
Class
- AjaxCommentsDeleteForm
- Provides ajax enhancements to core Comment delete form.
Namespace
Drupal\ajax_comments\FormCode
public function buildForm(array $form, FormStateInterface $form_state, CommentInterface $comment = NULL) {
$form = parent::buildForm($form, $form_state);
// Check if this form is being loaded through ajax or as a modal dialog.
$request = $this->requestStack
->getCurrentRequest();
$is_ajax_request = Utility::isAjaxRequest($request, $form_state
->getUserInput());
$is_modal_request = Utility::isModalRequest($request);
if ($is_modal_request || $is_ajax_request) {
// In some circumstances the $comment object needs to be initialized.
if (empty($comment)) {
$comment = $form_state
->getFormObject()
->getEntity();
}
// Get the selectors from the request.
$this->tempStore
->getSelectors($request, $overwrite = TRUE);
$wrapper_html_id = $this->tempStore
->getSelectorValue($request, 'wrapper_html_id');
// Add the wrapping fields's HTML id as a hidden input
// so we can access it in the controller.
$form['wrapper_html_id'] = [
'#type' => 'hidden',
'#value' => $wrapper_html_id,
];
// Add a class to target this form in JavaScript.
$form['#attributes']['class'][] = 'ajax-comments';
// Add a class to the cancel button to trigger modal dialog close.
$form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
// Set up this form to ajax submit so that we aren't redirected to
// another page upon clicking the 'Delete' button.
$form['actions']['submit']['#ajax'] = [
'url' => Url::fromRoute('ajax_comments.delete', [
'comment' => $comment
->id(),
]),
'wrapper' => $wrapper_html_id,
'method' => 'replace',
'effect' => 'fade',
];
}
return $form;
}