How to assign role with PHP in Drupal?

前端 未结 4 2075
误落风尘
误落风尘 2021-02-06 16:38

I need to assign a kind of \"premium status\" to members that purchase an amount equal or greater than 100 € via PHP.

Conditional Actions are already set up (user = anon

4条回答
  •  忘了有多久
    2021-02-06 17:11

    for drupal 7:

         global $user; // Retrieve user object for currently logged in user.
    
      $role_name = 'test role'; // Name of role to add
    
      // Get RID of role
      $rid = db_query("SELECT r.rid FROM {role} r WHERE r.name = :rname", array(':rname' => $role_name))->fetchField();
    
      // Save the user object with the new role.
      if (!isset($user->roles[$rid])) {
        $roles = $user->roles + array($rid => $role_name);
        user_save($user, array('roles' => $roles));
      }
    

    thanks rix.

提交回复
热议问题