Created
September 17, 2019 23:46
-
-
Save drewfranz/41e854bdb79a8439ae7dd286878b82de to your computer and use it in GitHub Desktop.
Example Drupal 7 Utility Module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @file | |
* Provides cache utility functions | |
*/ | |
/** | |
* Implements hook_menu(). | |
*/ | |
function cache_form_util_menu() { | |
$items['admin/cache_form_util'] = array( | |
'title' => t('Cache Form Utility'), | |
'page callback' => 'drupal_get_form', | |
'page arguments' => array('cache_form_util_callback'), | |
'access callback' => 'user_access', | |
'access arguments' => array('administer cache form util'), | |
'type' => MENU_NORMAL_ITEM, | |
); | |
return $items; | |
} | |
/** | |
* Admin cron config form | |
*/ | |
function cache_form_util_callback($form, &$form_state) { | |
$form['cache_form_util'] = array( | |
'#type' => 'fieldset', | |
'#title' => t('Cache Form Utility'), | |
'#collapsible' => TRUE, | |
'#collapsed' => FALSE | |
); | |
$form['cache_form_util']['cache_form_util_cache_form_clear_limit'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Limit for cache_form pruning'), | |
'#default_value' => variable_get('cache_form_util_cache_form_clear_limit', 1000), | |
'#description' => t('Make cron limit the number of expired entries on cache_form') | |
); | |
$form['cache_form_util']['cache_form_util_cache_form_clear_interval'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Cron Job Time Interval'), | |
'#default_value' => variable_get('cache_form_util_cache_form_clear_interval', 60), | |
'#description' => t('Time in minutes between cron job excution times.') | |
); | |
return system_settings_form($form); | |
} | |
/** | |
* Impliments hook_cron(). | |
* | |
* Remove a limited number of objects from the cache_form table. | |
* | |
*/ | |
function cache_form_util_cron() { | |
$limit = variable_get('cache_form_util_cache_form_clear_limit', 1000); | |
// Default to an hourly interval. Of course, cron has to be running at least | |
// hourly for this to work. | |
$interval = variable_get('cache_form_util_cache_form_clear_interval', 60) * 60; | |
// We usually don't want to act every time cron runs (which could be every | |
// minute) so keep a time for the next run in a variable. | |
if (time() >= variable_get('cache_form_util_cache_form_clear_next_execution', 0)) { | |
watchdog("Cache Form Util", t('Selecting up to !limit removable items...', array('!limit' => $limit))); | |
// Get $limit number of cached forms that are older than the expire default. | |
// By default, expiration is 6 hours after the time an entry was written | |
$results = db_select('cache_form') | |
->fields('cache_form', array('cid')) | |
->condition('expire', CACHE_PERMANENT, '<>') | |
->condition('expire', REQUEST_TIME, '<') | |
->range(0, $limit) | |
->execute(); | |
$cids = $results->fetchCol('cid'); | |
if (empty($cids)) { | |
return 0; | |
} | |
watchdog("Cache Form Util", t('Deleting !count items...', array('!count' => count($cids)))); | |
// Delete the batch of expired entries. | |
$deleted = db_delete('cache_form') | |
->condition('cid', $cids, 'IN') | |
->execute(); | |
watchdog("Cache Form Util", t('!count items deleted', array('!count' => $deleted))); | |
variable_set('cache_form_util_cache_form_clear_next_execution', time() + $interval); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment