- Issue created by @alina_herbst
- 🇩🇪Germany killah89 Schneverdingen
Try to use "Switch user account" and choose the user with privileged permissions to set a role, instead of "execute models with user".
- 🇩🇪Germany jurgenhaas Gottmadingen
Agreed, @killah89 is right. That setting to globally execute ECA as a specific user is not longer considered a good idea. It comes with too many negative side effects. Switching user explicitly when needed is much better.
And on a separate note: when you start with the "Update entity" event and then update that entity again, you run into a recursion. That's what you also want to avoid. You should instead start with the "Presave entity" event.
- 🇩🇪Germany alina_herbst
Aha ok, thanks for the hints. Unfortunately, I still can't get it to work, and I don't understand why. Here's my model with an attempt to assign the content_editor role. Can anyone please have a look?
I expected that after updating a user, they would receive the role unconditionally.
- 🇩🇪Germany jurgenhaas Gottmadingen
There are a few issues with that model:
- When you use the pre-save content entity event, you need to avoid that this entity will be saved in the subsequent actions, because you can't save an entity while it is already in the process of being saved.
- That means you can't use the "Add user role" action because that would save the entity.
- And you can use the "Save entity" action. The entity will be saved anyway, that's what the whole context of that pre-save event actually is.
- When you switch the user, the user token will be that new user and no longer the one that you want to add a role to.
- Therefore you need to save the user entity to a token first, then switch to the other user, and then add the role to the user in your own token.
The good news is, there is a complete sample model which does exactly that, see Add role to inserted or updated user.
- 🇮🇳India prabha1997
@jurgenhaas with out storing token value it will work
here i have implemented same model please check this - 🇩🇪Germany alina_herbst
@jurgenhaas thanks for looking and the informations. I see I have to learn to use properly the right event. I will have a look at the linked sample model.
@prabha1997 your model presented in #7 works for me. I was surprised to see that your action "Entity: set field value" also works.
- 🇩🇪Germany alina_herbst
The sample model in #6 also works with the action "Entity: set field value" to add the role. That's fine but I wonder for what purpose the role related actions like "add a defined role" are usable. Isn't this action obsolete in faour of "Entity: set field value")
@jurgenhaas I found your explantion of the entity related events: https://www.drupal.org/project/eca/issues/3307106#comment-14676850 → but not at https://ecaguide.org/ whre I looked before. Did I miss it there?
- 🇩🇪Germany jurgenhaas Gottmadingen
I wonder for what purpose the role related actions like "add a defined role" are usable. Isn't this action obsolete in faour of "Entity: set field value")
Well, everybody can develop and distribute action plugins and we can't prevent anyone from doing this. Actually, we do want to encourage people to write more action plugins to grow the ecosystem.
The one you're struggling with is from Drupal core. And it has been created maybe 2 decades ago and is used in the people list of all Drupal sites, where you can apply a bulk action to all selected users and give them the selected role. The difference there is that the action needs to add the role AND (!!!) save the result to the user entity. In your context of "Entity pre-save" that's what you don't need.
I know, that's a lot what people need to know about scope and context of action plugins.
And when it comes to documentation to help with all that, we're always looking for help to improve that with all the knowledge which redies in this issue queue and in the Slack channel.
I'm closing this issue now as it seems to have been resolved.