滚动
在 Drupal 中创建模块,快速入门
我们从 api.drupal.org 开始,打开钩子 hook_node_presave() 的页面,它会在节点(node)添加之前触发。
http://api.drupal.org/api/drupal/modules!node!node.api.php/function/hook_node_validate/7
钩子允许我们将自定义代码嵌入 Drupal 的正常执行流程中,例如添加验证、数据字段、表单元素等。
在目录 sites/all/modules
中添加我们的模块:
sites/all/modules/custom/ sites/all/modules/custom/custom.info sites/all/modules/custom/custom.module
在 custom.info 文件中写入以下内容:
name = Custom description = Custom core = 7.x
在 custom.module 文件中,我们添加一个检查,判断内容字段是否为空。如果字段为空,则显示错误信息(当然,我们也可以在 Drupal 管理界面中将该字段设置为必填项,但这里通过代码实现):
custom.module
<?php function custom_node_validate($node){ if(empty($node->body['und'][0]['value']()