You are here

LoqateApiKeyConfigForm.php in Loqate 2.x

Same filename and directory in other branches
  1. 8 src/Form/LoqateApiKeyConfigForm.php

Namespace

Drupal\loqate\Form

File

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

namespace Drupal\loqate\Form;

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

/**
 * Class LoqateApiKeyConfigForm.
 */
class LoqateApiKeyConfigForm extends ConfigFormBase {

  /**
   * Config key for the default API key.
   */
  public const DEFAULT_API_KEY = 'loqate_api_key';

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = $this
      ->config('loqate.loqateapikeyconfig');
    $read_more_url = Url::fromUri('https://www.loqate.com/resources/support/setup-guides/advanced-setup-guide/#creating_a_key');
    $description_read_more_link = Link::fromTextAndUrl('Read more about the Loqate API.', $read_more_url)
      ->toString();
    $form[self::DEFAULT_API_KEY] = [
      '#type' => 'key_select',
      '#title' => $this
        ->t('Default Loqate API key'),
      '#description' => $description_read_more_link,
      '#default_value' => $config
        ->get(self::DEFAULT_API_KEY),
    ];
    $doc_link = Link::fromTextAndUrl('Security', Url::fromUri('https://www.loqate.com/resources/support/setup-guides/advanced-setup-guide/#security'));
    $doc_markup = $this
      ->t('<strong>Important:</strong> See Loqate\'s @link section for more details about keeping your Loqate API key secure!', [
      '@link' => $doc_link
        ->toString(),
    ]);
    $form['information'] = [
      '#markup' => '<p>' . $doc_markup . '</p>',
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    parent::submitForm($form, $form_state);
    $this
      ->config('loqate.loqateapikeyconfig')
      ->set(self::DEFAULT_API_KEY, $form_state
      ->getValue(self::DEFAULT_API_KEY))
      ->save();
  }

}

Classes

Namesort descending Description
LoqateApiKeyConfigForm Class LoqateApiKeyConfigForm.