You are here

function esi_form_block_admin_configure_alter in ESI: Edge Side Includes 6.2

Implementation of hook_form_FORM_ID_alter(). for block_admin_configure Add ESI-configuration options to the block-config pages.

File

./esi.module, line 284
Adds support for ESI (Edge-Side-Include) integration, allowing blocks to be\ delivered by ESI, with support for per-block cache times.

Code

function esi_form_block_admin_configure_alter(&$form, $form_state) {

  // TODO: describe how the cache configs can be configured as defaults in code.
  // load our helper functions
  require_once drupal_get_path('module', 'esi') . '/esi.inc';
  $module = $form['module']['#value'];
  $delta = $form['delta']['#value'];
  $config = esi_get_settings($module . '_' . $delta);
  $element['esi_config'] = array(
    '#title' => t('ESI settings'),
    '#type' => 'fieldset',
    '#description' => t('Control how this block is cached on an ESI-enabled reverse proxy.'),
    '#tree' => TRUE,
  );
  $element['esi_config']['scope'] = array(
    '#title' => t('Cache Scope'),
    '#type' => 'select',
    '#options' => array(
      0 => 'Disabled',
      1 => 'Not Cached',
      2 => 'Global',
      3 => 'Page',
      4 => 'User Role',
      5 => 'User Role/Page',
      6 => 'User ID',
      7 => 'User ID/Page',
    ),
    '#default_value' => $config['scope'],
    '#description' => t('Disabled - Do not use ESI. <br />Not Cached - Use ESI, but never cache the content. <br />Global - Content is same on every page. <br />Page - Content changes based on the URL. <br />User Role - Content changes based on the user role. <br />User Role/Page - Content changes based on the user role as well as the URL. <br />User ID - Content changes based on the UID; otherwise it is the same as global. <br />User ID/Page - Content changes based on the UID and based on the URL.'),
  );
  $max_age = $config['max_age'];
  $element['esi_config']['max_age'] = array(
    '#title' => t('Cache Maximum Age (TTL)'),
    '#type' => 'select',
    '#options' => esi_max_age_options($max_age),
    '#default_value' => $max_age,
    '#description' => t('External page caches (proxy/browser) will not deliver cached paged older than this setting; time to live in short.'),
  );
  $widget_vis = "\n\$(esi_update_visibility);\n\$(function(){ \$('#edit-esi-config-scope').change(function (){esi_update_visibility();})});\nfunction esi_update_visibility() {\n  var esi_scope = \$('#edit-esi-config-scope').val();\n  if (esi_scope == '0' || esi_scope == '1') {\n    \$('#edit-esi-config-max-age-wrapper').hide();\n  }\n  else {\n    \$('#edit-esi-config-max-age-wrapper').show();\n  }\n}";
  drupal_add_js($widget_vis, 'inline');

  // inject our ESI config-fieldset onto the form,
  // just after the 'block_settings' fieldset.
  $i = 1;
  foreach ($form as $key => $value) {
    if ($key == 'block_settings') {
      break;
    }
    $i++;
  }
  $f = array_slice($form, 0, $i);
  $f += $element;
  $f += array_slice($form, $i);
  $form = $f;

  // add a submit-handler to save our config.
  $form['#submit'][] = 'esi__block_config_save';
}