在 Drupal 7 中构建表单之前如何执行一些代码(有点预处理)?

发布于 2024-11-25 11:26:34 字数 1609 浏览 1 评论 0原文

我想问一下在Drupal 7中构建表单之前如何编写一些代码? 在定义表单之前,我想执行一些代码(我想构建一个对象),我该怎么做?

我想要执行的代码:

if (isset($_GET["team"])){$team = $_GET["team"];} else {$team=1; };
$myteam = new Team($team);

我定义了形式:

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'),
);
}

我尝试使用以下钩子,但它不起作用。 (我仍然无法访问变量$team和对象$myteam(据说它们是未定义的))

/** 
 *  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是我得到的团队ID使用 GET 方法,或者如果未设置,则分配默认值。 $myteam 是我根据 team_id 构建的对象。 我想访问函数 teamform_nameform() 中的对象。在此函数中,我使用 returnCity() 方法来返回团队所属的城市;作为默认值。 我想对对象进行更改。具体来说,当用户更改团队的城市并单击提交按钮时,我想更新对象 $myteam 中的城市。因此我使用该功能:

function teamform_editteam_submitContentChanges($form, &$form_state){
  $team_city=$form_state['values']['team_city'];
  $myteam->updateTeamCity($team_city); //i got the error here. it's said that $myteam is undefined!
}

I'd like to ask how can I do some code before I build the form in Drupal 7?
Before defining a form I'd like to perform some code(I want to build an object), How can I do it?

The code i want to perform:

if (isset($_GET["team"])){$team = $_GET["team"];} else {$team=1; };
$myteam = new Team($team);

I define the 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 to use the following hook, but it doesn't work. (i still can't get access to variable $team and to the object $myteam (it's written that they are 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 is the team id which i get using method GET or if it's not set than i assign the default value. $myteam is a object which i build based on the team_id.
Than I'd like to get access to the object in the function teamform_nameform(). In this function i use method returnCity() in order to return the city which the team belongs to; as a default value.
Than I'd like to make changes with the object. Specifically, when the user changes the city of the team and click submit button then i want to update the city in the object $myteam. Therefore i use the function:

function teamform_editteam_submitContentChanges($form, &$form_state){
  $team_city=$form_state['values']['team_city'];
  $myteam->updateTeamCity($team_city); //i got the error here. it's said that $myteam is undefined!
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光磨忆 2024-12-02 11:26:34

看起来您希望一个对象在页面加载过程中保持不变,这听起来对吗?如果是这种情况,您可以将其存储在会话中。尝试这样的事情:

/** 
 *  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);
}

It looks like you want an object to persist across page loads, does that sound right? If that's the case, you can store it in the session. Try something like 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);
}
若相惜即相离 2024-12-02 11:26:34

hook_form_alter 允许您在呈现表单之前对其进行修改,包括添加 #default_value 属性。

hook_form_alter allows you to modify the form before it's rendered including adding the #default_value property.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文