You are here

ConfirmDeleteEditorNoteForm.php in Editor Notes 8

File

src/Form/ConfirmDeleteEditorNoteForm.php
View source
<?php

namespace Drupal\editor_note\Form;

use Drupal\editor_note\EditorNoteHelperService;
use Drupal\editor_note\Entity\EditorNote;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class ConfirmDeleteEditorNoteForm.
 */
class ConfirmDeleteEditorNoteForm extends FormBase {

  /**
   * Editor Note entity.
   *
   * @var \Drupal\editor_note\Entity\EditorNote
   */
  protected $editorNote;

  /**
   * The editor note helper.
   *
   * @var \Drupal\editor_note\EditorNoteHelperService
   */
  protected $editorNoteHelper;

  /**
   * Form constructor.
   *
   * @param \Drupal\editor_note\EditorNoteHelperService $editorNoteHelper
   *   Editor note helpers.
   */
  public function __construct(EditorNoteHelperService $editorNoteHelper) {
    $this->editorNoteHelper = $editorNoteHelper;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('editor_note.helper'));
  }

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'confirm_delete_editor_note_form';
  }

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, EditorNote $editor_note = NULL, $nojs = NULL) {
    $this->editorNote = $editor_note;
    $form['use_ajax_container']['description'] = [
      '#type' => 'item',
      '#markup' => $this
        ->t('Are you sure you want to remove the note? This action cannot be undone.'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Delete'),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'submitModalFormAjax',
        ],
        'event' => 'click',
      ],
    ];
    $form['cancel'] = [
      '#type' => 'button',
      '#value' => $this
        ->t('Cancel'),
      '#attributes' => [
        'class' => [
          'use-ajax',
        ],
      ],
      '#ajax' => [
        'callback' => [
          $this,
          'closeModalForm',
        ],
        'event' => 'click',
      ],
    ];
    return $form;
  }

  /**
   * Ajax callback for the "Submit" button.
   *
   * Remove table row and close modal dialog.
   *
   * @param array $form
   *   The Form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Ajax response.
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    if ($this->editorNote) {
      $replace_command = $this->editorNoteHelper
        ->getWidgetAjaxReplaceCommand($this->editorNote);
      $response
        ->addCommand($replace_command);
    }
    $command = new CloseModalDialogCommand();
    $response
      ->addCommand($command);
    return $response;
  }

  /**
   * Ajax callback for the "Cancel" button.
   *
   * Close modal dialog.
   *
   * @param array $form
   *   The Form.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state object.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Ajax response.
   */
  public function closeModalForm(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();
    $command = new CloseModalDialogCommand();
    $response
      ->addCommand($command);
    return $response;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // Remove Editor Note entity.
    if ($this->editorNote) {
      $this->editorNote
        ->delete();
    }
  }

}

Classes

Namesort descending Description
ConfirmDeleteEditorNoteForm Class ConfirmDeleteEditorNoteForm.