AjaxCommentsDeleteForm.php in AJAX Comments 8
File
src/Form/AjaxCommentsDeleteForm.php
View source
<?php
namespace Drupal\ajax_comments\Form;
use Drupal\ajax_comments\TempStore;
use Drupal\ajax_comments\Utility;
use Drupal\comment\CommentInterface;
use Drupal\comment\Form\DeleteForm;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class AjaxCommentsDeleteForm extends DeleteForm {
protected $tempStore;
public function __construct(EntityRepositoryInterface $entity_repository, EntityTypeBundleInfoInterface $entity_type_bundle_info, TimeInterface $time, TempStore $temp_store) {
parent::__construct($entity_repository, $entity_type_bundle_info, $time);
$this->tempStore = $temp_store;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('entity.repository'), $container
->get('entity_type.bundle.info'), $container
->get('datetime.time'), $container
->get('ajax_comments.temp_store'));
}
public function buildForm(array $form, FormStateInterface $form_state, CommentInterface $comment = NULL) {
$form = parent::buildForm($form, $form_state);
$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) {
if (empty($comment)) {
$comment = $form_state
->getFormObject()
->getEntity();
}
$this->tempStore
->getSelectors($request, $overwrite = TRUE);
$wrapper_html_id = $this->tempStore
->getSelectorValue($request, 'wrapper_html_id');
$form['wrapper_html_id'] = [
'#type' => 'hidden',
'#value' => $wrapper_html_id,
];
$form['#attributes']['class'][] = 'ajax-comments';
$form['actions']['cancel']['#attributes']['class'][] = 'dialog-cancel';
$form['actions']['submit']['#ajax'] = [
'url' => Url::fromRoute('ajax_comments.delete', [
'comment' => $comment
->id(),
]),
'wrapper' => $wrapper_html_id,
'method' => 'replace',
'effect' => 'fade',
];
}
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
parent::submitForm($form, $form_state);
$request = $this->requestStack
->getCurrentRequest();
if (Utility::isAjaxRequest($request)) {
$form_state
->disableRedirect();
}
}
}