主题、验证、提交函数的调用次序

你已经看到,在Drupal中,有多个地方可以用来放置你的主题、验证、提交函数。拥有这么多的选项会让人选择,到底要选择哪个函数呢?下面是Drupal查找位置的总结,这里按先后顺序排列,对于一个主题函数,假定你使用基于PHPTemplate的名为bluemarine的主题,并且你正在调用drupal_get_form('formexample_nameform')。然而,这还取决于你的hook_theme()实现。

    首先,如果在表单定义中将$form['#theme']设置为了'foo':
 
1. themes/bluemarine/foo.tpl.php // Template file provided by theme.
2. formexample/foo.tpl.php // Template file provided by module.
3. bluemarine_foo() // Function provided theme.
4. phptemplate_foo() // Theme function provided by theme engine.
5. theme_foo() // 'theme_' plus the value of $form['#theme'].
 
    然而,如果在表单定义中没有设置$form['#theme']:
 
1. themes/bluemarine/formexample-nameform.tpl.php // Template provided by theme.
2. formexample/formexample-nameform.tpl.php // Template file provided by module.
3. bluemarine_formexample_nameform() // Theme function provided by theme.
4. phptemplate_formexample_nameform() // Theme function provided by theme engine.
5. theme_formexample_nameform() // 'theme_' plus the form ID.
 
    在验证期间,表单验证器的设置次序如下:
1. A function defined by $form['#validate']
2. formexample_nameform_validate // Form ID plus 'validate'.
 
    当需要查找处理表单提交的函数时,查找的次序如下:
1. A function defined by $form['#submit']
2. formexample_nameform_submit // Form ID plus 'submit'.
 

    注意,表单可以有多个验证和提交函数。

老葛的Drupal培训班 Think in Drupal

Drupal版本: