Commit 13e60240 authored by juanl's avatar juanl Committed by Patrick Kannekens
Browse files

Issue #2820545 by petermallett, juanl: JSON configuration breaks config-export

parent 4576da06
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@

namespace Drupal\easychart\Form;

use Drupal\Component\Serialization\Json;
use Drupal\Core\Form\ConfigFormBase;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Form\FormStateInterface;
@@ -37,11 +38,15 @@ class TemplatesForm extends ConfigFormBase {
    $form['#attached']['library'][] = 'easychart/lib.easycharts.full';
    $form['#attached']['library'][] = 'easychart/lib.highcharts';

    $default = $config->get('templates');
    if (!empty($default)) {
      $default = json_encode(Json::decode($config->get('templates')), JSON_PRETTY_PRINT);
    }
    $form['templates'] = [
      '#type' => 'textarea',
      '#title' => $this->t('Templates'),
      '#title_display' => 'invisible',
      '#default_value' => $config->get('templates'),
      '#default_value' => $default,
      '#description' => $this->t("These templates will be selectable in the Easychart interface when creating/editing a chart."),
      '#attributes' => ['class' => ['easychart-templates']],
      '#rows' => 30,
@@ -58,13 +63,29 @@ class TemplatesForm extends ConfigFormBase {
    return parent::buildForm($form, $form_state);
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {
    $templates = $form_state->getValue('templates');
    if (!empty($templates)) {
      $json = Json::decode($templates);
      if ($json === NULL) {
        $form_state->setError($form['templates'], 'The templates value must be valid JSON.');
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state->getValues();
    $templates = $form_state->getValue('templates');
    if (!empty($templates)) {
      $templates =Json::encode(Json::decode($templates));
    }
    $this->config('easychart.settings')
      ->set('templates', $values['templates'])
      ->set('templates', $templates)
      ->save();
  }