You are here

function analytics_amp_element_validate_config_json in Analytics 7

1 string reference to 'analytics_amp_element_validate_config_json'
AmpAnalyticsService::buildConfigurationForm in analytics_amp/src/AmpAnalyticsService.php
Form constructor.

File

analytics_amp/analytics_amp.module, line 35

Code

function analytics_amp_element_validate_config_json($element, &$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_error($element, t('%name is not valid JSON.', array(
        '%name' => $element['#title'],
      )));
    }
    elseif ($element['#required'] && empty($data)) {
      form_error($element, t('%name is required.', array(
        '%name' => $element['#title'],
      )));
    }
    else {

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