0
votes

I'm a noob junior so I apologise in advance if this is a very basic question and if it has been asked a gazillion times before.

I am basically trying to run another function when a user registers. After some googling I came upon: hook_entity_insert($entity, $type) from (https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_entity_insert/7.x) now, even though there are code examples it does not tell me where to put the code, how to get the data that is submitted etc...

Which file do I put the sample code to test. The sample code provided is:

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();
} 
3

3 Answers

1
votes

First you should understand the hook system in Drupal. For Drupal 7 this page is a good start. It gives you a quick overview and understanding of the concept.

Understanding the hook system for Drupal modules

There is a specific hook that 'fires' after an user is inserted, named hook_user_insert

0
votes

You don't need to use hook_entity_insert. In your custom module use below hook when user registers.

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

Refer Hook into Drupal registration and validate user info against business logic

0
votes

If you want to run a function after the user has registered, use hook_user_insert (or, if this needs to be run every time a user is changed, hook_user_presave).

In general: Hooks in drupal are functions that comply with a specific naming scheme. In the places where a hook is executed (i.e., on user registration), Drupal searches for all modules that contain a function where the function name consists of the module's (machine) name, followed by the hook name. For hook user insert, you would need to implement a module (or place your code in a module you already implemented), see documentation here. Supposing your module is called "custom_module", you then implement a function like so:

function custom_module_user_insert(&$edit, $account, $category) {
  //Do what you wanted to do here
}

Hope this helps