Question

I need to do a REST request every time a new user signs up.

The REST request returns some data that I need to store for the Drupal user account, and be able to present as content somewhere.

What is the best/cleanest way to do that?

Was it helpful?

Solution

You can do it in a simple custom module, be sure to first understand Drupal hooks system as this is the base for (almost) everything Drupal.

In your module, you can implement hook_user_presave() to be called before a user account is saved. It will be called for both creation and update, so you will need to guard your code with a condition to detect new user. After the REST call, there is several option to store the returned data. The easiest one is the use the data attribute of the user account. It is a simple PHP array that will be serialized in a database columns. It is not very good performande wise and not very flexible (you can query the stored data) but it is easy.

function MODULE_user_presave(&$edit, $account, $category) {
  // Accoding to user_save's documentation, this is a new user if
  // $account->is_new is set to TRUE or if $account->uid is not set
  if ($category == 'account' && (!empty($account->is_new) || !isset($account->uid)))) {
    $edit['data']['MODULE'] = MODULE_get_data_from_rest($account);
  }
}

OTHER TIPS

A core feature in Drupal 7 is available that can trigger a specific action if a user is created, etc. You don't have to code to achieve this:

  • Enable the module Triggers
  • Go to Configuration >> Actions and create an action available on the dropdown list
  • Go to Structure >> Triggers, click on the User tab and assign the action you created awhile back to Trigger: After creating a new user account

The Rules or Workflow modules may be what you are looking for.

Licensed under: CC-BY-SA with attribution
Not affiliated with drupal.stackexchange
scroll top