You are here

public function AddViewsCustomTable::submitForm in Views Custom Table 8

Same name and namespace in other branches
  1. 9.0.x src/Form/AddViewsCustomTable.php \Drupal\view_custom_table\Form\AddViewsCustomTable::submitForm()

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/AddViewsCustomTable.php, line 234

Class

AddViewsCustomTable
Add views custom table form.

Namespace

Drupal\view_custom_table\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $opeation = $form_state
    ->getValue('op')
    ->render();
  if ($opeation == $this
    ->t('Next')) {
    $this->PreviousStepData = $form_state
      ->cleanValues()
      ->getValues();
    $form_state
      ->setRebuild();
    $this->step++;
  }
  if ($opeation == $this
    ->t('Save')) {
    $user = $this->account;
    $table_name = $this->PreviousStepData['table_name'];
    $table_database = $this->PreviousStepData['table_database'];
    $description = $this->PreviousStepData['description'];
    $relations = $form_state
      ->getValue('columns');
    $column_relations = [];
    foreach ($relations as $relation) {
      if ($relation['entity'] != 'none') {
        $column_relations[$relation['field']] = $relation['entity'];
      }
    }
    $serialize_relations = serialize($column_relations);
    $this->configEditable
      ->set($table_name . '.table_name', $table_name)
      ->set($table_name . '.table_database', $table_database)
      ->set($table_name . '.description', $description)
      ->set($table_name . '.column_relations', $serialize_relations)
      ->set($table_name . '.created_by', $user
      ->id());
    $result = $this->configEditable
      ->save();
    if ($result) {
      drupal_set_message($this
        ->t('@table is added to views. Please clear cache to see changes.', [
        '@table' => $table_name,
      ]));
    }
    else {
      drupal_set_message($this
        ->t('Could not add table to views, please check log messages for error.'), 'error');
    }
  }
  $form_state
    ->setRedirect('view_custom_table.customtable');
}