You are here

protected function AmpAnalytics::elementValidateConfigJson in Analytics 8

File

analytics_amp/src/Plugin/AnalyticsService/AmpAnalytics.php, line 90

Class

AmpAnalytics
Provides AMP analytics.

Namespace

Drupal\analytics_amp\Plugin\AnalyticsService

Code

protected function elementValidateConfigJson(array &$element, FormStateInterface $form_state) {
  $value = $element['#value'];
  if ($value == '') {
    return;
  }
  elseif (is_string($value)) {

    // Otherwise attempt to convert the value to JSON.
    $data = json_decode($value, TRUE);
    if (json_last_error()) {
      $form_state
        ->setError($element, $this
        ->t('%name is not valid JSON.', [
        '%name' => $element['#title'],
      ]));
    }
    elseif ($element['#required'] && empty($data)) {
      $form_state
        ->setError($element, $this
        ->t('%name is required.', [
        '%name' => $element['#title'],
      ]));
    }
    else {

      // @todo This should attempt to validate the top-level keys.
      $form_state
        ->setValueForElement($element, $data);
    }
  }
}