function authcache_form_alter in Authenticated User Page Caching (Authcache) 6
Same name and namespace in other branches
- 7.2 authcache.module \authcache_form_alter()
- 7 authcache.module \authcache_form_alter()
Implements hook_form_alter(),
File
- ./
authcache.module, line 270 - Authenticated User Page Caching (and anonymous users, too!)
Code
function authcache_form_alter(&$form, $form_state, $form_id) {
global $user, $is_page_authcache, $is_ajax_authcache;
// Forms for logged-in users
if ($user->uid && $is_page_authcache) {
// 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) {
// Don't cache name on comment form
case 'comment_form':
$form['_author']['#value'] = '<span class="authcache-user"></span>';
break;
// Remove default values on contact form (hook_authcache_ajax will retrieve defaults)
case 'contact_mail_page':
$form['name']['#default_value'] = $form['mail']['#default_value'] = '';
$form['form_token_id']['#value'] = 'contact_mail_page';
// Don't use user's email
break;
// /user/%uid/contact URL shouldn't even be cached
case 'contact_mail_user':
break;
}
}
// Anonymous & authenticatd cacheable forms
if ($is_page_authcache) {
// Forms won't be cached on cached pages, so no need for build ids
unset($form['form_build_id']);
unset($form['#build_id']);
}
// Forms being rendered during Ajax phase
if ($is_ajax_authcache) {
$form['#action'] = "";
}
if ($is_page_authcache || $is_ajax_authcache) {
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['submit']['#submit'][] = 'authcache_form_submit';
break;
}
}
// Alter all forms
switch ($form_id) {
// Alter Drupal's "Performance" admin form
case 'system_performance_settings':
$form['page_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['page_cache']['cache']['#options'] = array(
0 => t('Disabled') . ' ' . t('by') . ' Authcache',
);
}
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'] = -10;
$form['authcache_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Authcache Ajax'),
'#collapsible' => TRUE,
'#collapsed' => FALSE,
'#weight' => -5,
);
$form['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['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;
}
}