pcb-8.x-2.2/pcb.module
pcb.module
<?php
/**
* @file
* Module file for pcb.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Messenger\MessengerInterface;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
*/
function pcb_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.pcb':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('This module allows keeping site performance in check but not deleting caches which are not Drupal caches when doing drush cr. See README in module to get more details.') . '</p>';
return $output;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function pcb_form_system_performance_settings_alter(array &$form, FormStateInterface $form_state) {
$bins = Cache::getBins();
foreach ($bins as $bin => $object) {
if (method_exists($object, 'deleteAllPermanent')) {
$form['clear_cache'][$bin] = [
'#name' => $bin,
'#type' => 'submit',
'#value' => t('Clear permanent cache for @bin', ['@bin' => $bin]),
'#submit' => ['pcb_form_system_performance_settings_submit'],
];
}
}
}
/**
* Submit callback for clearing specific permanent cache bins.
*/
function pcb_form_system_performance_settings_submit(array &$form, FormStateInterface $form_state) {
$element = $form_state->getTriggeringElement();
$bin = $element['#name'] ?? '';
if (empty($bin)) {
return;
}
$messenger = \Drupal::messenger();
try {
\Drupal::service('cache.' . $bin)->deleteAllPermanent();
$messenger->addMessage(t('Flushed cache for @bin.', ['@bin' => $bin]));
}
catch (\Exception $e) {
$messenger->addMessage($e->getMessage(), MessengerInterface::TYPE_ERROR);
}
}
