By jorje29 on
Below, I have 2 ahah functions for teams and players. Everything is working. How can I merge them and make player fields nested into team fields without losing the functionality ? I have failed to do that, although I have been working on this a serious amount of time.
Any help is really highly appreciated... Thanx in advance
<?php
function jorje29_menu() {
$items['teamadd/js'] = array(
'title' => 'Javascript team Form',
'page callback' => 'team_add_js',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
$items['playeradd/js'] = array(
'title' => 'Javascript team Form',
'page callback' => 'player_add_js',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
function jorje29_form(&$node, $form_state) {
$type = node_get_types('type', $node);
# league name
$form['title'] = array(
'#type' => 'textfield',
'#title' => check_plain($type->title_label),
'#description' => t('Give a name to your league'),
//'#required' => true,
'#default_value' => $node->title,
'#weight' => -5,
);
# description
$form['description'] = array(
'#type' => 'textarea',
'#title' => 'Description',
'#description' => t('Write a short description for your league'),
'#maxlength' => 255,
'#rows' => 3,
'#default_value' => '',
'#weight' => -4,
);
# smooth add
$form['set'] = array(
'#type' => 'fieldset',
'#title' => t('Teams'),
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
);
# Container for more teams
$form['set']['teamname'] = array(
'#prefix' => '<div id="anotherteam">',
'#suffix' => '</div>',
);
#assign value to team_count
if (isset($form_state['team_count'])) {
$team_count = $form_state['team_count'];
}
else {
$team_count = 1;
}
for ($delta = 0; $delta < $team_count; $delta++) {
# pass default values
$team = isset($node->teamname[$delta]['name']) ? $node->teamname[$delta]['name'] : '';
//$desc = isset($node->teamname[$delta]['desc']) ? $node->teamname[$delta]['desc'] : '';
# create teams.
$form['set']['teamname'][$delta] = _team_add_form($delta, $team);
}
# for smooth effect we include this wrapper
$form['set']['pset'] = array(
'#type' => 'fieldset',
'#title' => t('Players'),
'#description' => t('Add players to your team'),
'#collapsible' => true,
'#collapsed' => true,
//'#tree' => true,
'#prefix' => '<div class="clear-block">',
'#suffix' => '</div>',
);
# Container for more players
$form['set']['pset']['playername'] = array(
'#prefix' => '<div id="anotherplayer">',
'#suffix' => '</div>',
);
if (isset($form_state['player_count'])) {
$player_count = $form_state['player_count'];
}
else {
$player_count = 1;
}
for ($z = 0; $z < $player_count; $z++) {
$player = isset($node->pset['playername'][$z]['pname']) ? $node->pset['playername'][$z]['pname'] : '';
$form['set']['pset']['playername'][$z] = _player_add_form($z, $player);
if ( $z == $player_count-1 ) {
$form['players_number'] = array(
'#type' => 'value',
'#value' => $z,
);
}
}
$form['set']['pset']['add'] = array(
'#type' => 'submit',
'#value' => t('Add Player'),
'#submit' => array('player_add_submit'),
'#ahah' => array(
'path' => 'playeradd/js',
'wrapper' => 'anotherplayer',
'method' => 'replace',
'effect' => 'fade',
),
);
# ahah
$form['set']['add'] = array(
'#type' => 'submit',
'#value' => t('Add Team'),
'#submit' => array('team_add_submit'), // If no javascript action.
'#ahah' => array(
'path' => 'teamadd/js',
'wrapper' => 'anotherteam',
'method' => 'replace',
'effect' => 'fade',
),
);
return $form;
}
function team_add_submit($form, &$form_state) {
node_form_submit_build_node($form, $form_state);
$form_state['team_count'] = count($form_state['values']['teamname'])+1;
$form_state['player_count'] = count($form_state['values']['pset']['playername']);
}
function player_add_submit($form, &$form_state) {
node_form_submit_build_node($form, $form_state);
$form_state['player_count'] = count($form_state['values']['pset']['playername'])+1;
$form_state['team_count'] = count($form_state['values']['teamname']);
}
function _team_add_form($delta, $team = '') {
$form['set']['team'] = array(
'#type' => 'fieldset',
);
$form['set']['team'][$delta] = array(
'#type' => 'textfield',
'#title' => t('Team @n', array('@n' => ($delta + 1))),
'#default_value' => $team,
'#parents' => array('teamname', $delta, 'name'),
);
$form['teams_number'] = array (
'#type' => 'value',
'#value' => $delta,
);
return $form;
}
function _player_add_form($z, $player = '') {
$form['set']['pset']['playername'] = array(
'#type' => 'textfield',
'#title' => t('Player @n', array('@n' => ($z + 1))),
'#default_value' => $player,
'#maxlength' => 50,
'#parents' => array('pset', 'playername', $z, 'pname'),
);
return $form;
}
function player_add_js() {
include_once 'modules/node/node.pages.inc';
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
$player_form = $form['set']['pset']['playername'];
unset($player_form['#prefix'], $player_form['#suffix']); // Prevent duplicate wrappers.
$output = drupal_render($player_form);
drupal_json(array('status' => TRUE, 'data' => $output));
}
function team_add_js() {
include_once 'modules/node/node.pages.inc';
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
// We will run some of the submit handlers so we need to disable redirecting.
$form['#redirect'] = FALSE;
// We need to process the form, prepare for that by setting a few internals
// variables.
$form['#post'] = $_POST;
$form['#programmed'] = FALSE;
$form_state['post'] = $_POST;
// Build, validate and if possible, submit the form.
drupal_process_form($form_id, $form, $form_state);
// This call recreates the form relying solely on the form_state that the
// drupal_process_form set up.
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
// Render the new output.
$team_form = $form['set']['teamname'];
unset($team_form['#prefix'], $team_form['#suffix']); // Prevent duplicate wrappers.
$output = drupal_render($team_form);
drupal_json(array('status' => TRUE, 'data' => $output));
}
?>