site_audit-8.x-3.0-rc1/site_audit.module
site_audit.module
<?php
/**
* @file
* Contains hooks and functions for the site_audit.module.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function site_audit_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the site_audit module.
case 'help.page.site_audit':
$output = '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Site Audit is a Drupal site analysis platform that generates reports with actionable best practice recommendations.') . '</p>';
return $output;
default:
}
}
/**
* Determine if in a development environment.
*
* @return bool
* Whether the site is in a development environment.
*/
function site_audit_env_is_dev() {
// Acquia.
if (defined('AH_SITE_ENVIRONMENT')) {
return !in_array(PANTHEON_ENVIRONMENT, ['test', 'prod']);
}
// Pantheon.
if (defined('PANTHEON_ENVIRONMENT')) {
return !in_array(PANTHEON_ENVIRONMENT, ['test', 'live']);
}
return FALSE;
}
if (!function_exists('human_filesize')) {
/**
* create the human readable file size
* @see https://gist.github.com/liunian/9338301
*
* @param $size
* @param int $precision
* @return string
*/
function human_filesize($size, $precision = 2) {
static $units = array('B','KB','MB','GB','TB','PB','EB','ZB','YB');
$step = 1024;
$i = 0;
while (($size / $step) > 0.9) {
$size = $size / $step;
$i++;
}
return round($size, $precision) . ' ' . $units[$i];
}
}
/**
* Form alter hook for site.module's settings page.
*
* Tells site.module to use Site Audit reports for Site State.
*
* @param $form
* @return void
*/
function site_audit_form_site_entity_settings_alter(&$form, FormStateInterface $form_state)
{
$form['state']['state_factors']['#options']['site_audit'] = t('Site Audit Reports <a href=":url">view</a>', [
':url' => '/admin/reports/site-audit',
]);
$audit_manager = \Drupal::service('plugin.manager.site_audit_checklist');
$site_settings = \Drupal::config('site.settings');
$checklists = $audit_manager->getDefinitions();
foreach ($checklists as $checklist) {
$options[$checklist['id']] = $checklist['name'];
}
$form['state']['site_audit_required'] = [
'#type' => 'checkboxes',
'#title' => t('Required Site Audit Checklists'),
'#description' => t('Choose the Reports that must pass 100% to to put the site in OK state.'),
'#options' => $options,
'#default_value' => $site_settings->get('site_audit_required') ?? [],
'#states' => [
'visible' => [
':input[name="state_factors[site_audit]"]' => ['checked' => true]
]
],
];
$form['#submit'][] = 'site_audit_site_entity_settings_save';
}
/**
* Entity builder for the commerce_product_attribute config entity.
*/
function site_audit_site_entity_settings_save(array &$form, FormStateInterface $form_state) {
\Drupal::configFactory()->getEditable('site.settings')
->set('site_audit_required', $form_state->getValue('site_audit_required'))
->save()
;
}
