You are here

delta.module in Delta 7

Same filename and directory in other branches
  1. 6 delta.module
  2. 7.3 delta.module
  3. 7.2 delta.module

The Delta Theme API is an advanced manipulation of the Theme Settings API to allow for customization/configuration of theme settings based on node types, context, or groups of paths.

This module was developed with the Omega base theme in mind (http://drupal.org/project/omega) but will be set up in a way to allow any theme to take advantage of the functionality it provides.

File

delta.module
View source
<?php

/**
 * @file
 * The Delta Theme API is an advanced manipulation of the Theme Settings API 
 * to allow for customization/configuration of theme settings based on 
 * node types, context, or groups of paths.
 * 
 * This module was developed with the Omega base theme in mind (http://drupal.org/project/omega)
 * but will be set up in a way to allow any theme to take advantage of the functionality it provides.
 */

/**
 * Implementation of hook_init()
 * @see http://api.drupal.org/api/function/hook_init/6
 */
function delta_init() {

  // only include delta.css & .js on the administrative section
  // I really get angry when administrative modules include their css & js on the front end.
  if (drupal_substr($_GET['q'], 0, 5) == 'admin') {
    $path = drupal_get_path('module', 'delta');
    drupal_add_css($path . '/delta.css', 'module', 'all');
    drupal_add_js($path . '/delta.js', 'module', 'header');
  }
}

/**
 * Implementation of hook_help().
 * @see http://api.drupal.org/api/function/hook_help/6
 */
function delta_help($path, $arg) {

  // pull the theme arg in default drupal IA
  if ($arg[0] == 'admin' && $arg[1] == 'build' && $arg[2] == 'delta' && $arg[3] == 'settings') {
    $theme = $arg[4];
  }
  switch ($path) {
    case 'admin/build/delta':
    case 'admin/build/delta/settings/' . $theme:
    case 'admin/help#delta':
      $output = '';
      $output .= '<p>The Delta Theme Settings API provides advanced theme functionality to implement a system where the default theme settings may be overriden based on a set of criteria.</p>';
      $output .= '<p>This is pretty much cool, wrapped in freaking cool.</p>';
      return $output;
  }
}

/**
 * Implementation of hook_perm().
 * @see http://api.drupal.org/api/function/hook_perm/6
 */
function delta_perm() {
  return array(
    'configure delta theme api',
  );
}

/**
 * Implementation of hook_menu().
 */
function delta_menu() {

  /**
   * provides a menu tab inline with the theme list/configure tabs
   * this will also provide the default settings for the module
   *
   * @see http://api.drupal.org/api/function/hook_menu/6
   * @see delta_theme_settings_config()
   */
  $items['admin/build/delta'] = array(
    'title' => 'Delta Theme Settings API',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'delta_theme_settings_config',
    ),
    'access arguments' => array(
      'configure delta theme api',
    ),
    'file' => 'delta-settings.inc',
    'type' => MENU_NORMAL_ITEM,
  );

  // Default Delta API settings tab item
  $items['admin/build/delta/settings'] = array(
    'title' => 'Configure',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );

  // Default Delta API settings tab item
  $items['admin/build/delta/settings/global'] = array(
    'title' => 'Global Settings',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -1,
  );

  /**
   * provide a subtab item for each theme that is currently enabled in the system
   * @todo implement a menu_cache_clear_all() after a previously unselected theme is enabled.
   */
  $themes = delta_get_themes_array();
  foreach ($themes as $id => $theme) {
    $items['admin/build/delta/settings/' . $theme->name] = array(
      'title' => $theme->info['name'],
      'page callback' => 'delta_theme_overrides',
      'page arguments' => array(
        $theme->name,
      ),
      'type' => MENU_LOCAL_TASK,
      'access arguments' => array(
        'configure delta theme api',
      ),
    );
  }
  $items['admin/build/delta/import'] = array(
    'title' => 'Import',
    'page callback' => 'delta_create_import',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'configure delta theme api',
    ),
  );
  $items['admin/build/delta/export'] = array(
    'title' => 'Export',
    'page callback' => 'delta_create_export',
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'configure delta theme api',
    ),
  );

  // Callbacks

  /**
   * These items create and help the Add/Edit Override pages
   */
  $items['admin/build/delta/override/add'] = array(
    'title' => 'Add Override',
    'page callback' => 'delta_manage_override_page',
    'access arguments' => array(
      'configure delta theme api',
    ),
    'type' => MENU_LOCAL_TASK,
  );

  // AHAH Callback for the Override Add/Edit form.
  $items['delta/theme-select-theme-settings-overrides'] = array(
    'title' => 'AHAH Theme Settings Override Callback',
    'page callback' => 'delta_ahah',
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'configure delta theme api',
    ),
  );
  $items['admin/build/delta/add-template/%'] = array(
    'title' => 'Add Template',
    'page callback' => 'delta_create_theme_settings_template',
    // wildcard from URL will be the theme name
    'page arguments' => array(
      4,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'configure delta theme api',
    ),
  );
  $items['admin/build/delta/override/manage/%/edit'] = array(
    'title' => 'Edit Override',
    'page callback' => 'delta_manage_override_page',
    'page arguments' => array(
      5,
    ),
    'access arguments' => array(
      'configure delta theme api',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/delta/override/manage/%/delete'] = array(
    'title' => 'Delete Override',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'delta_override_delete',
      5,
    ),
    'access arguments' => array(
      'configure delta theme api',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/build/delta/override/manage/%/export'] = array(
    'title' => 'Export Override',
    'page callback' => 'delta_manage_override_page',
    'page arguments' => array(
      5,
    ),
    'access arguments' => array(
      'configure delta theme api',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implementation of hook_theme
 * @see http://api.drupal.org/api/function/hook_theme/6
 * 
 * @param $existing
 * An array of existing implementations that may be used for override purposes. 
 * This is primarily useful for themes that may wish to examine existing implementations to extract 
 * data (such as arguments) so that it may properly register its own, higher priority implementations.
 * 
 * @param $type 
 * What 'type' is being processed. This is primarily useful so that themes tell if they are the 
 * actual theme being called or a parent theme.
 * 
 * @param $theme 
 * The actual name of theme that is being being checked (mostly only useful for theme engine).
 * 
 * @param $path 
 * The directory path of the theme or module, so that it doesn't need to be looked up.
 * 
 * @return array()
 * Modules and themes implementing this return an array of arrays. 
 * The key to each sub-array is the internal name of the hook, and the array contains info about the hook.
 */
function delta_theme($existing, $type, $theme, $path) {
  return array(
    'delta_theme_overrides_form' => array(
      'arguments' => array(
        'form' => NULL,
      ),
      //'file' => 'theme-functions.inc',
      'template' => 'delta-override-form',
    ),
  );
}

/**
 * Menu callback for admin/build/delta/%.
 */
function delta_theme_overrides($theme) {
  $result = db_query("\n    SELECT \n      did, tid, system_name, name, weight, value \n    FROM {delta_theme_overrides} \n    WHERE theme = '%s'\n    ORDER BY weight ASC", $theme);
  while ($delta = db_fetch_object($result)) {
    $conditions = unserialize($delta->value);
    $data[$delta->did] = array(
      'did' => $delta->did,
      'tid' => $delta->tid,
      'name' => $delta->name,
      'system_name' => $delta->system_name,
      'weight' => $delta->weight,
      'conditions' => array(
        'nodes' => $conditions['node-types'],
        'contexts' => $conditions['contexts'],
        'paths' => $conditions['paths'],
      ),
    );
  }

  // add the override form to the page
  $output = '<h2>Theme Settings Overrides</h2>';
  $output .= drupal_get_form('delta_theme_overrides_form', $theme, $data);

  // get the theme settings template for this theme
  $template_result = db_query("\n    SELECT\n      name, tid\n    FROM {delta_theme_settings}\n    WHERE theme = '%s'\n    ORDER BY name ASC\n  ", $theme);
  $output .= '<div class="template-overrides"><h2>Theme Settings Templates</h2>';
  while ($templates = db_fetch_object($template_result)) {
    $output .= '<div>' . $templates->name . ' <a href="">edit</a></div>';
  }
  $output .= '</div>';
  return $output;
}

/**
 * Generate the override list page.
 */
function delta_theme_overrides_form(&$form_state, $theme, $data) {

  // Build form tree
  $form = array(
    '#action' => url('admin/build/delta/settings/' . $theme),
    '#tree' => TRUE,
  );
  $form['theme'] = array(
    '#type' => 'hidden',
    '#value' => $theme,
  );

  //krumo($data);
  if (count($data) > 0) {
    foreach ($data as $k => $delta) {
      $key = 'delta_id_' . $k;
      $form[$key]['name'] = array(
        '#value' => $delta['name'],
      );

      /**
       * Build out the type of override(s) this is.
       * Possible values are node, context, path
       */
      $override_type = array();
      if (count(array_filter($delta['conditions']['nodes'])) > 0) {
        $override_type[] = 'Node';
      }
      if (count(array_filter($delta['conditions']['contexts'])) > 0) {
        $override_type[] = 'Context';
      }
      if ($delta['conditions']['paths']) {
        $override_type[] = 'Path';
      }
      $override_types = implode(', ', $override_type);

      //krumo($override_types);
      $form[$key]['types'] = array(
        '#value' => $override_types,
      );
      $form[$key]['did'] = array(
        '#type' => 'hidden',
        '#value' => $delta['did'],
      );
      $form[$key]['weight'] = array(
        '#type' => 'weight',
        '#default_value' => $delta['weight'],
        '#delta' => 100,
      );
      $theme_settings_templates = delta_get_theme_settings_templates($theme);
      $form[$key]['theme_settings_template'] = array(
        '#type' => 'select',
        '#options' => count($theme_settings_templates) > 0 ? $theme_settings_templates : array(
          '0' => '<' . t('none') . '>',
        ),
        '#default_value' => $delta['tid'],
      );
      $form[$key]['#info'] = 'delta_override';

      // operations
      $form[$key]['edit'] = array(
        '#value' => l(t('edit'), 'admin/build/delta/override/manage/' . $delta['did'] . '/edit'),
      );
      $form[$key]['delete'] = array(
        '#value' => l(t('delete'), 'admin/build/delta/override/manage/' . $delta['did'] . '/delete'),
      );
      $form[$key]['export'] = array(
        '#value' => l(t('export'), 'admin/build/delta/override/manage/' . $delta['did'] . '/export'),
      );
      $form[$key]['add_template'] = array(
        '#value' => l(t('add new template'), 'admin/build/delta/add-template/' . $theme, array(
          'attributes' => array(
            'class' => 'add-template',
            'id' => $delta['did'],
          ),
        )),
      );
    }
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Changes'),
    );
  }
  return $form;
}

/**
 * Process main blocks administration form submission.
 */
function delta_theme_overrides_form_submit($form, &$form_state) {

  // here we will be updating the default weight AND the assigned template
  foreach ($form_state['values'] as $delta) {
    if (is_array($delta)) {
      db_query("\n        UPDATE {delta_theme_overrides} \n        SET weight = %d, tid = %d \n        WHERE did = %d", $delta['weight'], $delta['theme_settings_template'], $delta['did']);
    }
  }
  drupal_set_message(t('The Delta API settings have been updated.'));
}

/**
 * Preprocess functionality for the Delta Overrides form
 * @param $vars
 */
function template_preprocess_delta_theme_overrides_form(&$vars) {
  foreach (element_children($vars['form']) as $i) {
    $delta =& $vars['form'][$i];
    if (isset($delta['#info'])) {

      // Set special classes needed for table drag and drop.
      $vars['form'][$i]['weight']['#attributes']['class'] = 'delta-weight';
      $vars['delta_listing'][$i]->row_class = isset($delta['#attributes']['class']) ? $delta['#attributes']['class'] : '';
      $vars['delta_listing'][$i]->name = drupal_render($delta['name']);

      /*
       * @todo
       * This will need to be generated by pulling the unserialized data from
       * the value field, and seeing what types it applies to
       * structure change
       */
      $vars['delta_listing'][$i]->types = drupal_render($delta['types']);
      $vars['delta_listing'][$i]->weight_select = drupal_render($delta['weight']);
      $vars['delta_listing'][$i]->theme_settings_template = drupal_render($delta['theme_settings_template']);
      $vars['delta_listing'][$i]->edit_link = drupal_render($delta['edit']);
      $vars['delta_listing'][$i]->delete_link = !empty($delta['delete']) ? drupal_render($delta['delete']) : '';
      $vars['delta_listing'][$i]->export_link = drupal_render($delta['export']);
      $vars['delta_listing'][$i]->add_template_link = drupal_render($delta['add_template']);
      $vars['delta_listing'][$i]->printed = FALSE;
    }
  }
  $vars['form_submit'] = drupal_render($vars['form']);

  // set the default add template link
  $theme = $vars['form']['theme']['#value'];
  $vars['default_add_override_link'] = l(t('add new override'), 'admin/build/delta/add-template/' . $theme);
  drupal_add_js('misc/tableheader.js');
  drupal_add_tabledrag('delta-overrides', 'order', 'sibling', 'delta-weight');
}

/**
 * Implmentation of hook_form_alter for system_theme_settings()
 * @return $form
 * @see http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6
 * @see http://api.drupal.org/api/function/hook_form_alter/6
 */
function delta_form_system_theme_settings_alter(&$form, &$form_state) {
  return $form;
}

/**
 * Implmentation of hook_form_alter for delta_theme_settings_config()
 * @return $form
 * @see http://api.drupal.org/api/function/hook_form_FORM_ID_alter/6
 * @see http://api.drupal.org/api/function/hook_form_alter/6
 */
function delta_form_delta_theme_settings_config_alter(&$form, &$form_state) {

  // adding submit handler to rebuild the menu on submitting this form
  $form['#submit'][] = 'delta_menu_rebuild';
}
function delta_form_alter(&$form, &$form_state) {

  // adding submit handler to rebuild the menu on submitting this form

  //krumo($form);
}

/**
 * Called when saving the default delta settings form.
 * This is needed to rebuild the menu when an update occurs in order to
 * Add/Remove any themes that need to be in the menu system for rendering
 * the appropriate sub-tabs.
 */
function delta_menu_rebuild() {
  menu_rebuild();
  menu_cache_clear_all();
}

/**
 * Pull data from actively selected themes
 * @return the full theme array as normally pulled by list_themes(), but filters out the inactive themes.
 */
function delta_get_themes_array() {
  $system_themes = list_themes();
  $themes = array();
  $configurable_themes = variable_get('delta_themes', array());
  if (is_array($system_themes)) {
    foreach ($system_themes as $name => $theme) {

      // let's gather active themes only, and the ones that have been set in Delta API global settings
      if ($theme->status == 1 && $configurable_themes[$theme->name]) {
        $themes[$name] = $theme;
      }
    }
    return $themes;
  }
  return FALSE;
}

/**
 * Pull data from actively selected themes
 * @return a usable array of theme data for use in forms for checkboxes or radio buttons
 */
function delta_get_themes_form_array(&$filter) {
  $system_themes = list_themes();
  $configurable_themes = variable_get('delta_themes', array());
  $themes = array();
  if (is_array($system_themes)) {
    foreach ($system_themes as $name => $theme) {

      // let's gather active themes only
      if (!$filter) {
        if ($theme->status == 1) {
          $themes[$name] = $theme->info['name'];
        }
      }
      else {
        if ($theme->status == 1 && $configurable_themes[$theme->name]) {
          $themes[$name] = $theme->info['name'];
        }
      }
    }
    return $themes;
  }
  return FALSE;
}

/**
 * Pull an object of available templates for the currently selected theme
 * @param $theme
 */
function delta_get_theme_settings_templates($theme = FALSE) {

  // pull select records only from a single theme
  if ($theme) {
    $result = db_query("\n      SELECT \n        tid, name \n      FROM {delta_theme_settings} \n      WHERE theme = '%s'\n      ORDER BY name ASC", $theme);
    while ($delta = db_fetch_object($result)) {
      $data[$delta->tid] = $delta->name;
    }
  }
  else {
    $result = db_query("\n      SELECT \n        tid, name, theme\n      FROM {delta_theme_settings} \n      ORDER BY theme ASC, name ASC");
    while ($delta = db_fetch_object($result)) {
      $data[$delta->theme][$delta->tid] = $delta->name;
    }
  }
  return $data;
}

/**
 * Page callback for admin/build/delta/add-override
 * @return
 * Returns the page, including the form generated by delta_manage_override_form()
 * @see delta_manage_override_form()
 */
function delta_manage_override_page($oid = FALSE) {
  drupal_set_title(t('Add Override | Delta API'));
  if ($oid) {

    //krumo($oid);
    drupal_set_title(t('Edit Override | Delta API'));
  }
  $output = drupal_get_form('delta_manage_override_form', $oid);
  return $output;
}
function delta_manage_override_form(&$form_state, $oid = FALSE) {

  //krumo($form_state);
  if ($oid && !$form_state['delta']) {
    $stored_data = delta_get_stored_override($oid);

    //krumo($stored_data);
  }
  $form = array();

  // Set the cache to true to create a $form_state cache on submit
  $form['#cache'] = TRUE;
  if ($oid) {
    $form['#action'] = base_path() . '/admin/build/delta/override/manage/' . $oid . '/edit';
  }
  else {
    $form['#action'] = base_path() . '/admin/build/delta/override/add';
  }
  if (isset($form_state['delta'])) {
    $delta = $form_state['delta'] + (array) $delta;
  }

  // $filter_themes tells us to only pull the themes selected in the Delta API config page, not all active themes.
  $filter_themes = TRUE;
  $theme_select = array(
    '0' => '<' . t('none') . '>',
  ) + delta_get_themes_form_array($filter_themes);

  // gather all available theme templates for the select menu
  $theme_templates = delta_get_theme_settings_templates();
  if ($delta['delta_override']['theme']) {
    $type = $delta['delta_override']['theme'];
  }
  elseif ($stored_data['delta_override']['theme']) {
    $type = $stored_data['delta_override']['theme'];
  }
  else {
    $type = 'none';
  }
  $form['delta_override'] = array(
    '#type' => 'fieldset',
    '#title' => t('Delta Override General Info'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );

  /**
   * Since there are issues with AHAH and required fields, the
   * following two fields will be marked as NOT required, but
   * manually checked during validation.
   */

  // mark this for an update rather than insert sql
  $form['delta_override']['did'] = array(
    '#type' => 'hidden',
    '#default_value' => $stored_data['delta_override']['did'],
  );
  $form['delta_override']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Override Title'),
    '#required' => FALSE,
    '#default_value' => $stored_data['delta_override']['name'],
    '#description' => t('Use a descriptive title to name your theme settings override.'),
  );
  $form['delta_override']['system_name'] = array(
    '#type' => 'textfield',
    '#title' => t('System Name'),
    '#required' => FALSE,
    '#default_value' => $stored_data['delta_override']['system_name'],
    '#description' => t('This is the unique name of the override. It must contain only alphanumeric characters and underscores'),
  );
  $form['delta_override']['theme'] = array(
    '#title' => t('Select a Theme to Override'),
    '#type' => 'select',
    '#default_value' => $stored_data['delta_override']['theme'],
    '#options' => $theme_select,
    '#ahah' => array(
      'event' => 'change',
      'path' => 'delta/theme-select-theme-settings-overrides/',
      'wrapper' => 'theme-settings-template-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
    '#default_value' => $stored_data['delta_override']['theme'],
  );
  $form['theme_submit'] = array(
    '#type' => 'submit',
    '#value' => t('Select Theme'),
    '#access' => FALSE,
    '#submit' => array(
      'delta_add_override_select_theme_submit',
    ),
  );
  $theme = $delta['delta_override']['theme'] ? $delta['delta_override']['theme'] : $stored_data['delta_override']['theme'];
  switch ($type) {
    case 'none':
      $msg_1 = t('You must first select a theme to apply this override to, then you will be able to select or create a theme settings template.');
      $msg_2 = t(' ');
      break;
    default:
      $msg_1 = t(' ');
      $msg_2 = t('If you do not want to use a theme settings template already in the system, you may also ') . l(t('add new template'), 'admin/build/delta/add-template/' . $theme, array(
        'attributes' => array(
          'class' => 'popups-form',
          'id' => $delta['did'],
        ),
      ));
      break;
  }

  // create an empty form element to contain the second dropdown
  $form['delta_override']['tst'] = array(
    '#tree' => TRUE,
    '#prefix' => '<div class="description" id="theme-settings-template-wrapper">',
    '#value' => $msg_1,
    '#suffix' => '</div>',
  );
  $tid = $stored_data['delta_override']['tid'];
  $form['delta_override']['tst']['theme_settings_template'] = array(
    '#title' => t('Select Theme Settings Template'),
    '#description' => $msg_2,
    '#type' => 'select',
    '#access' => $type != 'none' || $tid ? TRUE : FALSE,
    '#options' => $type == 'none' ? array(
      '0' => '<' . t('none') . '>',
    ) : $theme_templates[$type],
    '#default_value' => $tid ? $tid : 0,
  );
  $form['delta_override']['weight'] = array(
    '#type' => 'weight',
    '#description' => t('The weight field will prioritize this override, and its importance in ordering overrides for processing. The lower the weight, the earlier in the process it occurs.'),
    '#default_value' => $stored_data['delta_override']['weight'] ? $stored_data['delta_override']['weight'] : 0,
    '#delta' => 100,
  );
  $form['delta_conditions'] = array(
    '#type' => 'fieldset',
    '#title' => t('Delta Override Conditional Criteria'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
    '#tree' => TRUE,
  );
  $stored_nodes = $stored_data['delta_conditions']['nodes'] ? $stored_data['delta_conditions']['nodes'] : array();
  $form['delta_conditions']['nodes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Node Types'),
    '#options' => node_get_types('names'),
    '#default_value' => $stored_nodes,
    '#description' => t('You may select as many node types as you like here. Any node types selected will have this override applied to them unless a higher priority override applies to the same node type.'),
  );

  /**
   * If we have the context module installed, we will enable a list to select those
   * contexts that the override should apply to. </rockstar-stuff>
   */
  if (module_exists('context')) {
    $contexts = context_enabled_contexts();
    $form_contexts = array();
    if (count($contexts > 0)) {
      foreach ($contexts as $key => $context) {
        $namespace = $context->namespace;
        $attribute = $context->attribute;
        $value = $context->value;
        $form_contexts[$context->cid] = $namespace . ' &gt; ' . $attribute . ' &gt; ' . $value;
      }
    }
    $stored_contexts = $stored_data['delta_conditions']['contexts'] ? $stored_data['delta_conditions']['contexts'] : array();
    $form['delta_conditions']['contexts'] = array(
      '#type' => 'checkboxes',
      '#title' => t('Contexts'),
      '#options' => $form_contexts,
      '#default_value' => $stored_contexts,
      '#description' => t('You may select as many contexts as you like here. Any node types selected will have this override applied to them unless a higher priority override applies to the same context.'),
    );
  }
  $form['delta_conditions']['paths'] = array(
    '#type' => 'textarea',
    '#title' => t('Paths'),
    '#default_value' => $stored_data['delta_conditions']['paths'],
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );

  // Since a #select element can't have it's own #submit handler, we have to create an extra
  // submit button to which we attach the correct callback function that's going to be called on a change
  // We'll hide this with css! This is taken care of in ahah_example_init().
  // submit button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Override'),
    '#validate' => array(
      'delta_add_override_validate',
    ),
    '#submit' => array(
      'delta_add_override_submit',
    ),
  );
  return $form;
}

/**
 * This submit handler is hooked to the hidden submit button and is executed on the AHAH call
 **/
function delta_add_override_select_theme_submit($form, &$form_state) {
  $delta = $form_state['values'];

  //drupal_set_message('<pre>'. print_r($delta, TRUE) .'</pre>');
  unset($form_state['submit_handlers']);
  form_execute_handlers('submit', $form, $form_state);
  $form_state['delta'] = $delta;
  $form_state['rebuild'] = TRUE;
}
function delta_add_override_validate($form, &$form_state) {

  // only valid when we are truly submitting the form, not for AHAH
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    $delta = $form_state['values']['delta_override'];
    if (!$delta['name']) {
      form_set_error('name', t('You must create a valid <strong>Override Title</strong> for this override.'));
    }
    if (!$delta['system_name']) {
      form_set_error('system_name', t('You must create a valid <strong>System Name</strong> for this override.'));
    }
    if (!$delta['theme']) {
      form_set_error('theme', t('You need to select a theme for this override to apply to.'));
    }
  }
}
function delta_add_override_submit($form, &$form_state) {
  $delta = $form_state['values'];
  $override_data = array(
    'node-types' => $delta['delta_conditions']['nodes'],
    'contexts' => $delta['delta_conditions']['contexts'],
    'paths' => $delta['delta_conditions']['paths'],
  );

  // New Override, Insert query
  if (!$delta['delta_override']['did']) {
    db_query("\n      INSERT INTO {delta_theme_overrides}  \n        (tid, system_name, name, value, weight, theme) \n      VALUES(%d, '%s', '%s', '%s', %d, '%s')", $delta['delta_override']['tst']['theme_settings_template'], t($delta['delta_override']['system_name']), t($delta['delta_override']['name']), serialize($override_data), $delta['delta_override']['weight'], $delta['delta_override']['theme']);
    drupal_set_message('New Override Saved.');
  }
  else {
    db_query("\n      UPDATE {delta_theme_overrides}  \n        SET \n          tid = %d, \n          system_name = '%s', \n          name = '%s', \n          value = '%s', \n          weight = %d, \n          theme = '%s' \n      WHERE did = %d", $delta['delta_override']['tst']['theme_settings_template'], t($delta['delta_override']['system_name']), t($delta['delta_override']['name']), serialize($override_data), $delta['delta_override']['weight'], $delta['delta_override']['theme'], $delta['delta_override']['did']);
  }

  // redirect to the settings page for the theme this override was created for
  drupal_goto('admin/build/delta/settings/' . $delta['delta_override']['theme']);
}
function delta_ahah() {

  // this part is used to set up $form_state.
  // In Drupal 7, these next 11 lines will be put in a core utility function.
  // Just remember you'll need them in D6 when you do AHAH!
  $form_state = array(
    'storage' => NULL,
    'submitted' => FALSE,
  );

  //$form_state = array('storage' => NULL, 'rebuild' => TRUE);
  $form_build_id = $_POST['form_build_id'];
  $form = form_get_cache($form_build_id, $form_state);
  $args = $form['#parameters'];
  $form_id = array_shift($args);
  $form['#post'] = $_POST;
  $form['#redirect'] = FALSE;
  $form['#programmed'] = FALSE;
  $form_state['post'] = $_POST;
  drupal_process_form($form_id, $form, $form_state);
  $form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  // From here on, we start our own code.
  // We just get the element of $form that needs to be refreshed, and just resubmit that
  // part through the json call. In this case we want to replace the 'kind' <div> wrapper and it's
  // containing select box
  $changed_elements = $form['delta_override']['tst'];

  //unset($changed_elements['#prefix'], $changed_elements['suffix']); // we'll unset the div to make sure it won't be repeated!

  // the actual JSON call
  $javascript = drupal_add_js(NULL, NULL, 'header');
  drupal_json(array(
    'status' => TRUE,
    'data' => theme('status_messages') . drupal_render($changed_elements),
    // rebuild just the part that needs to be changed
    'settings' => call_user_func_array('array_merge_recursive', $javascript['setting']),
  ));
}
function delta_get_stored_override($oid) {
  $result = db_query("\n    SELECT \n      did, tid, theme, system_name, name, weight, value \n    FROM {delta_theme_overrides} \n    WHERE did = %d\n    ORDER BY weight ASC", $oid);
  $delta = db_fetch_object($result);
  $conditions = unserialize($delta->value);
  $data = array(
    'delta_override' => array(
      'did' => $delta->did,
      'tid' => $delta->tid,
      'theme' => $delta->theme,
      'name' => $delta->name,
      'system_name' => $delta->system_name,
      'weight' => $delta->weight,
    ),
    'delta_conditions' => array(
      'nodes' => $conditions['node-types'],
      'contexts' => $conditions['contexts'],
      'paths' => $conditions['paths'],
    ),
  );
  return $data;
}

/**
 * Function to provide confirmation before deleting an override
 */
function delta_override_delete($form_state, $oid) {
  $form = array();
  $stored_data = delta_get_stored_override($oid);
  $form['delta_override']['did'] = array(
    '#type' => 'hidden',
    '#default_value' => $oid,
  );
  $form['delta_override']['name'] = array(
    '#type' => 'hidden',
    '#default_value' => $stored_data['delta_override']['name'],
  );
  $form['delta_override']['theme'] = array(
    '#type' => 'hidden',
    '#default_value' => $stored_data['delta_override']['theme'],
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Delete Override'),
    '#prefix' => t('<h3>Are you sure you want to delete the override named <em>' . $stored_data['delta_override']['name'] . '</em>?</h3>'),
  );
  return $form;
}

/**
 * submit handler for delta_override_delete()
 * @see delta_override_delete()
 */
function delta_override_delete_submit($form, &$form_state) {
  $oid = $form_state['values']['did'];
  db_query("\n    DELETE FROM {delta_theme_overrides}  \n    WHERE did = %d\n    LIMIT 1", $oid);
  drupal_set_message('Override: <em>' . $form_state['values']['name'] . '</em> deleted.');
  drupal_goto('admin/build/delta/settings/' . $form_state['values']['theme']);
}
function delta_create_export() {
  drupal_set_title(t('Export Delta API Overrides & Theme Settings Templates'));
  $data = delta_build_export();
  $output = '<div style="height: 420px;overflow: auto;"><pre>';
  $output .= print_r($data, TRUE);
  $output .= '</pre></div>';
  return $output;
}
function delta_build_export() {
  $overrides_result = db_query("SELECT * FROM {delta_theme_overrides}");
  $data = array(
    'overrides' => array(),
    'templates' => array(),
  );
  while ($delta = db_fetch_object($overrides_result)) {
    $conditions = unserialize($delta->value);
    $data['overrides'][$delta->did] = array(
      'did' => $delta->did,
      'tid' => $delta->tid,
      'name' => $delta->name,
      'system_name' => $delta->system_name,
      'weight' => $delta->weight,
      'theme' => $delta->themet,
      'conditions' => array(
        'nodes' => $conditions['node-types'],
        'contexts' => $conditions['contexts'],
        'paths' => $conditions['paths'],
      ),
    );
  }
  return $data;
}
function delta_create_import() {
  drupal_set_title(t('Import Delta API Overrides & Theme Settings Templates'));
  $output = '';
  return $output;
}

/**
 * Page callback for AJAX implementation of the add template form
 * @param $theme
 * The theme to which the template is being added/edited
 */
function delta_create_theme_settings_template($theme) {
  $output = drupal_get_form('delta_create_theme_settings_template_form', $theme);
  return $output;
}

/**
 * Form callback for the AJAX form using Popups API
 * @param $form_state
 * @param $theme
 */
function delta_create_theme_settings_template_form($form_state, $theme) {
  $form = array();
  $form['info'] = array(
    '#value' => t('Create a new Theme Settings Template for the <strong><em>' . $theme . '</em></strong> theme.'),
  );
  $form['delta_template'] = array(
    '#type' => 'fieldset',
    '#title' => t('General Template Information'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );
  $form['delta_template']['theme'] = array(
    '#type' => 'hidden',
    '#value' => $theme,
  );
  $form['delta_template']['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Theme Settings Template Title'),
    '#required' => FALSE,
    '#default_value' => $form_state['delta_template']['name'],
    '#description' => t('Use a descriptive title to name your theme settings template.'),
  );
  $form['delta_template_overrides'] = array(
    '#type' => 'fieldset',
    '#title' => t('Theme Settings to Override.'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#tree' => TRUE,
  );

  /**
   * Let's look to locate a theme-settings.php file for the theme we are
   * creating the template for.
   * Once we have the file, we can included it and load the function
   */
  if (file_exists(drupal_get_path('theme', $theme) . '/theme-settings.php')) {
    include_once drupal_get_path('theme', $theme) . '/theme-settings.php';
    if (function_exists($theme . '_settings')) {
      $function = $theme . '_settings';
    }
    elseif (function_exists('phptemplate_settings')) {
      $function = 'phptemplate_settings';
    }
    else {

      // @todo let's put in a check if both these functions don't exist, and throw fat errors.
    }
    $settings = theme_get_settings($theme);
    $override_form_elements = $function($settings);
    foreach ($override_form_elements as $k => $v) {
      $form['delta_template_overrides'][$k] = $v;
    }
  }

  // submit button
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Template'),
    '#validate' => array(
      'delta_create_theme_settings_template_validate',
    ),
    '#submit' => array(
      'delta_create_theme_settings_template_submit',
    ),
  );
  $form['#redirect'] = 'admin/build/delta/settings/' . $theme;
  return $form;
}
function delta_create_theme_settings_template_validate($form, &$form_state) {
  if ($form_state['clicked_button']['#id'] == 'edit-submit') {
    if (!$form_state['values']['delta_template']['name']) {
      form_set_error('name', t('You must create a valid <strong>Theme Settings Template Title</strong> for this override.'));
    }
  }
}
function delta_create_theme_settings_template_submit($form, &$form_state) {
  $delta = $form_state['values'];

  // New Override, Insert query
  db_query("\n      INSERT INTO {delta_theme_settings}  \n        (name, theme, data) \n      VALUES('%s', '%s', '%s')", $delta['delta_template']['name'], $delta['delta_template']['theme'], serialize($delta['delta_template_overrides']));
  drupal_set_message('New theme settings template: ' . $form_state['values']['delta_template']['name'] . ' created.');
}

Functions

Namesort descending Description
delta_add_override_select_theme_submit This submit handler is hooked to the hidden submit button and is executed on the AHAH call
delta_add_override_submit
delta_add_override_validate
delta_ahah
delta_build_export
delta_create_export
delta_create_import
delta_create_theme_settings_template Page callback for AJAX implementation of the add template form
delta_create_theme_settings_template_form Form callback for the AJAX form using Popups API
delta_create_theme_settings_template_submit
delta_create_theme_settings_template_validate
delta_form_alter
delta_form_delta_theme_settings_config_alter Implmentation of hook_form_alter for delta_theme_settings_config()
delta_form_system_theme_settings_alter Implmentation of hook_form_alter for system_theme_settings()
delta_get_stored_override
delta_get_themes_array Pull data from actively selected themes
delta_get_themes_form_array Pull data from actively selected themes
delta_get_theme_settings_templates Pull an object of available templates for the currently selected theme
delta_help Implementation of hook_help().
delta_init Implementation of hook_init()
delta_manage_override_form
delta_manage_override_page Page callback for admin/build/delta/add-override
delta_menu Implementation of hook_menu().
delta_menu_rebuild Called when saving the default delta settings form. This is needed to rebuild the menu when an update occurs in order to Add/Remove any themes that need to be in the menu system for rendering the appropriate sub-tabs.
delta_override_delete Function to provide confirmation before deleting an override
delta_override_delete_submit submit handler for delta_override_delete()
delta_perm Implementation of hook_perm().
delta_theme Implementation of hook_theme
delta_theme_overrides Menu callback for admin/build/delta/%.
delta_theme_overrides_form Generate the override list page.
delta_theme_overrides_form_submit Process main blocks administration form submission.
template_preprocess_delta_theme_overrides_form Preprocess functionality for the Delta Overrides form