View source
<?php
namespace Drupal\analytics_amp\Plugin\AnalyticsService;
use Drupal\analytics\Plugin\ServicePluginBase;
use Drupal\Component\Utility\UrlHelper;
use Drupal\Core\Form\FormStateInterface;
class AmpAnalytics extends ServicePluginBase {
public function defaultConfiguration() {
return [
'type' => NULL,
'config_url' => '',
'config_json' => [],
];
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['type'] = [
'#title' => t('Type'),
'#type' => 'select',
'#default_value' => $this->configuration['type'],
'#options' => [
'adobeanalytics' => t('Adobe Analytics'),
'googleanalytics' => t('Google Analytics'),
],
'#required' => TRUE,
];
$form['config_url'] = [
'#title' => t('Remote configuration JSON URL'),
'#type' => 'textfield',
'#default_value' => $this->configuration['config_url'],
'#placeholder' => 'https://example.com/analytics.config.json',
];
$form['config_json'] = [
'#title' => t('Inline configuration JSON'),
'#type' => 'textarea',
'#default_value' => $this->configuration['config_json'],
'#description' => t('See the <a href="https://www.ampproject.org/docs/reference/extended/amp-analytics.html">amp-analytics documentation</a> for example configuration values.'),
'#element_validate' => [
[
get_class($this),
'validateJson',
],
],
];
return $form;
}
public function getOutput() {
if (\Drupal::service('router.amp_context')
->isAmpRoute()) {
return $this
->getAmpOutput($this->configuration);
}
}
protected function elementValidateConfigUrl(array &$element, FormStateInterface $form_state) {
$value = $element['#value'];
if ($value == '') {
return;
}
elseif (\Drupal::service('stream_wrapper_manager')
->isValidUri($value)) {
return;
}
elseif (!UrlHelper::isExternal($value)) {
$form_state
->setError($element, $this
->t('Not a valid URL.'));
}
}
protected function elementValidateConfigJson(array &$element, FormStateInterface $form_state) {
$value = $element['#value'];
if ($value == '') {
return;
}
elseif (is_string($value)) {
$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 {
$form_state
->setValueForElement($element, $data);
}
}
}
}