Problem/Motivation
I am attempting to send an email to a user that is specified by a reference field on the node in the user's preferred language. When I use data selection mode to select: node.field_my_userref.entity.preferred_langcode.language (object) I get the following error:
Expected a language data type for context Language but got a language_reference data type instead.
Steps to reproduce
1. Create a content-type
2. Add an entity reference -> user field called field_my_userref
3. Add a new reaction rule on content update
4. Add an action Send email
Parameters: to: [node.field_my_userref.entity.mail.value], subject: My subject, message: My email body, reply: '', language: node.field_my_userref.entity.preferred_langcode.language (object)
5. Hit save and error should appear.
Proposed resolution
Not sure, happy to provide a patch if someone can point me in the right direction.
Remaining tasks
User interface changes
API changes
Data model changes
Issue fork rules-3263463
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #2
joel_osc commentedComment #3
tr commentedThis seems to be a problem where you're passing a reference to a language object instead of a language object. That situation (reference instead of entity) is the subject of #3059402: Set data action in referenced entity.. You can try out the patch there to see if it fixes the problem for you.
In the future, please export your Rule and post it, rather than trying to describe how you configured the Rule. The export gives me all the information, while a description is always missing important information. But from your description, I think you entered
[node.field_my_userref.entity.mail.value]as the "To:" address, which is not valid. A good strategy when debugging is to print out the values in a "Show a message" action to see if the values you entered really have the values you expect them to have.Comment #4
joel_osc commentedHi @TR, thank-you for the quick reply - I will take a look at the issue you have referenced and see if I can get my rule working. Your help is much appreciated!
Sorry, about not including the rule - for reference here it is, thanks for finding the issue with the email address. It looked like it was working because I was debugging with email redirect on for testing. :(
We really appreciate all the work you maintainers are doing on this incredibly important module. Cheers.
Comment #5
joel_osc commentedI tried it with the patch and I still got the same error:
I am not sure how to either load a language object from a langcode or convert the existing language_reference data type to a language data type in Rules without writing a custom action. Any suggestions are appreciated.
Comment #6
jonathan1055 commentedHi joel_osc,
Yes you are right, there is a problem specifying a language via selection. I've done some investigating and cannot make it work for any of the actions or conditions that need a language.
Selecting @user.current_user_context:current_user.preferred_langcode gives
"Expected a language type ... but got a list data type instead."preferred_langcode.language gives
"... but got a language_reference data type instead."preferred_langcode.value gives
" ... but got a string data type instead."I was aware of this via my testing on #2471481: Integrate Typed Data Widgets and had been trying to solve it there, but now it seems it would be better to fix it separately to that issue.
Comment #7
joel_osc commentedHi Jonathon1055, thank-you for confirming! One option I can think of for a solution would be to change the param to accept a string (langcode) instead of a language object, since this is all it uses anyway, but I am afraid that might be a bit hackish. I was concerned about backwards compatibility, but now that you have confirmed the issue I don't think anyone can actually have a rule that uses the language option. So then the only issue would be making the same change in a couple of other actions that have the same issue.
Happy to provide a patch if people think this is a reasonable solution. Thank-you for all your work on rules!
Comment #8
jonathan1055 commentedYes that is exactly what I was thinking, I have discussed it with TR and have already started work on it. One action and one condition already coded and working locally.
You are right. The data selection gives the errors as in #6, and if using direct input text you get the error
Warning: preg_match_all() expects parameter 2 to be string, object given in Drupal\typed_data\PlaceholderResolver->scan() (line 197 of ___/typed_data/src/PlaceholderResolver.php).This would have been solved in the Typed Data Widgets issue by changing the context to "entity:configurable_language" but a simpler solution is to accept a text value of langcode.
I will finish the work to do all the actions and conditions and post it here.
Comment #9
jonathan1055 commentedHi joel_osc
I have added a new patch #170 on #2471481-170: Integrate Typed Data Widgets which should allow you to pick your
node.field_mgr_approver.entity.preferred_langcode.languagefield for the language in your rule. Would you be able to test that patch and let us know how it goes?Thanks
Comment #10
joel_osc commentedSure will do, thank-you so much!
Comment #11
joel_osc commentedHi @jonathon1055, your changes work - I can now add node.field_mgr_approver.entity.preferred_langcode.language for the language and if I place a dpm($langcode) in doExecute it is the correct language ('fr' in my case). The actual email is not sent in the correct language, but I am thinking it would be best to open a new issue on that.
Comment #12
jonathan1055 commentedHey, pleased to hear that the language selection works.
Yes, raise a separate issue for the mail not respecting the language setting. Although I am not sure what exactly should happen. The subject and body are given (hard-coded) in your rule, so are you expecting those to be translated before sending? I don't think that could work, unless those strings are already in the "translation" store for the language required. But anyway, raise the new issue and we'll discuss it there.
Comment #13
joel_osc commentedSounds good, I will now close this issue as a duplicate of #2471481: Integrate Typed Data Widgets as that patch now fixes this problem. Cheers.
Comment #14
tr commentedYeah,
rules_mail()needs to be modified to properly translate subject and body. These strings are stored in the reaction rule configuration, so they are available for translating, butrules_mail()needs to actually use the translation. It can do that by wrapping the subject and body int()and using the optional third argument tot()to specify the language code needed. It is allowed to uset()on a variable value here, because we know the string in that variable comes from configuration and so it exists in the translation table.An example is in https://api.drupal.org/api/drupal/core%21modules%21contact%21contact.mod...