Php Drupal 7:hook\u entity\u insert($entity,$type)

Php Drupal 7:hook\u entity\u insert($entity,$type),php,drupal,drupal-7,Php,Drupal,Drupal 7,我是一名noob少年,所以如果这是一个非常基本的问题,并且之前被问过无数次,我会提前道歉 我基本上是在用户注册时运行另一个函数。在谷歌搜索之后,我发现:hook\u entity\u insert$entity,$type from now,即使有代码示例,它也不会告诉我将代码放在哪里,如何获取提交的数据等等 我应该将示例代码放在哪个文件中进行测试。提供的示例代码是: function hook_entity_insert($entity, $type) { // Insert the n

我是一名noob少年,所以如果这是一个非常基本的问题,并且之前被问过无数次,我会提前道歉

我基本上是在用户注册时运行另一个函数。在谷歌搜索之后,我发现:hook\u entity\u insert$entity,$type from now,即使有代码示例,它也不会告诉我将代码放在哪里,如何获取提交的数据等等

我应该将示例代码放在哪个文件中进行测试。提供的示例代码是:

function hook_entity_insert($entity, $type) {

  // Insert the new entity into a fictional table of all entities.
  $info = entity_get_info($type);
  list($id) = entity_extract_ids($type, $entity);
  db_insert('example_entity')
    ->fields(array(
    'type' => $type,
    'id' => $id,
    'created' => REQUEST_TIME,
    'updated' => REQUEST_TIME,
  ))
    ->execute();
} 

首先,您应该了解Drupal中的钩子系统。对于Drupal7来说,这个页面是一个好的开始。它为您提供了对该概念的快速概述和理解

有一个特定的钩子在用户被插入后“激发”,名为

,您不需要使用钩子实体插入。在自定义模块中,使用下面的钩子 当用户注册时

   function yourModuleName_form_user_register_alter(&$form, &$form_state) {
    // Add your own function to the array of validation callbacks
    $form['#validate'][] = 'yourModuleName_user_register_validate';
   }
提及

如果要在用户注册后运行函数,请使用hook\u user\u insert,或者如果需要在每次用户更改时运行,请使用hook\u user\u pressave

一般来说:drupal中的钩子是符合特定命名方案的函数。在执行钩子的地方,即在用户注册时,Drupal搜索包含函数名由模块的机器名组成的所有模块,然后是钩子名。对于钩子用户插入,您需要实现一个模块或将代码放在已经实现的模块中,请参阅文档。假设您的模块名为custom_module,那么您可以实现如下功能:

function custom_module_user_insert(&$edit, $account, $category) {
  //Do what you wanted to do here
}
希望这有帮助