Skip to content

Commit

Permalink
Add async + defer attribute in config form
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Kohl committed Jan 19, 2022
1 parent 77ab69e commit 03095db
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
6 changes: 4 additions & 2 deletions liveramp_integration.module
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ use Drupal\Core\Render\Markup;
function liveramp_integration_page_attachments(array &$page) {
$config = \Drupal::config('liveramp_integration.configuration');
$appId = $config->get('app_id');
$async = $config->get('async_mode');
$defer = $config->get('defer_mode');

$disableConsent = \Drupal::request()->query->get('disable_gdpr_consent');
$isDisabled = (1 === (int) $disableConsent) ? TRUE : FALSE;
Expand All @@ -35,8 +37,8 @@ function liveramp_integration_page_attachments(array &$page) {
'#tag' => 'script',
'#attributes' => [
'src' => 'https://gdpr-wrapper.privacymanager.io/gdpr/' . $appId . '/gdpr-liveramp.js',
'async' => TRUE,
'defer' => TRUE,
'async' => ($async) ? TRUE : FALSE,
'defer' => ($defer) ? TRUE : FALSE,
],
'#weight' => -1,
],
Expand Down
14 changes: 14 additions & 0 deletions src/Form/LiveRampIntegrationConfigForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,18 @@ public function buildForm(array $form, FormStateInterface $form_state) {
];
}

$form['consent']['async_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use script async attribute.'),
'#default_value' => $config->get('async_mode'),
];

$form['consent']['defer_mode'] = [
'#type' => 'checkbox',
'#title' => $this->t('Use script defer attribute.'),
'#default_value' => $config->get('defer_mode'),
];

$form['consent']['actions']['submit'] = [
'#type' => 'submit',
'#value' => $this->t('Save configuration'),
Expand All @@ -139,6 +151,8 @@ public function submitForm(array &$form, FormStateInterface $form_state) {

$config->set('app_id', $formData['field_app_id']);
$config->set('vendor_ids', $formData['field_vendor_id']['values']);
$config->set('async_mode', $formData['async_mode']);
$config->set('defer_mode', $formData['defer_mode']);

$config->save();
}
Expand Down

0 comments on commit 03095db

Please sign in to comment.