display_cache.admin.inc in Display Cache 7
Provides the Display Cache administrative interface.
File
display_cache.admin.incView source
<?php
/**
* @file
* Provides the Display Cache administrative interface.
*/
/**
* Returns a 'Flush Caches'-button.
*
* @see: display_cache_menu()
*/
function display_cache_flush_caches_form($form, $form_state) {
$form['display_cache_flush_caches'] = array(
'#type' => 'submit',
'#value' => t('Flush all display caches'),
);
return $form;
}
/**
* Submit handler for display_cache_flush_caches_form().
*/
function display_cache_flush_caches_form_submit($form, $form_state) {
cache_clear_all('*', DISPLAY_CACHE_CACHE_BIN, TRUE);
drupal_set_message(t('Display cache cleared.'));
}
/**
* Display cache settings form.
*
* @see display_cache_form_field_ui_display_overview_form_alter()
*/
function display_cache_field_ui_form(&$form, &$form_state) {
$entity_type = $form['#entity_type'];
$bundle = $form['#bundle'];
$view_mode = $form['#view_mode'];
$settings = display_cache_get_settings($entity_type, $bundle, $view_mode);
// Add additional settings vertical tab.
if (!isset($form['additional_settings'])) {
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
'#theme_wrappers' => array(
'vertical_tabs',
),
'#prefix' => '<div>',
'#suffix' => '</div>',
);
$form['#attached']['js'][] = 'misc/form.js';
$form['#attached']['js'][] = 'misc/collapse.js';
}
// Add fieldset.
$form['display_cache'] = array(
'#title' => t('Display Cache Display Settings'),
'#type' => 'fieldset',
'#group' => 'additional_settings',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#access' => user_access('administer display cache'),
);
// Add radios with caching options for the display.
$display_cache_options = array(
DISPLAY_CACHE_DISABLED => t('No caching'),
DISPLAY_CACHE_ENABLED => t('Cache display'),
DISPLAY_CACHE_FIELDS => t('Cache fields only'),
);
// Set default value.
$use_default_value = 0;
if (!empty($settings['default']['use'])) {
$use_default_value = $settings['default']['use'];
}
$form['display_cache']['display_cache_use'] = array(
'#type' => 'radios',
'#title' => t('Cache the rendered HTML of this view mode.'),
'#default_value' => $use_default_value,
'#options' => $display_cache_options,
);
// Add radios with granularity options for cache-per-page.
$page_granularity_options = array(
0 => t('No page granularity'),
DRUPAL_CACHE_PER_PAGE => t('Cache per URL'),
);
// Set default value.
$page_default_value = 0;
if (!empty($settings['default']['page_granularity'])) {
$page_default_value = $settings['default']['page_granularity'];
}
$form['display_cache']['display_cache_page_granularity'] = array(
'#type' => 'radios',
'#title' => t('Cache per URL'),
'#default_value' => $page_default_value,
'#options' => $page_granularity_options,
'#states' => array(
'invisible' => array(
'input[name="display_cache_use"]' => array(
'value' => 0,
),
),
),
);
// Add radios with granularity options for users.
$user_granularity_options = array(
0 => t('No user granularity'),
DRUPAL_CACHE_PER_ROLE => t('Cache per role'),
DRUPAL_CACHE_PER_USER => t('Cache per user'),
);
// Set default value.
$user_default_value = 0;
if (!empty($settings['default']['user_granularity'])) {
$user_default_value = $settings['default']['user_granularity'];
}
$form['display_cache']['display_cache_user_granularity'] = array(
'#type' => 'radios',
'#title' => t('User granularity'),
'#default_value' => $user_default_value,
'#options' => $user_granularity_options,
'#states' => array(
'invisible' => array(
'input[name="display_cache_use"]' => array(
'value' => 0,
),
),
),
);
if ($entity_type === 'node') {
$clear_on_comment_publication = display_cache_get_comment_publication_clears_host_entity_cache_settings($entity_type, $bundle, $view_mode);
$form['display_cache']['clear_on_comment_publication'] = array(
'#default_value' => $clear_on_comment_publication,
'#description' => t('Should the display cache for this view mode be cleared, if a comment is (un-)published or deleted for this node type.<br />
This could be useful if you use any information about the comments in a view mode.'),
'#states' => array(
'invisible' => array(
'input[name="display_cache_use"]' => array(
'value' => 0,
),
$settings['display_cache_use'] => FALSE,
),
),
'#title' => t('Clear display cache on comment (un-)publication/deletion'),
'#type' => 'checkbox',
);
}
// Add forms for field settings.
$fields = array();
$field_instances = field_info_instances($entity_type, $bundle);
foreach ($field_instances as $field_name => $field_instance) {
if ($field_instance['display'][$view_mode]['type'] !== 'hidden') {
$fields[$field_instance['display'][$view_mode]['weight']] = $field_name;
}
}
// Add fieldset for field settings.
if (!empty($fields)) {
ksort($fields);
}
$form['display_cache_fields'] = array(
'#title' => t('Display Cache Field Settings'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#group' => 'additional_settings',
'#access' => user_access('administer display cache'),
'#description' => t('Use this options to cache fields with less granularity.'),
);
// Add settings form for each field.
foreach ($fields as $field_name) {
$form_state['display_cache_field_settings'][] = $field_name;
_display_cache_field_settings_form($form, $form_state, $settings, $field_name);
}
// Add custom submit handler.
$form['#submit'][] = 'display_cache_view_mode_settings_form_submit';
}
/**
* Display cache field settings form.
*
* @see display_cache_form_field_ui_display_overview_form_alter()
* @see display_cache_field_ui_form()
*/
function _display_cache_field_settings_form(&$form, &$form_state, $settings, $field_name) {
$field_settings = array();
if (!empty($settings[$field_name])) {
$field_settings = $settings[$field_name];
}
// Add fieldset for this fields settings.
$fieldset = 'display_cache_field_' . $field_name;
$form['display_cache_fields'][$fieldset] = array(
'#type' => 'fieldset',
'#title' => $field_name,
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
// Disable field settings if display cache is disabled.
if ($settings['default']['use'] == DISPLAY_CACHE_DISABLED) {
$form['display_cache_fields'][$fieldset]['#disabled'] = TRUE;
}
// Open fieldset if field settings are overridden.
if (isset($field_settings['override']) && ($field_settings['override'] == DISPLAY_CACHE_ENABLED || $field_settings['override'] == DISPLAY_CACHE_DISABLED)) {
$form['display_cache_fields'][$fieldset]['#collapsed'] = FALSE;
}
// Add radios with caching options for this field.
$display_cache_options = array(
DISPLAY_CACHE_FIELDS => t('Use default'),
DISPLAY_CACHE_ENABLED => t('Override display cache defaults'),
);
// Set default value.
if (!empty($field_settings['override']) && $field_settings['override'] != 0) {
$override_default_value = $field_settings['override'];
}
else {
$override_default_value = DISPLAY_CACHE_DISABLED;
}
// Add exclude from cache option if display is set to field cache only.
if ($settings['default']['use'] === DISPLAY_CACHE_FIELDS) {
$display_cache_options[DISPLAY_CACHE_DISABLED] = t('Do not cache this field');
}
$form['display_cache_fields'][$fieldset][$fieldset . '_override'] = array(
'#type' => 'radios',
'#title' => t('Override display cache defaults'),
'#default_value' => $override_default_value,
'#options' => $display_cache_options,
);
// Add radios with page granularity options.
$page_granularity_options = array(
0 => t('No page granularity'),
);
$page_default_value = 0;
// Add page granularity option, if display is set to be URL aware.
if (!empty($settings['default']['page_granularity'])) {
$page_granularity_options[DRUPAL_CACHE_PER_PAGE] = t('Cache per URL');
if (!empty($field_settings['page_granularity']) && $field_settings['page_granularity'] != 0) {
$page_default_value = $field_settings['page_granularity'];
}
}
$form['display_cache_fields'][$fieldset][$fieldset . '_page_granularity'] = array(
'#type' => 'radios',
'#title' => t('Cache per URL'),
'#default_value' => $page_default_value,
'#options' => $page_granularity_options,
'#states' => array(
'visible' => array(
'input[name="' . $fieldset . '_override"]' => array(
'value' => 1,
),
),
),
);
// Add radios with user granularity options.
$user_default_value = 0;
if (!empty($field_settings['user_granularity']) && $field_settings['user_granularity'] != 0) {
$user_default_value = $field_settings['user_granularity'];
}
$user_granularity_options = array(
0 => t('No user granularity'),
);
if (!empty($settings['default']['user_granularity'])) {
if ($settings['default']['user_granularity'] == DRUPAL_CACHE_PER_ROLE) {
// Add role granularity option if display is set to role awareness.
$user_granularity_options[DRUPAL_CACHE_PER_ROLE] = t('Cache per role');
}
elseif ($settings['default']['user_granularity'] == DRUPAL_CACHE_PER_USER) {
// Add role and user granularity options if display is set to user or role
// awareness.
$user_granularity_options[DRUPAL_CACHE_PER_ROLE] = t('Cache per role');
$user_granularity_options[DRUPAL_CACHE_PER_USER] = t('Cache per user');
}
}
else {
$user_default_value = 0;
}
$form['display_cache_fields'][$fieldset][$fieldset . '_user_granularity'] = array(
'#type' => 'radios',
'#title' => t('User granularity'),
'#default_value' => $user_default_value,
'#options' => $user_granularity_options,
'#states' => array(
'visible' => array(
'input[name="' . $fieldset . '_override"]' => array(
'value' => 1,
),
),
),
);
}
/**
* Custom submit handler for display cache settings form.
*
* @see display_cache_form_field_ui_display_overview_form_alter()
*/
function display_cache_view_mode_settings_form_submit($form, $form_state) {
$values = $form_state['values'];
// Store display settings.
$settings['default']['use'] = $values['display_cache_use'];
$settings['default']['page_granularity'] = $values['display_cache_page_granularity'];
$settings['default']['user_granularity'] = $values['display_cache_user_granularity'];
$settings['default']['granularity'] = $values['display_cache_page_granularity'] | $values['display_cache_user_granularity'];
// Store field settings.
if (!empty($form_state['display_cache_field_settings'])) {
foreach ($form_state['display_cache_field_settings'] as $field_name) {
// Store override settings.
$field_override = $values['display_cache_field_' . $field_name . '_override'];
if ($settings['default']['use'] != DISPLAY_CACHE_FIELDS && $field_override == DISPLAY_CACHE_DISABLED) {
$settings[$field_name]['override'] = DISPLAY_CACHE_FIELDS;
}
else {
$settings[$field_name]['override'] = $field_override;
}
if ($settings[$field_name]['override'] == DISPLAY_CACHE_ENABLED) {
// Store page granularity for this field.
$field_page_granularity = $values['display_cache_field_' . $field_name . '_page_granularity'];
if (!empty($settings['default']['page_granularity']) && $settings['default']['page_granularity'] <= $field_page_granularity) {
$settings[$field_name]['page_granularity'] = $settings['default']['page_granularity'];
}
else {
$settings[$field_name]['page_granularity'] = $field_page_granularity;
}
// Store user granularity for this field.
$field_user_granularity = $values['display_cache_field_' . $field_name . '_user_granularity'];
if (!empty($settings['default']['user_granularity']) && $settings['default']['user_granularity'] <= $field_user_granularity) {
$settings[$field_name]['user_granularity'] = $settings['default']['user_granularity'];
}
else {
$settings[$field_name]['user_granularity'] = $field_user_granularity;
}
}
else {
// Set default settings for this field if it is not overridden.
$settings[$field_name]['page_granularity'] = $settings['default']['page_granularity'];
$settings[$field_name]['user_granularity'] = $settings['default']['user_granularity'];
}
$settings[$field_name]['granularity'] = $settings[$field_name]['page_granularity'] | $settings[$field_name]['user_granularity'];
}
}
$bundle = $form['#bundle'];
$entity_type = $form['#entity_type'];
$view_mode = $form['#view_mode'];
$variable_name = 'display_cache_' . $entity_type . '_' . $bundle . '_' . $view_mode;
variable_set($variable_name, $settings);
$display_cache_variables = variable_get('display_cache_variables', array());
if (!in_array($variable_name, $display_cache_variables)) {
$display_cache_variables[] = $variable_name;
variable_set('display_cache_variables', $display_cache_variables);
}
if ($entity_type === 'node') {
$clear_on_comment_publication_settings = display_cache_get_comment_publication_clears_host_entity_cache_settings($entity_type, $bundle);
$clear_on_comment_publication_settings[$view_mode] = $values['clear_on_comment_publication'];
$variable_name = display_cache_get_comment_publication_clears_host_entity_cache_setting_name($entity_type, $bundle);
variable_set($variable_name, $clear_on_comment_publication_settings);
// Unset the global clearing for this node type, because else it would over-
// write this setting.
variable_del('display_cache_comment_publication_clears_host_entity_cache_' . $bundle);
if (!in_array($variable_name, $display_cache_variables)) {
$display_cache_variables[] = $variable_name;
variable_set('display_cache_variables', $display_cache_variables);
}
}
}
/**
* Alters the node_type_form form.
*
* @param array $form
* The form to alter.
* @param array $form_state
* The form state.
*/
function display_cache_node_type_form(&$form, &$form_state) {
if (isset($form['comment'])) {
$form['comment']['display_cache_comment_publication_clears_host_entity_cache'] = array(
'#description' => t('Should the display cache for this node type be cleared, if a comment is (un-)published or deleted for this node type.<br />
This could be useful if you use any information about the comments in a view mode.<br />
<b>Please be aware that on a site with a lot of view modes for this node type and an active community this can have a big perfomance impact.</b>'),
'#title' => t('Clear host entity display cache on comment (un-)publication/deletion'),
'#type' => 'checkbox',
'#default_value' => variable_get('display_cache_comment_publication_clears_host_entity_cache_' . $form['#node_type']->type, FALSE),
);
}
}
Functions
Name | Description |
---|---|
display_cache_field_ui_form | Display cache settings form. |
display_cache_flush_caches_form | Returns a 'Flush Caches'-button. |
display_cache_flush_caches_form_submit | Submit handler for display_cache_flush_caches_form(). |
display_cache_node_type_form | Alters the node_type_form form. |
display_cache_view_mode_settings_form_submit | Custom submit handler for display cache settings form. |
_display_cache_field_settings_form | Display cache field settings form. |