You are here

function hosting_alias_form_data in Hosting 7.4

Same name and namespace in other branches
  1. 6.2 alias/hosting_alias.module \hosting_alias_form_data()
  2. 7.3 alias/hosting_alias.module \hosting_alias_form_data()

Alter the node form for a site to add the aliases and redirection items.

Parameters

array $form: The form to alter, should come from hook_form_alter().

array $form_state: A keyed array containing the current state of the form.

1 call to hosting_alias_form_data()
hosting_alias_form_site_node_form_alter in alias/hosting_alias.module
Implements hook_form_alter().

File

alias/hosting_alias.module, line 89
Allow sites to have domain aliases that they can be accessed with.

Code

function hosting_alias_form_data(&$form, &$form_state) {

  // List the automatic aliasses first.
  $automatic_aliases = hosting_alias_get_aliases($form['#node'], HOSTING_ALIAS_AUTOMATIC);
  if (count($automatic_aliases)) {
    foreach ($automatic_aliases as $link) {
      $links[] = l($link, "http://{$link}");
    }
    $form['aliases_automatic'] = array(
      '#type' => 'item',
      '#title' => t('Automatic domain aliases'),
      '#markup' => implode(', ', $links),
      '#weight' => 10,
    );
  }

  // Add a wrapper for the aliases and more button.
  $form['aliases_wrapper'] = array(
    '#tree' => FALSE,
    '#title' => t('Domain Names'),
    '#type' => 'fieldset',
  );
  $form['aliases_wrapper']['aliases'] = array(
    '#prefix' => '<div class="clear-block" id="hosting-aliases-wrapper">',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );
  $form['aliases_wrapper']['aliases']['_default'] = array(
    '#type' => 'item',
    '#title' => t('Primary domain name'),
    '#description' => t('The primary domain name for this site.'),
    '#markup' => l($form['#node']->title, $form['#node']->title, array(
      'options' => array(
        'attributes' => array(
          'target' => '_blank',
        ),
      ),
    )),
  );
  $form['aliases_wrapper']['aliases']['label'] = array(
    '#type' => 'item',
    '#title' => t('Domain name aliases'),
    '#description' => t('Configure the web server to respond to these domain aliases in addition to the primary domain.'),
  );

  // Get the list of existing aliases, either from form_state or the node.
  if (isset($form_state['input']['aliases'])) {
    $aliases = array_filter($form_state['input']['aliases']);
  }
  elseif (isset($form['#node']->aliases)) {
    $aliases = array_filter($form['#node']->aliases);
  }
  else {
    $aliases = array();
  }

  // Add alias textfields.
  for ($delta = 0; $delta <= count($aliases); $delta++) {
    $form['aliases_wrapper']['aliases'][$delta] = array(
      '#type' => 'textfield',
      '#default_value' => isset($aliases[$delta]) ? $aliases[$delta] : '',
    );
  }

  // "Add Alias" button.
  $form['aliases_wrapper']['add_alias'] = array(
    '#type' => 'submit',
    '#value' => t('Add another alias'),
    '#submit' => array(
      'hosting_alias_add_alias_submit',
    ),
    '#states' => array(
      'visible' => array(
        '#hosting-aliases-wrapper .form-type-textfield:last input' => array(
          'filled' => TRUE,
        ),
      ),
    ),
    '#ajax' => array(
      'callback' => 'hosting_alias_add_alias_callback',
      'wrapper' => 'hosting-aliases-wrapper',
      'method' => 'replace',
      'effect' => 'fade',
    ),
  );

  // Redirection Domain.
  $options = array();
  $options[0] = t('No redirection');
  if (isset($form['#node']->title)) {
    $options[$form['#node']->title] = $form['#node']->title;
  }
  if (!empty($aliases)) {
    $options += array_combine($aliases, $aliases);
  }
  if (!empty($automatic_aliases)) {
    $options += array_combine($automatic_aliases, $automatic_aliases);
  }
  $default = isset($form['#node']->redirection) ? $form['#node']->redirection : '';
  $form['aliases_wrapper']['aliases']['redirection'] = array(
    '#type' => 'select',
    '#title' => t('Domain redirection target'),
    '#description' => t('Requests to all domains will be redirected to the redirection target.'),
    '#options' => $options,
    '#default_value' => $default,
  );
  return $form;
}