domain_settings.module

<?php

// $Id: domain_settings.module,v 1.1.2.6 2010/03/01 01:55:33 agentken Exp $

/**
 * @file
 * Allows domain-specific use of Drupal system settings forms.
 */

/**
 * Implement hook_perm().
 */
function domain_settings_perm() {
  return array('access domain settings form');
}

/**
 * Implement hook_form_alter()
 *
 * This function checks the #submit value of the form to see
 * if system_settings_form() has been invoked. If so, we may
 * attach domain-specific settings to the form, if the user is
 * permitted to use this module and the form is not on the
 * administrator's denied list.
 */
function domain_settings_form_alter(&$form, $form_state, $form_id) {
  global $_domain;

  // Remove settings that cannot be used reliably.
  $disallow = domain_settings_disallow();
  if (!user_access('access domain settings form') || in_array($form_id, $disallow) || !isset($form['#submit']) || !in_array('system_settings_form_submit', $form['#submit'])) {
    return;
  }
  // Set our drop down's weight to be one lighter than the submit button's,
  // ensuring that it always appears right above it (assuming nobody else
  // form_alter()s us out.)
  drupal_set_message(t('This form is domain-sensitive, be sure you select the proper domain before saving.'), 'warning', FALSE);
  $weight = isset($form['buttons']['#weight']) ? $form['buttons']['#weight'] : 0;
  $form['buttons']['#weight'] = $weight + 2;
  $domain_weight = $weight + 1;
  $form['#submit'][] = 'domain_settings_form_submit';
  $options = array();
  // Get the display format of the form element.
  $format = domain_select_format();
  foreach (domain_domains() as $data) {
    // Cannot pass zero in checkboxes.
    ($data['domain_id'] == 0) ? $key = -1 : $key = $data['domain_id'];
    // The domain must be valid.
    if ($data['valid'] || user_access('access inactive domains')) {
      // Checkboxes must be filtered, select listts should not.
      $options[$key] = empty($format) ? check_plain($data['sitename']) : $data['sitename'];
    }
  }
  $form['domain_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Domain-specific settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => $domain_weight,
  );
  $default = $_domain['domain_id'];
  if (!variable_get('domain_settings_behavior', 0) || $default == 0) {
    $default = -1;
  }
  $form['domain_settings']['domain_id'] = array(
    '#type' => empty($format) ? 'radios' : 'select',
    '#title' => t('Save settings for'),
    '#options' => $options,
    '#required' => TRUE,
    '#description' => t('Select the domain to which these settings apply.'),
    '#default_value' => $default,
  );
  if ($format) {
    $form['domain_settings']['domain_id']['#multiple'] = FALSE;
    $form['domain_settings']['domain_id']['#size'] = count($options) > 10 ? 10 : count($options);
  }
  foreach ($form['#submit'] as $key => $value) {
    if ($value == 'system_settings_form_submit') {
      unset($form['#submit'][$key]);
    }
  }
}

/**
 * Submit handler for domain-specific settings.
 */
function domain_settings_form_submit($form, &$form_state) {
  $domain_id = $form_state['values']['domain_id'];
  $reset = FALSE;
  if ($form_state['values']['op'] == $form_state['values']['reset']) {
    $reset = TRUE;
  }
  $values = array();
  foreach ($form_state['values'] as $key => $value) {
    if (!in_array($key, array('op', 'submit', 'reset', 'form_build_id', 'form_token', 'form_id', 'domain_id'))) {
      $values[$key] = $value;
    }
  }
  // -1 is the primary domain.
  foreach ($values as $name => $value) {
    if ($domain_id == -1) {
      if ($reset) {
        variable_del($name);
      }
      else {
        variable_set($name, $value);
      }
    }
    else {
      if ($reset) {
        domain_conf_variable_delete($domain_id, $name);
      }
      else {
        domain_conf_variable_save($domain_id, $name, $value);
      }
    }
  }
  module_invoke_all('domain_settings', $domain_id, $values);
  cache_clear_all();
}

/**
 * Implements hook_domainform().
 */
function domain_settings_domainform(&$form) {
  // Add the form element to the main screen.
  $form['domain_settings_module'] = array(
    '#type' => 'fieldset',
    '#title' => t('Domain-specific settings'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  $form['domain_settings_module']['domain_settings_behavior'] = array(
    '#type' => 'radios',
    '#title' => t('Domain settings behavior'),
    '#options' => array(0 => t('Use the default domain'), 1 => t('Use the active domain')),
    '#default_value' => variable_get('domain_settings_behavior', 0),
    '#description' => t('Default form value when submitting system settings.')
  );
  $form['domain_settings_module']['domain_settings_ignore'] = array(
    '#type' => 'textarea',
    '#title' => t('Disallowed forms'),
    '#rows' => 5,
    '#cols' => 40,
    '#default_value' => variable_get('domain_settings_ignore', ''),
    '#description' => t('Disallow domain-specific settings by entering a list of form_ids, one per line.'),
  );
}

/**
 * Helper function to disallow specific forms from the interface.
 *
 * By design, the configuration for Domain Access itself is disallowed.
 */
function domain_settings_disallow() {
  static $forms;

  if (!isset($forms)) {
    $items = array();
    $list = trim(variable_get('domain_settings_ignore', ''));
    if (!empty($list)) {
      $match = preg_replace('/(\r\n?|\n)/', '|', $list);
      $items = explode("|", $match);
    }
    // These forms are known to be problematic, so omit them.
    $default = array(
      'domain_configure_form',
      'system_file_system_settings', // Changing file paths is dangerous.
      'system_performance_settings', // JS and CSS aggregation is not supported.
    );
    $forms = array_merge($default, $items);
  }

  return $forms;
}

/**
 * Implement hook_domain_warnings_alter().
 */
function domain_settings_domain_warnings_alter(&$forms) {
  // Forms which Domain Settings handles and are set as warnings.
  $core_forms = array(
    'system_admin_theme_settings',
    'system_site_information_settings',
    'system_site_maintenance_settings',
    'menu_configure',
  );
  foreach ($core_forms as $form_id) {
    if (isset($forms[$form_id])) {
      unset($forms[$form_id]);
    }
  }
}

Contact

  • agentrickard [at] gmail [dot] com

Donations

My Amazon.com Wish List