bs_lib-8.x-1.0-alpha3/bs_lib.install
bs_lib.install
<?php
/**
* @file
* Installation and update functions for the BS Lib module.
*/
/**
* Implements hook_requirements().
*/
function bs_lib_requirements($phase) {
$requirements = [];
if ($phase != 'runtime') {
return $requirements;
}
// Check that Bootstrap library exist and it is of correct type.
// Bootstrap 4 library check.
$severity = REQUIREMENT_OK;
$value = t('Installed');
$description = '';
if (!bs_lib_library_resource_exist('bootstrap', 'dist/js/bootstrap.min.js')) {
$severity = REQUIREMENT_ERROR;
$value = t('Not installed');
$description = t('The <a href=":url">Bootstrap 4 library</a> should be installed in <strong>/libraries</strong> folder. See README.md in bs_lib module for additional instructions.', [':url' => 'https://github.com/twbs/bootstrap/releases']);
}
else {
// Check that library type is source code and not distribution.
if (!bs_lib_library_resource_exist('bootstrap', 'js/dist/alert.js')) {
$severity = REQUIREMENT_ERROR;
$value = t('Installed but type is not a source code');
$description = t('The <a href=":url">Bootstrap 4 library</a> is installed in <strong>/libraries</strong> folder. However the type of library is wrong, please make sure to download source code version of a library. See README.md in <em>bs_lib</em> module for additional instructions.', [':url' => 'https://github.com/twbs/bootstrap/releases']);
}
}
$requirements['bs_lib_library'] = [
'title' => t('BS Lib: Bootstrap 4 library'),
'description' => $description,
'severity' => $severity,
'value' => $value,
];
// Check that Popper.js library exist.
if (!bs_lib_library_resource_exist('popper', 'dist/umd/popper.min.js')) {
$requirements['bs_lib_popper_library'] = [
'title' => t('BS Lib: Popper.js library'),
'description' => t('The <a href=":url">Popper.js library</a> should be installed in <strong>/libraries</strong> folder. See README.md in bs_lib module for additional instructions.', [':url' => 'https://github.com/FezVrasta/popper.js/releases']),
'severity' => REQUIREMENT_ERROR,
'value' => t('Not installed'),
];
}
else {
$requirements['bs_lib_popper_library'] = [
'title' => t('BS Lib: Popper.js library'),
'description' => '',
'severity' => REQUIREMENT_OK,
'value' => t('Installed'),
];
}
// Check that Jasny Bootstrap library exist.
if (!bs_lib_library_resource_exist('jasny-bootstrap', 'js/offcanvas.js')) {
$requirements['bs_lib_jasny_bootstrap_library'] = [
'title' => t('BS Lib: Jasny Bootstrap library'),
'description' => t('The <a href=":url">Jasny Bootstrap library</a> should be installed in <strong>/libraries</strong> folder. You will not be able to use offcanvas navigation bar feature. See README.md in bs_lib module for additional instructions.', [':url' => 'https://github.com/jasny/bootstrap/releases/']),
'severity' => REQUIREMENT_WARNING,
'value' => t('Not installed'),
];
}
else {
$requirements['bs_lib_jasny_bootstrap_library'] = [
'title' => t('BS Lib: Jasny Bootstrap library'),
'description' => '',
'severity' => REQUIREMENT_OK,
'value' => t('Installed'),
];
}
return $requirements;
}
/**
* Check does resource exist in a given library.
*
* @param string $name
* Library name.
* @param string $resource_path
* Resource path in a library.
*
* @return bool true if the resource exist and is a regular file, false
* otherwise.
*/
function bs_lib_library_resource_exist($name, $resource_path) {
if (\Drupal::hasService('library.libraries_directory_file_finder')) {
/** @var \Drupal\Core\Asset\LibrariesDirectoryFileFinder $library_file_finder */
$library_file_finder = \Drupal::service('library.libraries_directory_file_finder');
$path = $library_file_finder->find($name . '/' . $resource_path);
}
// @todo remove libraries support once we require Drupal Core 8.9+.
elseif (\Drupal::moduleHandler()->moduleExists('libraries')) {
$path = libraries_get_path($name) . '/' . $resource_path;
}
else {
$path = DRUPAL_ROOT . "/libraries/$name/$resource_path";
}
return is_file($path);
}
/**
* Add new bs_lib.settings anchor_scroll to active configuration.
*/
function bs_lib_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('bs_lib.settings');
$config->set('anchor_scroll', [
'enable' => FALSE,
'offset' => 10,
'exclude_links' => ['[href="#"]', '.visually-hidden', '.bs-lib-scroll-to-top'],
'fixed_elements' => ['.toolbar-bar', '.toolbar-tray.is-active.toolbar-tray-horizontal'],
]);
$config->save(TRUE);
}
/**
* Deprecate bs_bootstrap theme and merge/rename enabled theme settings.
*/
function bs_lib_update_8002() {
$enabled_themes = \Drupal::service('theme_handler')->listInfo();
foreach ($enabled_themes as $thene_name => $theme) {
$theme_config = \Drupal::configFactory()->getEditable($thene_name . '.settings');
// If bs_bootstrap configuration exists, we need to merge it to bs_base.
$bs_bootstrap_config = $theme_config->get('bs_bootstrap');
if (!empty($bs_bootstrap_config)) {
$bs_base_config = $theme_config->get('bs_base');
// If bs_base doesn't exist, create it from bs_bootstrap.
if (empty($bs_base_config)) {
$bs_base_config = $bs_bootstrap_config;
}
// Otherwise, merge bs_bootstrap into bs_base (bs_bootstrap values override
// bs_base).
else {
$bs_base_config = array_merge($bs_base_config, $bs_bootstrap_config);
}
$theme_config->set('bs_base', $bs_base_config);
$theme_config->clear('bs_bootstrap');
$theme_config->save(TRUE);
}
}
}
