You are here

function blockcache_alter_form_alter in Block Cache Alter 7

Same name and namespace in other branches
  1. 6 blockcache_alter.module \blockcache_alter_form_alter()

Implementation of hook_form_alter().

File

./blockcache_alter.module, line 67
Block cache alter module.

Code

function blockcache_alter_form_alter(&$form, $form_state, $form_id) {

  // Add caching fieldset to the block configure page.
  if ($form_id == 'block_admin_configure') {

    // Core patch applied or not.
    $core_patch = variable_get('bca_corepatch', 0);

    // Blockcache options.
    $block_cache_options = array(
      DRUPAL_NO_CACHE => t('Do not cache'),
      DRUPAL_CACHE_GLOBAL => t('Cache once for everything (global)'),
      DRUPAL_CACHE_PER_PAGE => t('Per page'),
      DRUPAL_CACHE_PER_ROLE => t('Per role'),
      DRUPAL_CACHE_PER_ROLE | DRUPAL_CACHE_PER_PAGE => t('Per role per page'),
      DRUPAL_CACHE_PER_USER => t('Per user'),
      DRUPAL_CACHE_PER_USER | DRUPAL_CACHE_PER_PAGE => t('Per user per page'),
    );

    // Cache clear actions.
    $cache_clear_actions = array(
      '1' => t('Clear caching for this block only'),
      '2' => t('Clear all cache (block and page)'),
    );

    // Retrieve current cache setting for this block.
    $default_value = db_select('block', 'b')
      ->fields('b', array(
      'cache',
    ))
      ->condition('module', $form['module']['#value'])
      ->condition('delta', $form['delta']['#value'])
      ->execute()
      ->fetchField();

    // Blockcache fieldset.
    $form['cache'] = array(
      '#type' => 'fieldset',
      '#title' => t('Cache settings'),
      '#weight' => 1,
      '#collapsible' => TRUE,
    );

    // Cache setting.
    $form['cache']['cache_block'] = array(
      '#type' => 'select',
      '#title' => t('Cache setting'),
      '#description' => t('Select the appropriate cache setting for this block.'),
      '#options' => $block_cache_options,
      '#default_value' => $default_value,
    );

    // Cache clearing option after saving this block.
    $form['cache']['cache_block_clear'] = array(
      '#type' => 'radios',
      '#title' => t('Clear cache'),
      '#description' => t('Select the appropriate cache clearing action after saving this block.'),
      '#options' => $cache_clear_actions,
      '#default_value' => 1,
      '#states' => array(
        'invisible' => array(
          ':input[name="cache_block"]' => array(
            'value' => DRUPAL_NO_CACHE,
          ),
        ),
      ),
    );

    // Show options underneath onl if core patch is applied.
    if ($core_patch == TRUE) {

      // Blockcache fieldset.
      $form['cache']['option_1'] = array(
        '#type' => 'fieldset',
        '#title' => t('Cache refresh option 1: Cache lifetime'),
        '#description' => t('Set a minimum cache lifetime (in seconds). Leave 0 or empty to use the second option.'),
        '#states' => array(
          'invisible' => array(
            ':input[name="cache_block"]' => array(
              'value' => DRUPAL_NO_CACHE,
            ),
          ),
        ),
      );

      // Blockcache lifetime.
      $form['cache']['option_1']['bc_life'] = array(
        '#type' => 'textfield',
        '#title' => t('Cache lifetime'),
        '#size' => 10,
        '#default_value' => variable_get('bc_life_' . $form['module']['#value'] . '_' . $form['delta']['#value'], ''),
      );

      // Blockcache fieldset.
      $form['cache']['option_2'] = array(
        '#type' => 'fieldset',
        '#title' => t('Cache refresh option 2: Drupal actions'),
        '#description' => t('Refresh on certain actions in the Drupal system. When choosing node or comment action, you also need to specify on which node types it should refresh. Note that clicking any of these checkboxes will not have any effect if a minimum lifetime is set in option 1.'),
        '#states' => array(
          'invisible' => array(
            ':input[name="cache_block"]' => array(
              'value' => DRUPAL_NO_CACHE,
            ),
          ),
        ),
      );

      // Blockcache refresh.
      $options = array(
        'node' => t('A node is added/updated/deleted'),
        'comment' => t('A comment is added/updated/deleted'),
        'user' => t('A user is added/updated/deleted'),
        'login' => t('A user logs in our out'),
      );

      // Nodequeue support.
      if (module_exists('nodequeue') && $form['module']['#value'] == 'views' && strstr($form['delta']['#value'], 'nodequeue') !== FALSE) {
        $options['nodequeue'] = t('A node is added or removed from a nodequeue');
      }
      $form['cache']['option_2']['bc_refresh'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Refresh when'),
        '#options' => $options,
        '#default_value' => variable_get('bc_refresh_' . $form['module']['#value'] . '_' . $form['delta']['#value'], array()),
      );

      // Associate with node types.
      $node_types = array();
      $types = node_type_get_types();
      foreach ($types as $type) {
        $node_types[$type->type] = $type->name;
      }
      $form['cache']['option_2']['bc_relate'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Relation'),
        '#options' => $node_types,
        '#default_value' => variable_get('bc_relate_' . $form['module']['#value'] . '_' . $form['delta']['#value'], array()),
      );
    }

    // Add own submit handler.
    $form['#submit'][] = 'blockcache_alter_save_settings';
  }

  // Alter the performance settings page: remove the #disabled key from the block_cache form completely
  // and add a warning message instead to warn the user that block caching won't work when one
  // at least on module implements node_grants and blockcache_alter_no_node_grants is not applied.
  // Note, again, we don't check which patch is applied, that's up to the developer.
  if ($form_id == 'system_performance_settings') {
    $node_grants = $form['caching']['block_cache']['#disabled'];
    $form['caching']['block_cache']['#disabled'] = FALSE;
    if ($node_grants == TRUE) {
      $form['caching']['block_cache']['#description'] = t('WARNING: There are modules implementing node grants. If you want block caching to work, you need to apply the "blockcache_alter.patch"');
    }
    else {
      $form['caching']['block_cache']['#description'] = '';
    }
  }
}