hook_submit, hook_form_alter, hook_validate in Drupal 6.x

EDIT: 2011-02-07 更詳細的 hook_form_alter 例子

hook_submit() 是每一張form 提交的時候會經過的 hook
可以用作發電郵之類的自定義動作

類似的, hook_validate() 是驗證每一張 form 的時候會經過的hook
可以自定義更多驗證的 logic

而從Drupal 5.x 之中, hook_submit() 的 function callback 可以在form API 使用如下 syntax:

<?php
$form
['submit']['#submit'] = array('function1','function2');
?>

那 function1 和 function2 都會在 hook_submit() 之中callback 了

而 Drupal 6.x 之中, #submit 在不再在 submit button 之中定義:

<?php
$form
['#submit'][] = 'my_module_submit_handler'
?>

雖然 D5 的方法都可以, 但便不可以用 hook_form_alter() 從一個 module 修改另一個module 的 form 了

例如, contact form 需要一個contact number,
submit 時將 contact number 加到 body 之內:

<?php
function my_module_form_alter($form,&$form_statues) {
 
$form['#submit'][] = 'my_module_submit_handler';
}
function
my_module_submit_handler($form,&$form_statues){
 
//processing
}
?>

EDIT: 2011-02-07 更詳細的 hook_form_alter 例子

2010-03-04 更新到 Drupal 6.16

Drupal 6.16 update

更新發現了一個在icdsoft 實用的功能
就是resotre

因為我的 .htaccess 和 cron.php 不是 Drupal default 的,
我加了些少 logic, 更新以後便變回原本的版本
restore 可以 restore 最多 5 日的data,
而且可以 "單一/多個" "檔案/資料夾", click 一下便 restore 了, 方便!

UPDATE1: .htaccess in 6.16 had been modified as: http://drupal.org/node/638030

Drupal .htaccess block file access

In Drupal, in addition to hosting in icdsoft,
i have a php.ini file and maybe a backup folder(i do not have) that do not want unaurotized access

use .htaccess to do the job as:

<Files ~ "php\.ini|install\.php">
  Order allow,deny
  Deny from all
</Files>

which block php.ini and install.php from access by anyone from web

Mollem not properly configurated since last year?

Thank you Kay.L for pointing this out...

Sorry readers....

2010-02-22 Poor-ly structured Drupal site ever seen

Drupal template system is so flexible that you can do anything in node.tpl.php

This let chaos happens in a site i maintain, which made my pull my hair out of my head. (sort of)

That is a classic example that more power bring a larger disaster.
The developer that leaves the project even define a form , its hook_validate() and its hook_submit() inside the node.tpl.php. TOTALLY INSANE.

As a professional developer, not a part time one, YOU SHOULD TAKE RESPONSIBILITY TO YOUR CODE!!
so, you should:

  • avoid any node_load() in *.tpl.php (should be in custom module or preprocess function)
  • avoid any complex PHP code in custom block content, and visibility settings. Build a custom module will only cost you 5 mins
  • multi site settings should be carefully planned
  • DOCUMENTATION on your magic tricks
  • 山寨就是貌似做到一樣, 但細緻就不行了

God bless me, please open new projects instead of fixing old ones.

Pages

Google