Fail when cascading creating user + adding user to a role

Events happening in the community are now at Drupal community events on www.drupal.org.
Visgario's picture

This afternoon, I tried to automate user creation. Users are clients and I want to have their account created from their CRM details (content type # Client).

So, I tried this rule:

1- event: after saving new content
2- condition: content type = Client
3 - actions
a- create new entity "User". Username = mail, mail = mail
b- add user role (parameter: entity-created) + specify a role

It looks OK, but when I try to create a new "client", I have the following error message:
"Notice : Undefined property: stdClass::$uid dans rules_action_user_add_role() (line 45 in /siteroot/sites/all/modules/rules/modules/user.eval.inc)."

The user is correctly added to the role anyway. But I can't provide the functionnality this way to my users.

In a first time, I suppposed it was a bad idea to cascade user creation then user management. But it works fine when adding data to user instead of adding roles.

So ... Does any one has an idea about what's wrong ? Is there a bug in Rules ? Or is the problem between my chair and my keyboard :-) ?

Comments

There are two things

ohthehugemanatee's picture

There are two things user_add_role() needs to do it's job: UID of the targeted user, and the role ID of the targeted role. So this notice is complaining that $uid isn't properly defined when it calls the function.

I'm going to assume that you have a good PHP version etc, or at least that you've looked into that.

So we have to figure out why UID isn't getting defined... or just try and define it yourself. Try including an entity save in between the entity_create and user_add_role. Or separate the user_add_role into a rule component on its own. I would provide UID to the new component as a variable, and include a condition that checks to make sure the variable exists.

Then I would enable the debug log, and see what comes up. If the UID isn't making it into a variable, you'll see that in the log. It may be that explicitly assigning it to a variable defines it for the function.

Gosh, it works

Visgario's picture

Thanks a lot for your answer. And thanks a lot more for providing a solution that works !

I have a good PHP version (5.3.8). That's it.
I tried your magical "save entity before adding role". It works.

So ... I was the bug :-( It seems I still got to learn about Rules.

As for me, the topic is closed

In case anyone else is having

soulston's picture

In case anyone else is having this issue I found that I also had to force the saving of the entity immediately in the rules setup (Force saving immediately: true)

yes I have

bbruda's picture

Yes, I have the some issue...

Notice: Undefined property: stdClass::$uid in rules_action_user_add_role() (line 45 of /sites/all/modules/rules/modules/user.eval.inc).

Notice: Undefined property: stdClass::$uid in rules_action_user_add_role() (line 52 of /sites/all/modules/rules/modules/user.eval.inc).

Created a patch

lauriii's picture

I created a patch for this, you can find it from here: https://drupal.org/node/2014459

Save entity immediately works

angus_muir's picture

perfect

Works

ojchris's picture

Worked seamlessly for me

Rules

Group organizers

Group categories

Categories

Group notifications

This group offers an RSS feed. Or subscribe to these personalized, sitewide feeds: