function abjs_page_attachments in A/B Test JS 8
Same name and namespace in other branches
- 2.0.x abjs.module \abjs_page_attachments()
Implements hook_page_attachments().
File
- ./
abjs.module, line 26 - Write test JavaScript.
Code
function abjs_page_attachments(array &$page) {
if (\Drupal::service('router.admin_context')
->isAdminRoute()) {
return;
}
// Get all active tests that have at least one experience.
$tests = Database::getConnection()
->query("SELECT DISTINCT t.tid FROM {abjs_test} AS t LEFT JOIN {abjs_test_condition} AS tc ON t.tid = tc.tid INNER JOIN {abjs_test_experience} AS te ON t.tid = te.tid WHERE t.active=1")
->fetchAll();
// At least one test with at least one condition and at least one experience.
// must be active for any script to be added to the page. Because of this,
// we don't need a similar check later for conditions and experiences
// individually.
if (empty($tests)) {
return;
}
// The following section prints out javascript objects for the active tests,
// conditions, and experiences.
$abjs_script = abjs_generate_js($tests);
$page['#attached']['html_head'][] = [
[
'#tag' => 'script',
'#value' => Markup::create($abjs_script),
'#weight' => -100,
],
'abjs_script',
];
}