You are here

function domain_settings_form_alter in Domain Access 7.3

Same name and namespace in other branches
  1. 6.2 domain_settings/domain_settings.module \domain_settings_form_alter()
  2. 7.2 domain_settings/domain_settings.module \domain_settings_form_alter()

Implements 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.

File

domain_settings/domain_settings.module, line 49
Allows domain-specific use of Drupal system settings forms.

Code

function domain_settings_form_alter(&$form, $form_state, $form_id) {
  $_domain = domain_get_domain();

  // Remove settings that cannot be used reliably.
  if (!user_access('access domain settings form') || !isset($form['#submit']) || !in_array('system_settings_form_submit', $form['#submit']) || !domain_settings_add_element($form_id)) {

    // Is this form_id allowed?
    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_alters 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(
    DOMAIN_SETTINGS_ALL_DOMAINS => t('All domains'),
  );

  // Get the display format of the form element.
  $format = domain_select_format();
  foreach (domain_domains() as $data) {

    // The domain must be valid.
    if ($data['valid'] || user_access('access inactive domains')) {

      // Checkboxes must be filtered, select lists should not.
      $options[$data['domain_id']] = 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,
  );

  // Set the proper default.
  $behavior = variable_get('domain_settings_behavior', 0);
  if ($behavior == 1) {
    $default = $_domain['domain_id'];
  }
  elseif ($behavior == 2) {
    $default = DOMAIN_SETTINGS_ALL_DOMAINS;
  }
  else {
    $default = domain_default_id();
  }
  $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. If you select <em>All domains</em>, domain-specific settings will be removed.'),
    '#default_value' => $default,
  );
  if ($format) {
    $form['domain_settings']['domain_id']['#multiple'] = FALSE;
    $form['domain_settings']['domain_id']['#size'] = count($options) > 10 ? 10 : count($options);
  }
  if (isset($form['site_name'])) {
    $form['site_name']['#description'] = ' <em>' . t('This value will change the registered name of the selected domain.') . '</em>';
  }
  foreach ($form['#submit'] as $key => $value) {
    if ($value == 'system_settings_form_submit') {
      unset($form['#submit'][$key]);
    }
  }
}