You are here

LockrAdvancedForm.php in Lockr 8.2

Namespace

Drupal\lockr\Form

File

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

namespace Drupal\lockr\Form;

use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\State\StateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class LockrAdvancedForm implements ContainerInjectionInterface, FormInterface {

  /** @var StateInterface */
  protected $state;

  /** @var string */
  protected $drupalRoot;
  public function __construct(StateInterface $state, $drupal_root) {
    $this->state = $state;
    $this->drupalRoot = $drupal_root;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('state'), $container
      ->get('app.root'));
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['fs'] = [
      '#type' => 'details',
      '#title' => 'Advanced',
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
    ];
    $form['fs']['region'] = [
      '#type' => 'radios',
      '#title' => 'Region',
      '#default_value' => $this->state
        ->get('lockr.region', 'us'),
      '#options' => [
        'us' => 'US',
        'eu' => 'EU',
      ],
    ];
    $form['fs']['custom'] = [
      '#type' => 'checkbox',
      '#title' => 'Set custom certificate location',
      '#default_value' => (bool) $this->state
        ->get('lockr.custom', FALSE),
    ];
    $form['fs']['custom_cert'] = [
      '#type' => 'textfield',
      '#title' => 'Certificate path',
      '#default_value' => $this->state
        ->get('lockr.cert'),
      '#states' => [
        'visible' => [
          ':input[name="custom"]' => [
            'checked' => TRUE,
          ],
        ],
      ],
    ];
    $form['fs']['submit'] = [
      '#type' => 'submit',
      '#value' => '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;
    }
    if (substr($cert_path, 0, 10) === 'private://') {
      $private_stream = new PrivateStream();
      $private_stream
        ->setUri($cert_path);
      $cert_path = $private_stream
        ->realpath();
    }
    elseif ($cert_path[0] !== '/') {
      $cert_path = "{$this->drupalRoot}/{$cert_path}";
    }
    if (is_dir($cert_path) || !is_readable($cert_path)) {
      $form_state
        ->setErrorByName('custom_cert', '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');
    $this->state
      ->set('lockr.region', $region);
    $this->state
      ->set('lockr.custom', $custom);
    if ($custom) {
      $this->state
        ->setMultiple([
        'lockr.partner' => 'custom',
        'lockr.cert' => $form_state
          ->getValue('custom_cert'),
      ]);
    }
    else {
      $this->state
        ->deleteMultiple([
        'lockr.partner',
        'lockr.cert',
      ]);
    }
  }

}

Classes

Namesort descending Description
LockrAdvancedForm