php - how can I do some code before I build the form in Drupal 7 (kinda preprocess)? -
i'd ask how can code before build form in drupal 7? before defining form i'd perform code(i want build object), how can it?
the code want perform:
if (isset($_get["team"])){$team = $_get["team"];} else {$team=1; }; $myteam = new team($team);
i define form:
function teamform_nameform() { $form['editteam']['team_city'] = array( '#title' => t('team city'), '#type' => 'textfield', '#description' => t(''), '#required' => true, '#default_value' =>**$myteam->returncity()**, '#size' => 30, ); $form['editteam']['submitcontentchanges'] = array( '#type' => 'submit', '#value' => t('save changes'), '#submit' => array('teamform_editteam_submitcontentchanges'), ); }
i tried use following hook, doesn't work. (i still can't access variable $team
, object $myteam
(it's written undefined))
/** * implements hook_form_alter(). */ function teamform_form_alter(&$form, &$form_state, $form_id) { global $team; if (isset($_get["team"])){$team = $_get["team"];} else {$team=2;}; global $myteam $myteam = new team($team); }
$team team id using method or if it's not set assign default value. $myteam object build based on team_id. i'd access object in function teamform_nameform(). in function use method returncity() in order return city team belongs to; default value. i'd make changes object. specifically, when user changes city of team , click submit button want update city in object $myteam. therefore use function:
function teamform_editteam_submitcontentchanges($form, &$form_state){ $team_city=$form_state['values']['team_city']; $myteam->updateteamcity($team_city); //i got error here. it's said $myteam undefined! }
it looks want object persist across page loads, sound right? if that's case, can store in session. try this:
/** * implements hook_form_alter(). */ function teamform_form_alter(&$form, &$form_state, $form_id) { $team = isset($_get["team"]) ? $_get["team"] : 2; $_session['team'] = new team($team); }
function teamform_editteam_submitcontentchanges($form, &$form_state){ $team_city = $form_state['values']['team_city']; $myteam = $_session['team']; $myteam->updateteamcity($team_city); }
Comments
Post a Comment