Newer
Older
<?php
/**
* @file
* Requirements page for Font Awesome.
*/
Daniel Moberly
committed
Daniel Moberly
committed
use Drupal\Core\Link;
/**
* Implements hook_requirements().
*/
function fontawesome_requirements($phase) {
Daniel Moberly
committed
$requirements = [];
// Report the version of Font Awesome.
if ($phase == 'runtime') {
Daniel Moberly
committed
$requirements['fontawesome'] = [
'title' => t('Font Awesome'),
Daniel Moberly
committed
];
Mohammad Khamash
committed
Daniel Moberly
committed
// Load the configuration settings.
$configuration_settings = \Drupal::config('fontawesome.settings');
Daniel Moberly
committed
// Check if we are loading assets.
if (!$configuration_settings->get('load_assets')) {
$requirements['fontawesome']['severity'] = REQUIREMENT_OK;
$requirements['fontawesome']['value'] = t('Library being loaded manually');
$requirements['fontawesome']['description'] = t('The Font Awesome module is configured not to load the Font Awesome library. If this is an error, please see the @adminPage and the Font Awesome module README file for more details.', [
'@adminPage' => Link::createFromRoute(t('admin page'), 'fontawesome.admin_settings')->toString(),
]);
}
Daniel Moberly
committed
// Check if Font Awesome is installed.
Daniel Moberly
committed
elseif (fontawesome_check_installed()) {
Daniel Moberly
committed
// Get the version.
if ($configuration_settings->get('method') == 'webfonts') {
Daniel Moberly
committed
$version = t('Webfonts with CSS');
Daniel Moberly
committed
}
elseif ($configuration_settings->get('use_cdn')) {
Daniel Moberly
committed
$version = t('CDN SVG with JS');
Daniel Moberly
committed
}
else {
Daniel Moberly
committed
$version = t('SVG with JS');
Daniel Moberly
committed
}
Daniel Moberly
committed
// First check if we're using everything.
if (is_null($configuration_settings->get('use_solid_file')) === TRUE || ($configuration_settings->get('use_solid_file') && $configuration_settings->get('use_regular_file') && $configuration_settings->get('use_light_file') && $configuration_settings->get('use_brands_file'))) {
// Attach the main library.
$loadedMessages = [
t('All icons loaded'),
];
}
// Else we attach the libraries piecemeal.
else {
$loadedMessages = [];
if ($configuration_settings->get('use_solid_file')) {
$loadedMessages[] = t('Solid icons loaded');
}
if ($configuration_settings->get('use_regular_file')) {
$loadedMessages[] = t('Regular icons loaded');
}
if ($configuration_settings->get('use_light_file')) {
$loadedMessages[] = t('Light icons loaded');
}
if ($configuration_settings->get('use_brands_file')) {
$loadedMessages[] = t('Brands icons loaded');
}
}
Mohammad Khamash
committed
$requirements['fontawesome']['severity'] = REQUIREMENT_OK;
$requirements['fontawesome']['value'] = t('Font Awesome library is installed. Using %version version. (@moreInfoLink)', [
Daniel Moberly
committed
'%version' => $version,
'@moreInfoLink' => Link::createFromRoute(t('more information'), 'fontawesome.admin_settings')->toString(),
]);
Daniel Moberly
committed
$requirements['fontawesome']['description'] = [
'#theme' => 'item_list',
'#items' => $loadedMessages,
'#title' => '',
'#list_type' => 'ul',
'#attributes' => [],
];
Mohammad Khamash
committed
}
else {
Daniel Moberly
committed
$requirements['fontawesome']['severity'] = REQUIREMENT_ERROR;
$requirements['fontawesome']['value'] = t('Not installed');
$requirements['fontawesome']['description'] = t('The Font Awesome library could not be found. If you want this module to handle loading your FontAwesome assets, please verify Font Awesome is installed correctly or that the CDN has been activated and properly configured. Please see the @adminPage and the Font Awesome module README file for more details.', [
'@adminPage' => Link::createFromRoute(t('admin page'), 'fontawesome.admin_settings')->toString(),
Daniel Moberly
committed
]);
Mohammad Khamash
committed
}
/**
* Implements hook_install().
*/
function fontawesome_install() {
_fontawesome_install_grant_default_permissions();
}
/**
* Grants 'access fontawesome additional settings' to authenticated users.
*/
function _fontawesome_install_grant_default_permissions() {
/** @var \Drupal\user\RoleInterface $authenticated_role */
Daniel Moberly
committed
$authenticated_role = \Drupal::entityTypeManager()->getStorage('user_role')->loadOverrideFree('authenticated');
if ($authenticated_role) {
$authenticated_role->grantPermission('access fontawesome additional settings')->save();
}
}
/**
Daniel Moberly
committed
* Implements hook_uninstall().
*/
function fontawesome_uninstall() {
// Delete set variables.
Daniel Moberly
committed
$query = \Drupal::database()->delete('config');
$query->condition('name', 'fontawesome.settings');
$query->execute();
$query = \Drupal::database()->delete('key_value');
$query->condition('name', 'fontawesome');
$query->execute();
Daniel Moberly
committed
// Icon API module : Delete fontawesome icon bundle & clear cache.
if (\Drupal::moduleHandler()->moduleExists('icon') && ($cache = \Drupal::cache()->get('icon_bundles')) && !empty($cache->data)) {
$fa_icon_bundle = isset($cache->data['fontawesome']) ? $cache->data['fontawesome'] : [];
Inder Singh
committed
$fa_icon_bundle['path'] = isset($fa_icon_bundle['path']) ? $fa_icon_bundle['path'] : 'fontawesome';
}
Daniel Moberly
committed
/**
* Add new config for explicitly allowing module to load FontAwesome assets.
*/
function fontawesome_update_8001() {
$config_factory = \Drupal::configFactory();
$config = $config_factory->getEditable('fontawesome.settings');
$config->set('load_assets', TRUE);
$config->save(TRUE);
}
/**
* Grants new 'access fontawesome additional settings' to authenticated users.
*/
function fontawesome_update_8002() {
_fontawesome_install_grant_default_permissions();