public function AmpAnalyticsSettingsForm::buildForm in Accelerated Mobile Pages (AMP) 8.3
Form constructor.
Parameters
array $form: An associative array containing the structure of the form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.
Return value
array The form structure.
Overrides ConfigFormBase::buildForm
File
- src/
Form/ AmpAnalyticsSettingsForm.php, line 31
Class
- AmpAnalyticsSettingsForm
- Defines the configuration export form.
Namespace
Drupal\amp\FormCode
public function buildForm(array $form, FormStateInterface $form_state) {
$config = $this
->config('amp.analytics.settings');
$ampService = \Drupal::service('amp.utilities');
$form['google_analytics'] = [
'#type' => 'details',
'#title' => t('Google Analytics'),
'#open' => !empty($config
->get('google_analytics_id')),
];
$form['google_analytics']['google_analytics_id'] = [
'#type' => 'textfield',
'#default_value' => $config
->get('google_analytics_id'),
'#title' => $this
->t('Google Analytics ID'),
'#description' => '<p>' . $this
->t('Enter a value to add the Google Analytics ' . 'code to your AMP pages. This ID is unique to each site you want to ' . 'track separately, and is in the form of UA-xxxxxxx-yy. To get a Web ' . 'Property ID, <a href=":analytics">register your site with Google ' . 'Analytics</a>, or if you already have registered your site, go to ' . 'your Google Analytics Settings page to see the ID next to every site ' . 'profile. <a href=":webpropertyid">Find more information in the ' . 'documentation</a>.', [
':analytics' => 'http://www.google.com/analytics/',
':webpropertyid' => Url::fromUri('https://developers.google.com/analytics/resources/concepts/gaConceptsAccounts', [
'fragment' => 'webProperty',
])
->toString(),
]) . '</p><p>' . $ampService
->libraryDescription([
'amp/amp.analytics',
]) . '</p>',
'#maxlength' => 20,
'#size' => 15,
'#placeholder' => 'UA-',
];
$form['gtm'] = [
'#type' => 'details',
'#title' => t('Google Tag Manager'),
'#open' => !empty($config
->get('amp_gtm_id')),
];
$form['gtm']['amp_gtm_id'] = [
'#type' => 'textfield',
'#title' => $this
->t('The Google Tag Manager ID'),
'#default_value' => $config
->get('amp_gtm_id'),
'#description' => '<p>' . $this
->t('Enter a value to add the Google Tag Manager ' . 'code to your AMP pages. This is the Google Tag Manager ID for the ' . 'site owner. Get this in your <a href=":url">Google Tag Manager</a> ' . 'account. GTM has built-in AMP functionality. Go to the GTM ' . 'administration page, enter an account (your business) and container ' . '(the site url), select AMP, and click Create. Pull out the ' . 'id from that code (it looks like GTM-xxxxxxx) and paste it here.', [
':url' => Url::fromUri('https://tagmanager.google.com')
->toString(),
]) . '</p><p>' . $ampService
->libraryDescription([
'amp/amp.analytics',
]) . '</p>',
'#maxlength' => 20,
'#size' => 15,
'#placeholder' => 'GTM-',
];
$form['pixel'] = [
'#type' => 'details',
'#title' => t('amp-pixel'),
'#open' => !empty($config
->get('amp_pixel')),
];
$form['pixel']['amp_pixel'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Enable amp-pixel'),
'#default_value' => $config
->get('amp_pixel'),
'#description' => $ampService
->libraryDescription([
'amp/amp.pixel',
]),
];
$form['pixel']['amp_pixel_domain_name'] = [
'#type' => 'textfield',
'#title' => $this
->t('amp-pixel domain name'),
'#default_value' => $config
->get('amp_pixel_domain_name'),
'#description' => $this
->t('The domain name where the tracking pixel will be loaded: do not include http or https.'),
'#states' => [
'visible' => [
':input[name="amp_pixel"]' => [
'checked' => TRUE,
],
],
],
];
$form['pixel']['amp_pixel_query_string'] = [
'#type' => 'textfield',
'#title' => $this
->t('amp-pixel query path'),
'#default_value' => $config
->get('amp_pixel_query_string'),
'#description' => $this
->t('The path at the domain where the GET request will be received, e.g. "pixel" in example.com/pixel?RANDOM.'),
'#states' => [
'visible' => [
':input[name="amp_pixel"]' => [
'checked' => TRUE,
],
],
],
];
$form['pixel']['amp_pixel_random_number'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Random number'),
'#default_value' => $config
->get('amp_pixel_random_number'),
'#description' => $this
->t('Use the special string RANDOM to add a random number to the URL if required. Find more information in the <a href="https://github.com/ampproject/amphtml/blob/master/spec/amp-var-substitutions.md#random">amp-pixel documentation</a>.'),
'#states' => [
'visible' => [
':input[name="amp_pixel"]' => [
'checked' => TRUE,
],
],
],
];
return parent::buildForm($form, $form_state);
}