You are here

function hosting_alias_form_data in Hosting 7.3

Same name and namespace in other branches
  1. 6.2 alias/hosting_alias.module \hosting_alias_form_data()
  2. 7.4 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 87
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 Aliases'),
    '#type' => 'fieldset',
    '#prefix' => '<div class="clear-block" id="hosting-aliases-wrapper">',
    '#suffix' => '</div>',
  );
  $form['aliases_wrapper']['aliases'] = array(
    '#prefix' => '<div id="hosting-aliases"><label>' . t('Domain Aliases') . '</label>',
    '#suffix' => '</div>',
    '#tree' => TRUE,
  );

  // 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 an alias'),
    '#description' => t("Click here to add another alias."),
    '#weight' => 1,
    '#submit' => array(
      'hosting_alias_add_alias_submit',
    ),
    '#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']['redirection'] = array(
    '#type' => 'select',
    '#title' => t('Redirect all domain aliases to'),
    '#options' => $options,
    '#default_value' => $default,
    '#weight' => -1,
  );
  return $form;
}