editor_note-8.x-1.x-dev/src/Form/ConfirmDeleteEditorNoteForm.php

src/Form/ConfirmDeleteEditorNoteForm.php
<?php

namespace Drupal\editor_note\Form;

use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
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 EditorNote
   */
  protected $editorNote;

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

  /**
   * Form constructor.
   *
   * @param 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 FormStateInterface $form_state
   *   The form state object.
   *
   * @return AjaxResponse
   *   Ajax response.
   * @throws InvalidPluginDefinitionException
   * @throws 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 FormStateInterface $form_state
   *   The form state object.
   *
   * @return 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();
    }
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc