You are here

public function OptionsForm::submitForm in Easychart 8.3

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/OptionsForm.php, line 69
Contains \Drupal\easychart\Form\OptionsForm

Class

OptionsForm

Namespace

Drupal\easychart\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {

  // Merge options with dump.xml
  $current_options = json_decode($form_state
    ->getValue('options'));

  // Get all options.
  $full_options = json_decode(file_get_contents(DRUPAL_ROOT . '/libraries/easychart/src/js/config/dump.json'), TRUE);
  $all_options = array();
  foreach ($full_options as $key => $value) {
    $all_options[$value['fullname']] = $value;
  }

  // Start iterating and find the panes.
  if (!empty($current_options)) {
    foreach ($current_options as $key => $info) {
      if (isset($info->panes)) {
        foreach ($info->panes as $s_key => $s_value) {
          if (isset($s_value->options)) {
            foreach ($s_value->options as $ss_key => $ss_value) {
              if ($ss_value->fullname) {
                $current_options[$key]->panes[$s_key]->options[$ss_key] = (object) array_merge((array) $ss_value, $all_options[$ss_value->fullname]);
              }
            }
          }
        }
      }
    }

    // Write to file.
    file_unmanaged_save_data(json_encode($current_options), 'public://easychart-options.json', FILE_EXISTS_REPLACE);
    drupal_set_message($this
      ->t('The configuration options have been saved.'));
  }
  else {
    drupal_set_message($this
      ->t('Something went wrong saving the options.'));
  }
}