2

I'm working on a custom module that edits the user's details. I'm using

user_save($user_entity,$new_field_values);

But I see there is a third argument in user_save called "category". What exactly is it for?

1

$category is one of the strings returned from any implementations of hook_user_categories(). Think of them as a different tab that is visible in the form for editing a user profile.

By default, the User module defines just the "account" category, which contains all the data about the user account.

function user_user_categories() {
  return array(array(
      'name' => 'account', 
      'title' => t('Account settings'), 
      'weight' => 1,
    ));
}

Other modules can define new categories. In Drupal 6, the Profile module was one of those modules, which implemented hook_user_categories() with profile_user_categories().

function profile_user_categories() {
  $result = db_query("SELECT DISTINCT(category) FROM {profile_field}");
  $data = array();
  foreach ($result as $category) {
    $data[] = array(
      'name' => $category->category, 
      'title' => $category->category, 
      'weight' => 3, 
      'access callback' => 'profile_category_access', 
      'access arguments' => array(1, $category->category),
    );
  }
  return $data;
}
1

As you see in user_save function it tells that

category: (optional) The category for storing profile information in.

It basically used in below lines of code inside user_save function

user_module_invoke('presave', $edit, $account, $category);
user_module_invoke('update', $edit, $account, $category);
user_module_invoke('insert', $edit, $account, $category);

So, the information is just passed to all presave, update, insert hooks so that they can take contextual actions.

In user_module_invoke function $category is defined as

$category: The category of user information being acted upon.

So, from above it clarifies that is used as a value to tell all hooks that we are saving user details and on which part of user information we are acting upon. Something like if we change the user organic groups that he belongs to then we can specify that $category as "organic_group" .

For Example if you see hook_user_presave in user module

/**
 * Implements hook_user_presave().
 */
function user_user_presave(&$edit, $account, $category) {
  if ($category == 'account' || $category == 'register') {
    if (!empty($edit['picture_upload'])) {
      $edit['picture'] = $edit['picture_upload'];
    }
    // Delete picture if requested, and if no replacement picture was given.
    elseif (!empty($edit['picture_delete'])) {
      $edit['picture'] = NULL;
    }
    // Prepare user roles.
    if (isset($edit['roles'])) {
      $edit['roles'] = array_filter($edit['roles']);
    }
  }

  // Move account cancellation information into $user->data.
  foreach (array('user_cancel_method', 'user_cancel_notify') as $key) {
    if (isset($edit[$key])) {
      $edit['data'][$key] = $edit[$key];
    }
  }
}

You can see the use case of $category above where they check for account or register !

In your case just make it as an optional and don't assign any value :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.