You are here

class LockrAdvancedForm in Lockr 8.2

Same name and namespace in other branches
  1. 8.4 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

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 12

Namespace

Drupal\lockr\Form
View source
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',
      ]);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LockrAdvancedForm::$drupalRoot protected property @var string
LockrAdvancedForm::$state protected property @var StateInterface
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