You are here

HiddenLanguageForm.php in Hidden Language 2.x

File

Form/HiddenLanguageForm.php
View source
<?php

namespace Drupal\hidden_language\Form;

use Drupal\Core\Form\ConfigFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\hidden_language\HiddenLanguageManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Class HiddenLanguageForm.
 *
 * @package Drupal\hidden_language\Form
 */
class HiddenLanguageForm extends ConfigFormBase {

  /**
   * Language Manager Object.
   *
   * @var Drupal\hidden_language\HiddenLanguageManager
   */
  private $hiddenLanguageManager;

  /**
   * {@inheritDoc}
   */
  public function __construct(ConfigFactoryInterface $config_factory, HiddenLanguageManager $hiddenLanguageManagerService) {
    parent::__construct($config_factory);
    $this->hiddenLanguageManager = $hiddenLanguageManagerService;
  }

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

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

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $form['route_name'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Route name'),
      '#description' => $this
        ->t('Enter the routes separated by ( , )'),
      '#default_value' => $this->hiddenLanguageManager
        ->decodeArray($this->hiddenLanguageManager
        ->getRoutesException()),
      '#size' => 20,
      '#required' => FALSE,
    ];
    $form['path'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Path'),
      '#description' => $this
        ->t('Enter the paths with ( / ) and separated by ( , )'),
      '#default_value' => $this->hiddenLanguageManager
        ->decodeArray($this->hiddenLanguageManager
        ->getPathsException()),
      '#size' => 20,
      '#required' => FALSE,
    ];
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $this
      ->config('hidden_language.settings')
      ->set('route_name', $this->hiddenLanguageManager
      ->encodeArray($form_state
      ->getValue('route_name')))
      ->set('path', $this->hiddenLanguageManager
      ->encodeArray($form_state
      ->getValue('path')))
      ->save();
    parent::submitForm($form, $form_state);
  }

}

Classes

Namesort descending Description
HiddenLanguageForm Class HiddenLanguageForm.