You are here

class LockrAdvancedForm in Lockr 8.4

Same name and namespace in other branches
  1. 8.2 src/Form/LockrAdvancedForm.php \Drupal\lockr\Form\LockrAdvancedForm
  2. 8.3 src/Form/LockrAdvancedForm.php \Drupal\lockr\Form\LockrAdvancedForm
  3. 4.x src/Form/LockrAdvancedForm.php \Drupal\lockr\Form\LockrAdvancedForm

Form handler for advanced Lockr settings.

Hierarchy

Expanded class hierarchy of LockrAdvancedForm

1 file declares its use of LockrAdvancedForm
LockrAdminController.php in src/Controller/LockrAdminController.php

File

src/Form/LockrAdvancedForm.php, line 18

Namespace

Drupal\lockr\Form
View source
class LockrAdvancedForm implements ContainerInjectionInterface, FormInterface {
  use StringTranslationTrait;

  /**
   * Simple config factory.
   *
   * @var ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Drupal file system interface.
   *
   * @var FileSystemInterface
   */
  protected $fileSystem;

  /**
   * Constructs a new LockrAdvancedForm.
   *
   * @param ConfigFactoryInterface $config_factory
   *   The simple config factory.
   * @param FileSystemInterface $file_system
   *   The Drupal file system interface.
   * @param TranslationInterface $translation
   *   The Drupal translator.
   */
  public function __construct(ConfigFactoryInterface $config_factory, FileSystemInterface $file_system, TranslationInterface $translation) {
    $this->configFactory = $config_factory;
    $this->fileSystem = $file_system;
    $this
      ->setStringTranslation($translation);
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('config.factory'), $container
      ->get('file_system'), $container
      ->get('string_translation'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['fs'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Advanced'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $config = $this->configFactory
      ->get('lockr.settings');
    $form['fs']['region'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Region'),
      '#default_value' => $config
        ->get('region'),
      '#empty_option' => $this
        ->t('- Nearest -'),
      '#options' => [
        'us' => 'US',
        'eu' => 'EU',
      ],
    ];
    $form['fs']['custom'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Set custom certificate location'),
      '#default_value' => $config
        ->get('custom', FALSE),
    ];
    $form['fs']['custom_cert'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('Certificate path'),
      '#default_value' => $config
        ->get('cert_path'),
      '#states' => [
        'visible' => [
          ':input[name="custom"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['fs']['submit'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Save'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state
      ->getValue('custom')) {
      return;
    }
    $cert_path = $form_state
      ->getValue('custom_cert');
    if (!$cert_path) {
      $form_state
        ->setErrorByName('custom_cert', $this
        ->t('Certificate location is required for custom certs'));
      return;
    }
    $real_cert_path = $this->fileSystem
      ->realpath($cert_path);
    if (is_dir($real_cert_path) || !is_readable($real_cert_path)) {
      $form_state
        ->setErrorByName('custom_cert', $this
        ->t('Certificate must be a readable file'));
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $custom = $form_state
      ->getValue('custom');
    $region = $form_state
      ->getValue('region');
    $config = $this->configFactory
      ->getEditable('lockr.settings');
    if ($region) {
      $config
        ->set('region', $region);
    }
    else {
      $config
        ->clear('region');
    }
    $config
      ->set('custom', $custom);
    if ($custom) {
      $config
        ->set('cert_path', $form_state
        ->getValue('custom_cert'));
    }
    else {
      $config
        ->clear('custom_cert');
    }
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrAdvancedForm::$configFactory protected property Simple config factory.
LockrAdvancedForm::$fileSystem protected property Drupal file system interface.
LockrAdvancedForm::buildForm public function Form constructor. Overrides FormInterface::buildForm
LockrAdvancedForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
LockrAdvancedForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
LockrAdvancedForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
LockrAdvancedForm::validateForm public function Form validation handler. Overrides FormInterface::validateForm
LockrAdvancedForm::__construct public function Constructs a new LockrAdvancedForm.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.