Early Bird Registration for DrupalCon Portland 2024 is open! Register by 23:59 PST on 31 March 2024, to get $100 off your ticket.
Trying to set up a role on event "After saving new user (rules_entity_insert:user)" getting this:
Fatal error: Call to a member function id() on a non-object in /home/d094k/www/sites/default/modules/rules/src/Plugin/RulesAction/UserRoleAdd.php on line 49
here is my setup
I've also tried the user-readable role name (cleared cache each time after saving).
Comment | File | Size | Author |
---|
Comments
Comment #2
Daniel Schaefer CreditAttribution: Daniel Schaefer commentedComment #3
yanniboi CreditAttribution: yanniboi at FreelyGive commentedYea this seems to be broken... The Action expects a loaded role entity to be passed in, not just the id (ie. authenticated). This is pretty difficult to fix through the UI, so I suggest the the action loads to role if an object isnt passed in.
Comment #4
yanniboi CreditAttribution: yanniboi at FreelyGive commentedI could do with getting some feedback from maintainers about this before creating a pull request and getting this fixed. There may be another way they want this handled.
Comment #5
Daniel Schaefer CreditAttribution: Daniel Schaefer commentedWith id provided the rule fires fine now, dislaying my site message but it didn't apply the role to the user. When providing role name instead of id, the same error displays but now with "line 55". Rebuilt cache after every rule change.
in case anyone likes to check here's my simplytest.me: https://d09ln.ply.st/admin/config/workflow/rules/reactions/edit/user_reg.... (should be available for another good 23 hours from now - user: test p/w: 123)
Comment #6
yanniboi CreditAttribution: yanniboi at FreelyGive commentedCool I think found it.
It says in the comments of UserRoleAdd.php (http://cgit.drupalcode.org/rules/tree/src/Plugin/RulesAction/UserRoleAdd...):
So I changed the role being assigned to 'administrator' and it worked:
Comment #7
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedThis is not exactly the same, but very related, and you may wish to consider the fix of this with the above.
When adding a condition to check that a user has a role, we get
This is because $role is a string containing the role text entered when editing the condition, not an object. In /Condition/UserHasRole.php
In my case, the solution was to change
into
The trim is necessary to ensure array_intersect works as required (probably a cr or lf character which needs to be removed).
[edit: removed a second reported error, which was not a problem once the 'trim' had been added]
Comment #8
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedHere's a patch which has the change in #3 and fixes the problem in #7. Probably not the ultimate final answer, but it allows the condition to be used, which is important for 3rd-party contrib modules when developing and testing actions and conditions for the Rules module.
Comment #10
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedOld dev. Here's a patch again latest dev alpha1+21
Comment #12
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedtry again
Comment #14
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedDid not apply due to incorrect case of file names. Should be
/Plugin/Condition
not/plugin/condition
. Hope this one works.Comment #15
fagoThanks for the report and the patch. However, the patch just is a band-aid - the cause of this is a missing feature: #2800749: Support upcasting entity IDs to full entity contexts. Thus, we need to resolve the issue over there instead. Given that, I mark this as a duplicate.
Comment #16
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedOK thanks for the info. Yes my addition to the patch was a sticking-plaster, but it was needed for me to continue with developing actions, condition and events for Rules integration. Without it you get fatal errors which halted our work, and thus slowed development.
Good work on the overall Rules development - keep going, we appreciate the progress.
Jonathan
Comment #17
brooke_heaton CreditAttribution: brooke_heaton as a volunteer commentedIs this still an issue? I'm getting this error when trying to apply a role to a user in rules_entity_insert:
php-error ded-19747 [22-May-2018 13:38:39 America/New_York] PHP Fatal error: Call to a member function id() on string in /mnt/www/html/mysite/docroot/modules/contrib/rules/src/Plugin/RulesAction/UserRoleAdd.php on line 52 request_id="v-f6453e66-5de6-11e8-a9bd-0e21b719bad6"
My rule:
Comment #18
doxigo CreditAttribution: doxigo commentedI applied patch #14 and the error is gone for now but the thing is, it doesn't apply the role at all after 'saving a new user'
Comment #19
millionleaves CreditAttribution: millionleaves as a volunteer and commentedI followed the link to https://www.drupal.org/project/rules/issues/2800749 (see #15) but the latest patch in that issue didn't solve the problem of not being able to assign a role when saving a user.
I then applied the patch in #14 and it now works. I don't know whether I needed the patch from 2800749 to make this patch work.
I'm using Drupal 8.6.3 and the latest -dev version of Rules from September 17 2018.
Comment #20
TR CreditAttribution: TR commentedThe status of #2800749: Support upcasting entity IDs to full entity contexts is "Needs work" - there is no solution for that yet. When that gets solved, then this problem will be fixed.
The patch from #14 is just a temporary work-around and is not a long term solution. Use it if you must.
Comment #21
Daniel Korte#14 is missing a return
Comment #22
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commentedI have now added a generic upcast function on #2800749-36: Support upcasting entity IDs to full entity contexts so anyone having the problem listed above - please test patch #36 over there. Thanks
Comment #23
jonathan1055 CreditAttribution: jonathan1055 as a volunteer commented#2800749: Support upcasting entity IDs to full entity contexts has been committed.
Comment #24
pozo CreditAttribution: pozo as a volunteer commentedI've solved it for myself, with Drupal 9.3.3 and Rules 8.x-3.0-alpha7, by replacing "$role->id()" with "$role", on lines 54 and 59 of \src\Plugin\ RulesAction\UserRoleAdd. php.
It works, but you can add roles that don't exist.
Comment #25
Youcanlearnit CreditAttribution: Youcanlearnit as a volunteer commentedI have still this issue when removing and adding roles.
I have Rules 8.x-3.0-alpha7
and patches
https://www.drupal.org/files/issues/2021-12-15/2800749-102.user-restrict...
https://www.drupal.org/files/issues/2021-10-15/3243962-3-traversable.patch
PHP 8.1
EDIT: Had to add also this patch, and role deleting and adding works
https://www.drupal.org/files/issues/2021-12-08/2800749-97.upcast.patch