You are here

SettingsForm.php in Sparkpost email 8

Same filename and directory in other branches
  1. 8.2 src/Form/SettingsForm.php

File

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

namespace Drupal\sparkpost\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Class SettingsForm.
 *
 * @package Drupal\sparkpost\Form
 */
class SettingsForm extends ConfigFormBase {

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'sparkpost.settings',
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('sparkpost.settings');
    $form['api_key'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('API Key'),
      '#default_value' => $config
        ->get('api_key'),
    ];
    $form['sender'] = [
      '#type' => 'email',
      '#title' => $this
        ->t('Sender'),
      '#default_value' => $config
        ->get('sender'),
    ];
    $form['sender_name'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Sender Name'),
      '#default_value' => $config
        ->get('sender_name'),
    ];
    return parent::buildForm($form, $form_state);
  }

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

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('sparkpost.settings')
      ->set('api_key', $form_state
      ->getValue('api_key'))
      ->set('sender_name', $form_state
      ->getValue('sender_name'))
      ->set('sender', $form_state
      ->getValue('sender'))
      ->save();
  }

}

Classes

Namesort descending Description
SettingsForm Class SettingsForm.