function authcache_form_alter in Authenticated User Page Caching (Authcache) 7
Same name and namespace in other branches
- 6 authcache.module \authcache_form_alter()
- 7.2 authcache.module \authcache_form_alter()
Implements hook_form_alter(),
File
- ./
authcache.module, line 234 - Authenticated User Page Caching (and anonymous users, too!)
Code
function authcache_form_alter(&$form, &$form_state, $form_id) {
global $user, $_authcache_is_cacheable, $_authcache_is_ajax;
// Forms for logged-in users
if ($user->uid && $_authcache_is_cacheable) {
// Remove user-specific form token
if (isset($form['form_token'])) {
if (isset($form['form_token']['#default_value'])) {
$form['form_token']['#default_value'] = '';
}
if (isset($form['form_token']['#value'])) {
$form['form_token']['#value'] = '';
}
}
// Token will be generated via ajax_authcache.php, but correct id is needed
$form['form_token_id'] = array(
'#type' => 'hidden',
'#value' => isset($form['#token']) ? $form['#token'] : $form_id,
);
// Views exposed form (Views uses custom form rendering functions)
if (isset($form['#theme']) && is_array($form['#theme']) && in_array('views_exposed_form', $form['#theme'])) {
unset($form['#token']);
// Prevents validation error
unset($form['form_token_id']);
}
// Modify specific forms
switch ($form_id) {
// Remove default values on contact form (hook_authcache_ajax will retrieve defaults)
case 'contact_site_form':
unset($form['name']['#default_value']);
unset($form['mail']['#default_value']);
break;
}
}
// Anonymous & authenticatd cacheable forms
if ($_authcache_is_cacheable) {
if ($user->uid) {
// We do not support cached forms on cached pages for authenticated users.
// Therefore remove the form-build-id and ensure that the form is not
// saved to the cache.
$form['#after_build'][] = '_authcache_form_remove_build_id';
$form_state['no_cache'] = TRUE;
}
else {
// Prevent form state from leaking between anonymous sessions. Fix for
// SA-CORE-2014-002, see: https://drupal.org/node/2242659
$form_state['build_info']['immutable'] = TRUE;
}
}
// Forms being rendered during Ajax phase
if ($_authcache_is_ajax) {
$form['#action'] = "";
}
if ($_authcache_is_cacheable || $_authcache_is_ajax) {
switch ($form_id) {
// poll vote/results form may be ajax; must keep track of submit for cache invalidation
case 'poll_view_voting':
$form['vote']['#submit'][] = 'authcache_form_submit';
break;
case 'poll_cancel_form':
$form['actions']['submit']['#submit'][] = 'authcache_form_submit';
break;
}
}
// Alter all forms
switch ($form_id) {
// Alter Drupal's "Performance" admin form
case 'system_performance_settings':
$form['caching']['cache']['#description'] = ' <strong>' . t('If Authcache is enabled for the "anonymous user" role, Drupal\'s built-in page caching will be automatically disabled since all page caching is done through Authcache API instead of Drupal core.') . '</strong>';
if (_authcache_is_account_cacheable(drupal_anonymous_user())) {
$form['caching']['cache']['#disabled'] = TRUE;
//array(0 => t('Disabled') . ' ' . t('by') . ' Authcache');
$form['caching']['cache']['#value'] = FALSE;
// Disable hiding of compression check-box
unset($form['bandwidth_optimization']['page_compression']['#prefix']);
unset($form['bandwidth_optimization']['page_compression']['#suffix']);
}
break;
case 'user_profile_form':
// Don't allow user local timezone
if (_authcache_is_account_cacheable()) {
unset($form['timezone']);
}
break;
case 'block_add_block_form':
case 'block_admin_configure':
$authcache_block = variable_get('authcache_block', array());
$block_id = "{$form['module']['#value']}-{$form['delta']['#value']}";
$form['block_settings']['#weight'] = 50;
//simg: changed from -10. why would you want authcache settings at top of block page?
$form['visibility']['authcache_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Authcache Ajax'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'visibility',
'#attached' => array(
'js' => array(
drupal_get_path('module', 'authcache') . '/authcache.admin.js',
),
),
);
$form['visibility']['authcache_settings']['authcache'] = array(
'#type' => 'checkbox',
'#title' => t('Load block with Ajax on cached Authcache pages'),
'#description' => t('This is useful for dynamic or user-centric content, however it places additional load on the server.'),
'#default_value' => isset($authcache_block[$block_id]),
);
$form['visibility']['authcache_settings']['authcache_lifetime'] = array(
'#type' => 'textfield',
'#title' => t('Minimum cache lifetime'),
'#description' => t('Enter the number of seconds to locally cache the block in the user\'s browser. This improves performance and prevents jumpiness.'),
'#field_suffix' => t('seconds'),
'#size' => 8,
'#default_value' => isset($authcache_block[$block_id]) ? $authcache_block[$block_id] : 0,
);
$form['#submit'][] = 'authcache_block_submit';
break;
}
}