You are here

function blockcache_alter_form_alter in Block Cache Alter 6

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

Implementation of hook_form_alter().

File

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

Code

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

  // Rehash on the block admin screen.
  if ($form_id == 'block_admin_display_form') {
    blockcache_alter_rehash();
  }

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

    // Add some funky hide and show javascript.
    drupal_add_js(drupal_get_path('module', 'blockcache_alter') . '/blockcache_alter.js', 'module');

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

    // Blockcache options.
    $block_cache_options = array(
      BLOCK_NO_CACHE => t('Do not cache'),
      BLOCK_CACHE_GLOBAL => t('Cache once for everything (global)'),
      BLOCK_CACHE_PER_PAGE => t('Per page'),
      BLOCK_CACHE_PER_ROLE => t('Per role'),
      BLOCK_CACHE_PER_ROLE | BLOCK_CACHE_PER_PAGE => t('Per role per page'),
      BLOCK_CACHE_PER_USER => t('Per user'),
      BLOCK_CACHE_PER_USER | BLOCK_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_result(db_query("SELECT cache FROM {blockcache_alter} WHERE module = '%s' AND delta = '%s'", $form['module']['#value'], $form['delta']['#value']));

    // 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,
      '#attributes' => array(
        'onChange' => 'javascript:BlockCacheAlterCheck(); return false;',
      ),
    );

    // Cache clearing option after saving this block.
    $display = $default_value == BLOCK_NO_CACHE ? 'none' : 'block';
    $form['cache']['cache_block_clear'] = array(
      '#type' => 'radios',
      '#title' => t('Clear cache'),
      '#prefix' => '<div id="blockcache_alter_wrapper" style="display: ' . $display . ';">',
      '#description' => t('Select the appropriate cache clearing action after saving this block.'),
      '#options' => $cache_clear_actions,
      '#default_value' => 1,
    );
    if ($core_patch != TRUE) {
      $form['cache']['cache_block_clear']['#suffix'] = '</div>';
    }

    // 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.'),
      );

      // 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.'),
      );

      // 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(
        '#prefix' => '<div style="float: left;">',
        '#suffix' => '</div>',
        '#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_get_types('types');
      foreach ($types as $type) {
        $node_types[$type->type] = $type->name;
      }
      $form['cache']['option_2']['bc_relate'] = array(
        '#prefix' => '<div style="float: left; margin-left: 15px;">',
        '#suffix' => '</div><div style="clear: both;"></div>',
        '#type' => 'checkboxes',
        '#title' => t('Relation'),
        '#options' => $node_types,
        '#default_value' => variable_get('bc_relate_' . $form['module']['#value'] . '_' . $form['delta']['#value'], array()),
      );

      // End div.
      $form['cache']['enddiv'] = array(
        '#type' => 'hidden',
        '#suffix' => '</div>',
      );
    }

    // Add our own submit handler and remove the default submit handler.
    unset($form['#submit'][0]);
    $form['submit']['#weight'] = 2;
    $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['block_cache']['block_cache']['#disabled'];
    $form['block_cache']['block_cache']['#disabled'] = FALSE;
    if ($node_grants == TRUE) {
      $form['block_cache']['block_cache']['#description'] = t('There are modules implementing node grants. If you want block caching to work, you need to apply the "blockcache_alter_no_node_grants.patch"');
    }
    else {
      $form['block_cache']['block_cache']['#description'] = '';
    }
  }
}