View source
<?php
function crossdomain_form($form, &$form_state) {
$form['#tree'] = TRUE;
$form['domains_fieldset'] = array(
'#type' => 'fieldset',
'#title' => t('Domains to Allow For Flash Files'),
'#prefix' => '<div id="domains-fieldset-wrapper">',
'#suffix' => '</div>',
);
$form['domains_fieldset']['description'] = array(
'#markup' => 'Add domains that will be used to build a crossdomain.xml file so flash files can use it.',
);
$domains = variable_get('crossdomain', array());
if (empty($form_state['input']) && !empty($domains)) {
$form_state['num_domains'] = count($domains);
}
else {
if (!isset($form_state['num_domains'])) {
$form_state['num_domains'] = 1;
}
}
for ($i = 0; $i < $form_state['num_domains']; $i++) {
$form['domains_fieldset']['element'][$i] = array(
'#prefix' => '<div class="container-inline">',
'#suffix' => '</div>',
);
$form['domains_fieldset']['element'][$i]['domain'] = array(
'#type' => 'textfield',
);
if (empty($form_state['input']) && !empty($domains)) {
$form['domains_fieldset']['element'][$i]['domain']['#default_value'] = $domains[$i];
}
if ($form_state['num_domains'] > 1) {
$form['domains_fieldset']['element'][$i]['remove_button'] = array(
'#type' => 'submit',
'#value' => t('Remove'),
'#name' => 'remove-button-' . $i,
'#submit' => array(
'crossdomain_form_remove_one',
),
'#ajax' => array(
'callback' => 'crossdomain_form_callback',
'wrapper' => 'domains-fieldset-wrapper',
),
);
}
}
$form['domains_fieldset']['add_domain'] = array(
'#type' => 'submit',
'#value' => t('Add another'),
'#submit' => array(
'crossdomain_form_add_one',
),
'#ajax' => array(
'callback' => 'crossdomain_form_callback',
'wrapper' => 'domains-fieldset-wrapper',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function crossdomain_form_callback($form, $form_state) {
return $form['domains_fieldset'];
}
function crossdomain_form_add_one($form, &$form_state) {
$form_state['num_domains']++;
$form_state['rebuild'] = TRUE;
}
function crossdomain_form_remove_one($form, &$form_state) {
$element_id = $form_state['clicked_button']['#parents'][2];
if ($form_state['num_domains'] > 1) {
$form_state['num_domains']--;
}
unset($form_state['values']['domains_fieldset']['element'][$element_id]);
$form_state['values']['domains_fieldset']['element'] = array_values($form_state['values']['domains_fieldset']['element']);
unset($form_state['input']['domains_fieldset']['element'][$element_id]);
$form_state['input']['domains_fieldset']['element'] = array_values($form_state['input']['domains_fieldset']['element']);
$form_state['rebuild'] = TRUE;
}
function crossdomain_form_submit($form, &$form_state) {
$items = array();
foreach ($form_state['values']['domains_fieldset']['element'] as $field) {
if (!empty($field['domain'])) {
$items[] = $field['domain'];
}
}
variable_set('crossdomain', $items);
}