cacheflush_advanced.module in CacheFlush 8
Same filename and directory in other branches
Cacheflush Advanced module.
File
modules/cacheflush_advanced/cacheflush_advanced.moduleView source
<?php
/**
* @file
* Cacheflush Advanced module.
*/
use Drupal\Core\Cache\Cache;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_cacheflush_ui_tabs().
*/
function cacheflush_advanced_cacheflush_ui_tabs() {
$tabs = [
'vertical_tabs_advance' => [
'name' => t('Custom (advanced)'),
'validation' => 'cacheflush_advanced_tab_validation',
'weight' => -50,
],
];
return $tabs;
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function cacheflush_advanced_form_cacheflush_add_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_cacheflush_advanced_form($form, $form_state);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function cacheflush_advanced_form_cacheflush_edit_form_alter(&$form, FormStateInterface $form_state, $form_id) {
_cacheflush_advanced_form($form, $form_state, 'edit');
}
/**
* Form alter callback for add/edit cacheflush entity.
*/
function _cacheflush_advanced_form(&$form, &$form_state, $op = 'add') {
$form['vertical_tabs_advance']['cacheflush_advanced_table'] = [
'#type' => 'table',
'#header' => [
t('Cache ID'),
t('Service'),
t('Operations'),
],
'#attributes' => [
'id' => 'cacheflush-advanced-settings-table',
],
];
// Add new row button.
$form['vertical_tabs_advance']['advance_add'] = [
'#name' => 'advance_add',
'#type' => 'button',
'#value' => t('Add another row'),
'#ajax' => [
'callback' => '_cacheflush_advanced_form_callback',
'wrapper' => 'cacheflush-advanced-settings-table',
'method' => 'replace',
'effect' => 'fade',
],
];
$form['vertical_tabs_advance']['note'] = [
'#type' => 'item',
'#title' => t('Note'),
'#weight' => -10,
'#description' => t('Create custom preset to clear cache by "cid".') . '<ul><li>' . t('<b>Cache ID ($cid)</b>: The cache ID to delete.') . '</li><li>' . t('<b>Bin</b>: Cache service.') . '</li></ul>',
];
$trigger = $form_state
->getTriggeringElement();
if (isset($trigger)) {
$table_elements = $form_state
->getValues()['vertical_tabs_advance']['cacheflush_advanced_table'];
// Check if Remove row and remove.
if (strpos($trigger['#name'], 'advanced_remove_') === 0) {
$remove_id = explode('advanced_remove_', $trigger['#name'])[1];
unset($table_elements[$remove_id]);
}
// ReCreat table.
if (isset($table_elements)) {
foreach ($table_elements as $key => $value) {
_cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $key, $value['cid'], $value['table']);
}
}
// Check if Add row and add 1 more.
if ($trigger['#name'] == 'advance_add') {
$id = 1;
if (isset($table_elements)) {
$id = max(array_keys($table_elements)) + 1;
}
_cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $id);
}
}
else {
if ($op == 'add') {
_cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], 0);
}
else {
$data = $form_state
->getStorage()['data'];
if (isset($data['advanced'])) {
$container = \Drupal::getContainer();
$bins = $container
->getParameter('cache_bins');
foreach ($data['advanced']['functions'] as $key => $value) {
_cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], $key, $value['#params'][2], $bins[$value['#params'][0]]);
}
}
else {
_cacheflush_advanced_table_row($form['vertical_tabs_advance']['cacheflush_advanced_table'], 0);
}
}
}
}
/**
* Build cacheflush custom settings row form elements.
*
* @param array $element
* Form element.
* @param int $delta
* Row weight.
* @param string $cid
* Cache id to delete.
* @param string $bin
* Cache service.
*
* @staticvar type $table_options
* Cache services options.
*/
function _cacheflush_advanced_table_row(array &$element, $delta, $cid = NULL, $bin = NULL) {
static $table_options;
if (empty($table_options)) {
$module_handler = \Drupal::moduleHandler();
$module_handler
->invokeAll('cache_flush');
foreach (Cache::getBins() as $service_id => $cache_backend) {
$table_options[$service_id] = $service_id;
}
array_unshift($table_options, t("- Select a service -"));
}
$element[$delta]['cid'] = [
'#type' => 'textfield',
'#default_value' => $cid,
'#size' => 20,
];
$element[$delta]['table'] = [
'#type' => 'select',
'#default_value' => $bin,
'#options' => $table_options,
];
$element[$delta]['operations'] = [
'#name' => 'advanced_remove_' . $delta,
'#type' => 'button',
'#value' => t('Remove'),
'#ajax' => [
'callback' => '_cacheflush_advanced_form_callback',
'wrapper' => 'cacheflush-advanced-settings-table',
'method' => 'replace',
'effect' => 'fade',
],
];
}
/**
* Implements advanced preset validation.
*/
function cacheflush_advanced_tab_validation($tab, $form, $form_state) {
$element = $form_state
->getTriggeringElement();
if (isset($element['#name']) && ($element['#name'] == 'advance_add' || strpos($element['#name'], 'advanced_remove_') === 0)) {
$form_state
->clearErrors();
}
else {
$storage = $form_state
->getStorage();
$table_elements = $form_state
->getValues()['vertical_tabs_advance']['cacheflush_advanced_table'];
if ($table_elements) {
$container = \Drupal::getContainer();
$bins = array_flip($container
->getParameter('cache_bins'));
foreach ($table_elements as $key => $value) {
if (!empty($value['cid']) && $value['table'] == '0') {
$form_state
->setError($form['vertical_tabs_advance']['cacheflush_advanced_table'][$key]['table'], 'Service is required!');
}
elseif (empty($value['cid']) && $value['table'] != '0') {
$form_state
->setError($form['vertical_tabs_advance']['cacheflush_advanced_table'][$key]['cid'], 'Cache ID is required!');
}
elseif (!empty($value['cid'])) {
$storage['presets']['advanced']['functions'][$key] = [
'#name' => '\\Drupal\\cacheflush\\Controller\\CacheflushApi::clearBinCache',
'#params' => [
$bins[$value['table']],
'delete',
$value['cid'],
],
];
}
}
}
$form_state
->setStorage($storage);
}
}
/**
* AJAX callback for advanced table.
*
* @see ajax_get_form()
*/
function _cacheflush_advanced_form_callback(&$form, &$form_state, $request) {
return $form['vertical_tabs_advance']['cacheflush_advanced_table'];
}
Functions
Name | Description |
---|---|
cacheflush_advanced_cacheflush_ui_tabs | Implements hook_cacheflush_ui_tabs(). |
cacheflush_advanced_form_cacheflush_add_form_alter | Implements hook_form_FORM_ID_alter(). |
cacheflush_advanced_form_cacheflush_edit_form_alter | Implements hook_form_FORM_ID_alter(). |
cacheflush_advanced_tab_validation | Implements advanced preset validation. |
_cacheflush_advanced_form | Form alter callback for add/edit cacheflush entity. |
_cacheflush_advanced_form_callback | AJAX callback for advanced table. |
_cacheflush_advanced_table_row | Build cacheflush custom settings row form elements. |