You are here

analytics_amp.module in Analytics 7

File

analytics_amp/analytics_amp.module
View source
<?php

/**
 * Implements hook_analytics_service_info().
 */
function analytics_amp_analytics_service_info() {
  $info['amp-analytics'] = array(
    'label' => t('AMP'),
    'class' => 'AmpAnalyticsService',
    'multiple' => TRUE,
  );
  $info['amp-pixel'] = array(
    'label' => t('AMP Tracking Pixel'),
    'class' => 'AmpTrackingPixelAnalyticsService',
    'multiple' => TRUE,
  );
  return $info;
}
function analytics_amp_element_validate_config_url($element, &$form_state) {
  $value = $element['#value'];
  if ($value == '') {
    return;
  }
  elseif (file_valid_uri($value)) {

    // Allow file URIs like public:://config.json
    return;
  }
  else {
    elements_validate_url($element, $form_state);
  }
}
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);
    }
  }
}