You are here

StepTwoForm.php in TacJS 8

Same filename and directory in other branches
  1. 8.2 src/Form/Steps/StepTwoForm.php

File

src/Form/Steps/StepTwoForm.php
View source
<?php

namespace Drupal\tacjs\Form\Steps;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;

/**
 * Class StepOneForm.
 *
 * @package Drupal\tacjs\Form
 */
class StepTwoForm extends FormBase {

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

  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {
    $config = \Drupal::getContainer()
      ->get('config.factory')
      ->getEditable('tacjs.admin_settings_form');
    $data = \Drupal::service('tacjs.settings')
      ->getFieldsSelects();
    foreach ($data as $key => $item) {
      if (!empty($item)) {
        if (!(count($item) == 1 && $item[0]['value'] == 'hidden')) {
          $form[$key] = [
            '#type' => 'details',
            '#title' => 'Configuration Global Tarte au Citron ' . $key,
          ];
        }
        foreach ($item as $k => $v) {
          if ($v['value'] != 'hidden') {
            $form[$key][$v['value']] = [
              '#type' => 'textfield',
              '#title' => $v['name'],
              '#default_value' => $config
                ->get($v['value']),
              '#description' => !empty($v['description']) ? $config
                ->get($v['description']) : "",
            ];
            if (isset($v['value_'])) {
              $form[$key][$v['value_']] = [
                '#type' => 'textfield',
                '#title' => $v['value_'],
                '#default_value' => $config
                  ->get($v['value_']),
              ];
            }
          }
        }
      }
    }
    $form['actions']['previous'] = [
      '#type' => 'link',
      '#title' => $this
        ->t('Previous'),
      '#attributes' => [
        'class' => [
          'button',
        ],
      ],
      '#weight' => 0,
      '#url' => Url::fromRoute('tacjs.admin_settings_form'),
    ];
    $form['submit'] = [
      '#type' => 'submit',
      '#value' => t('Save'),
    ];
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $config = \Drupal::getContainer()
      ->get('config.factory')
      ->getEditable('tacjs.admin_settings_form');
    $data = \Drupal::service('tacjs.settings')
      ->cleanValues($form_state
      ->getValues());

    // Save Values.
    foreach ($data as $k => $v) {
      $config
        ->set($k, $v);
    }
    $config
      ->save();

    // Redirect to step one.
    $form_state
      ->setRedirect('tacjs.admin_settings_form');
  }

}

Classes

Namesort descending Description
StepTwoForm Class StepOneForm.