entity_generic-8.x-3.x-dev/src/Form/GenericDeleteModalForm.php

src/Form/GenericDeleteModalForm.php
<?php

namespace Drupal\entity_generic\Form;

use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\CloseModalDialogCommand;
use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Entity\ContentEntityConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Builds the form to toggle the status of the entity.
 */
class GenericDeleteModalForm extends ContentEntityConfirmFormBase {

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form = parent::buildForm($form, $form_state);

    $form['#prefix'] = '<div id="entity_generic_delete_modal_form_wrapper">';
    $form['#suffix'] = '</div>';

    // Override description.
    $form['description'] = ['#markup' => $this->getQuestion()];

    // Override actions.
    $form['actions']['submit']['#attributes'] = [
      'class' => [
        'use-ajax',
      ],
    ];
    $form['actions']['submit']['#ajax'] = [
      'callback' => [$this, 'submitModalFormAjax'],
      'event' => 'click',
    ];

    unset($form['actions']['cancel']);

    return $form;
  }

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

  /**
   * {@inheritdoc}
   */
  public function getQuestion() {
    return $this->t('Are you sure you want to delete %name?', ['%name' => $this->entity->id()]);
  }

  /**
   * {@inheritdoc}
   */
  public function getConfirmText() {
    return $this->t('Delete');
  }

  /**
   * AJAX callback handler that displays any errors or a success message.
   *
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   *
   * @return \Drupal\Core\Ajax\AjaxResponse
   *   Ajax response that either displays errors or closes the modal.
   */
  public function submitModalFormAjax(array $form, FormStateInterface $form_state) {
    $response = new AjaxResponse();

    // If there are any form errors, re-display the form.
    if ($form_state->hasAnyErrors()) {
      // Show error messages.
      $response->addCommand(new ReplaceCommand('#entity_generic_delete_modal_form_wrapper', $form));

      // Process actions for the submit fail case.
      $this->submitModalAjaxFail($response, $form, $form_state);
    }
    else {
      // Close modal dialog.
      $response->addCommand(new CloseModalDialogCommand());

      // Process actions for the successful submit case.
      $this->submitModalAjaxSuccess($response, $form, $form_state);
    }

    return $response;
  }

  /**
   * AJAX callback handler failed submission.
   *
   * @param \Drupal\Core\Ajax\AjaxResponse $response
   *   Response with failed submission.
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function submitModalAjaxFail(AjaxResponse &$response, array $form, FormStateInterface $form_state) {
    // @todo: implement this method.
  }

  /**
   * AJAX callback handler successful submission.
   *
   * @param \Drupal\Core\Ajax\AjaxResponse $response
   *   Response with success submission.
   * @param array $form
   *   Form array.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   Form state.
   */
  public function submitModalAjaxSuccess(AjaxResponse &$response, array $form, FormStateInterface $form_state) {
    // @todo: implement this method.
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this->entity->delete();
  }

  /**
   * {@inheritdoc}
   */
  public function getCancelUrl() {
    return NULL;
  }

}

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

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