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

 Call to a member function id() on a non-object in UserRoleAdd.php on line 49

I've also tried the user-readable role name (cleared cache each time after saving).

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Daniel Schaefer created an issue. See original summary.

Daniel Schaefer’s picture

Issue summary: View changes
yanniboi’s picture

Status: Active » Needs review
FileSize
921 bytes

Yea 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.

yanniboi’s picture

I 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.

Daniel Schaefer’s picture

With 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.

Fatal error: Call to a member function id() on a non-object in /home/d09ln/www/sites/default/modules/rules/src/Plugin/RulesAction/UserRoleAdd.php on line 55

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)

yanniboi’s picture

Issue summary: View changes
FileSize
18.39 KB
16 KB

Cool I think found it.

It says in the comments of UserRoleAdd.php (http://cgit.drupalcode.org/rules/tree/src/Plugin/RulesAction/UserRoleAdd...):

<?php
        // If you try to add anonymous or authenticated role to user, Drupal
        // will throw an \InvalidArgumentException. Anonymous or authenticated
        // role ID must not be assigned manually.
?>

So I changed the role being assigned to 'administrator' and it worked:

jonathan1055’s picture

This 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

Fatal error: Call to a member function id() on a non-object in /Library/WebServer/Documents/drupal8/modules/rules/src/Plugin/Condition/UserHasRole.php on line 61

This is because $role is a string containing the role text entered when editing the condition, not an object. In /Condition/UserHasRole.php

protected function doEvaluate(UserInterface $account, array $roles, $operation = 'AND') {
    
    $rids = array_map(function ($role) {
       return $role->id();  // $role is a text string, not an object.
    }, $roles);

In my case, the solution was to change

       return $role->id();

into

       return trim($role);  

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]

jonathan1055’s picture

Here'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.

Status: Needs review » Needs work

The last submitted patch, 8: 2725525-8.fatal_error_call_to_role_id.patch, failed testing.

jonathan1055’s picture

Old dev. Here's a patch again latest dev alpha1+21

Status: Needs review » Needs work

The last submitted patch, 10: 2725525-10.fatal_error_call_to_id_of_role.patch, failed testing.

jonathan1055’s picture

try again

Status: Needs review » Needs work

The last submitted patch, 12: 2725525-12.fatal_error_call_to_role_id.patch, failed testing.

jonathan1055’s picture

Status: Needs work » Needs review
FileSize
1.55 KB

Did not apply due to incorrect case of file names. Should be /Plugin/Condition not /plugin/condition. Hope this one works.

fago’s picture

Status: Needs review » Closed (duplicate)

Thanks 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.

jonathan1055’s picture

OK 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

brooke_heaton’s picture

Is 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:

langcode: en
status: true
dependencies: {  }
id: assign_roles_on_register
label: 'assign roles on register'
events:
  -
    event_name: 'rules_entity_insert:user'
description: ''
tags:
  - ''
config_version: '3'
expression:
  id: rules_rule
  uuid:
  conditions:
    id: rules_and
    uuid: 
    conditions: {  }
  actions:
    id: rules_action_set
    uuid: 
    actions:
      -
        id: rules_action
        uuid: 
        context_values:
          roles:
            - nia1
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
      -
        id: rules_action
        uuid: 
        context_values:
          roles:
            - dh1
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
      -
        id: rules_action
        uuid: 
        context_values:
          roles:
            - sdp1
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
      -
        id: rules_action
        uuid: 
        context_values:
          roles:
            - dl1
        context_mapping:
          user: user
        context_processors:
          roles:
            rules_tokens: {  }
        provides_mapping: {  }
        action_id: rules_user_role_add
doxigo’s picture

I 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'

millionleaves’s picture

I 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.

TR’s picture

The 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.

Daniel Korte’s picture

#14 is missing a return

jonathan1055’s picture

I 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

jonathan1055’s picture

pozo’s picture

I'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.

Youcanlearnit’s picture

I 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