function optimizely_install in Optimizely 8
Same name and namespace in other branches
- 8.3 optimizely.install \optimizely_install()
- 8.0 optimizely.install \optimizely_install()
- 7.3 optimizely.install \optimizely_install()
- 7.2 optimizely.install \optimizely_install()
Implements hook_install().
Included in the process of adding a optimizely database table is the creation of a default project entry in the table. The default entry is used to add an initial javascript file (snippit) on a sitewide basis. The Optimizely account ID will need to be entered in the account setup page to complete the default entry. The Optimizely site uses the account ID to generate the basic javascipt file to be included on the site. Once additional projects / experiments are created on the Optimizely site additional project entries can be added to load the additional javascript files on specific site paths. Selective loading of the Optimizely javascipt file helps in page load times and the amount of custom Optimizely javascript in each Javascript include file.
File
- ./
optimizely.install, line 94 - Install, update and uninstall functions for the Optimizely module.
Code
function optimizely_install() {
drupal_set_message(t('Optimizely database table has been created.'), 'status');
// Add default entry - check to see if entry already exsists.
$default_entry_exists = (bool) \Drupal::database()
->query("\n\t SELECT\n\t\t project_title\n\t\tFROM\n\t\t {optimizely}\n\t\tWHERE\n\t\t oid = 1\n\t\t")
->fetchField();
if ($default_entry_exists == TRUE) {
drupal_set_message(t('A default entry found in the optimizely database table.
Something funky is going on but it\'s not the end of the world.'), 'warning');
}
else {
// Create default entry.
$default_entry_created = (bool) \Drupal::database()
->insert('optimizely')
->fields([
'project_title' => 'Default',
'include' => 1,
'enabled' => 0,
'path' => serialize([
'*',
]),
'project_code' => 0,
])
->execute();
// Inform the administrator that a default snippet entry has been made.
// Acount ID and access permisisons need to be configured.
if ($default_entry_created == TRUE) {
drupal_set_message(t('A default project / experiment entry has been created.
Next, enter your Optimizely account ID on the module\'s ACCOUNT INFO page.
There is also an Optimizely permission that can be set for specific roles
to access the adminstration functionality.
You can access those pages via the Optimizely module below.'), 'status');
}
else {
drupal_set_message(t('An error was encountered while adding the default project entry
for the Optimizely module.'), 'error');
}
}
// Set the default value for the language code to be English.
$config = \Drupal::configFactory()
->getEditable('optimizely.settings');
$config
->set('langcode', 'en');
$config
->save();
}