ump-8.x-1.0-alpha1/src/Form/UmpRuleDeleteForm.php

src/Form/UmpRuleDeleteForm.php
<?php

namespace Drupal\ump\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Configure ump settings for this site.
 */
class UmpRuleDeleteForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $rule_key = NULL) {
    if ($rule_key != NULL && \Drupal::config('ump.settings')->get($rule_key) != NULL) {
      $form_state->setStorage(['rule_key' => $rule_key]);
    }
    else {
      return;
    }

    $form['actions']['submit'] = [
      '#type' => 'submit',
      '#storage' => 'delete',
      '#value' => $this->t('Delete'),
    ];
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#storage' => 'cancel',
      '#value' => $this->t('Cancel'),
    ];

    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $triggering_element = $form_state->getTriggeringElement();
    if ($triggering_element['#storage'] == "delete") {
      $storage = $form_state->getStorage();
      if (isset($storage['rule_key'])) {
        $rule_key = $storage['rule_key'];
        \Drupal::service('config.factory')->getEditable('ump.settings')->clear($rule_key)->save();
      }
    }
    $form_state->setRedirect('ump.settings');
    // Rebuild the router.
    \Drupal::service('router.builder')->rebuild();
  }

}

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

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