This issue is at the cross roads of entity, entity_token, profile2 and realname, so I'm not sure which module should get fixed.
Here is my use case. I'm using realname for user profiles, and I want to use the nice entity/profil2/token integration for my usernames, using [user:profile-my-profile:field-lastname] [user:profile-my-profile:field-firstname]. When I create a new user account, and enter the first and last names in the profile2 fields of the registration form, the realname is set to empty. I debuged a little bit, and it turns out that the profile2 field data is not yet stored as entity (and thus not available for entity_token) when realname runs for the first time, so the realname of that profile is set to '' (empty). realname.module caches the realname value, so you are stuck with an empty username until you go and resave the user profile, at which point the realname is regenerated from the right values.
Any way to force the profile2 entity to be created/stored before the user gets saved? (and realname.module called).
| Comment | File | Size | Author |
|---|---|---|---|
| #99 | profile2-tokens-1097684-99.patch | 1.58 KB | rickj |
| #90 | profile2-tokens-1097684-90.patch | 945 bytes | Jacqs |
| #60 | interdiff.txt | 724 bytes | spleshka |
| #60 | profile2-tokens-1097684-60.patch | 6.22 KB | spleshka |
| #54 | interdiff.txt | 7.28 KB | spleshka |
Comments
Comment #1
fagothis is more profile2 issue, so moving over. However, I think we cannot change the order of entity-creation so this is how it is. Maybe, just re-saving the user account with Rules after profile creation does the trick for your specific case?
Comment #2
bryancasler commentedsubscribe
Comment #3
ngstigator commentedsubscribe
Comment #4
dippers commentedThis is a general problem for all profile2 tokens during user registration so, for instance, you cannot use profile2 tokens in registration emails.
Comment #5
dippers commentedThis is a fix to recover the profile2 token values from the form data rather than from the profile2 data in the database (that hasn't been saved yet).
It doesn't handle languages or multiple entry fields but that would be easy to add.
Comment #6
ngstigator commentedsubscribe
Comment #7
m4oliveisubscribe
Comment #8
scor commentedhaven't tried @Dippers's solution which looks promising. What I ended up doing to forcing the realname in the database on user_insert and hence duplicating the token by hard-coding it :(
<?php
function MODULE_user_insert(&$edit, &$account, $category = NULL) {
$realname = $edit['profile_customtype']['field_firstname'][LANGUAGE_NONE][0]['value'] .
' ' . $edit['profile_customtype']['field_lastname'][LANGUAGE_NONE][0]['value'];
db_merge('realname')
->key(array('uid' => $account->uid))
->fields(array(
'realname' => check_plain($realname),
'created' => REQUEST_TIME,
))
->execute();
}
Comment #9
pivica commentedFix in #5 works OK, thx.
Comment #10
clemens.tolboomI consider this a bug. The solutions from #5 suggests profile2 should add this code.
Attached is a patch following along #5
A discussion with a colleague lead to using the rule 'After saving a new profile' which sounds way more plausible to use.
But the remark of @Dippers in #4 still holds thus making this patch valid too.
So please review whether this patch solve the Registration Mail problem
Comment #12
clemens.tolboomNew patch tests for valid values for the $parts array.
When creating rules from this do not follow the hint done on the Action page
as that test will fail. Tokens module however will try to eval the used tokens.
Comment #13
wOOge commentedI think my issue fits in here:
Rule:
"After saving a new user account"
"Create node of type ZZZ" -> "Set title of ZZZ to [profile:firstname] + [profile:lastname]"
The results of this is 2 nodes:
- One, with the title set to the actual replacement tokens - in this case the title of the new node would literally come out as: "[profile:firstname] [profile:lastname]"
- A second node, with the correct title built from the values of the profile2 tokens, so "Bob Smith" — as expected.
Seems as though the rule to create the node is being executed before Profile2 has a chance to save the data (creating node #1) — then once saved, the rule is executed again, creating the initially intended effect (creating node #2).
Help?
Comment #14
nrambeck commentedI took the advice from the first comment and created a Rule that re-saves the user after it is first created. That solved my problem.
Comment #15
funkym commentedSuccess with patch from #12
Comment #16
wOOge commentedConfirmed — Success with patch from #12
Comment #17
zeit_geist commentedI have a question to the patch from #12.
I'm not well familiar with git and versioning. How to apply this patch? Should I download git, and diff or somethink on this file?
When patch is applied, am i setting in rules value as before with: account:profile-name:profile-field?
Thanks for help :)
Comment #18
scor commented@zeit_geist: no need to have git installed: https://drupal.org/patch/apply. Make sure to use the profile tokens.
Comment #19
scor commentedMy use case is actually to use the profile value (first and last name) when sending the registration emails, which are sent right away during user creation, so the above workaround does not work. I haven't tried the patch yet.
Comment #20
clemens.tolboomFrom #15 and #16 we conclude it works right?
Let's move this forward.
Comment #21
roball commentedCan also confirm that patch #12 fixes the problem. Now, [user:profile-x:y] tokens can be used in the automatically sent welcome e-mails.
Please commit, thanks!
Comment #22
skylord commentedOne more confirmation - #12 makes welcome e-mail work OK. Please, commit.
Comment #23
joachim commentedPatch fixes a similar problem with use of Profile2 tokens in Mailchimp merge fields.
I'm a bit concerned about whether this really is the best way of fixing the problem though.
(Also, patch comments need work for Drupal coding standards.)
Comment #24
mrded commentedChanged to Drupal coding standards.
Comment #25
stella commentedPatch in #24 works for me too.
Comment #26
OliverColeman commentedThe patch in 24 only works if the column name for the field is 'value', and throws an error like "Notice: Undefined index: value in profile2_tokens_alter() ..." if it's not. I'm going to look into a more generalised solution starting now..
Update: it will also only work when the language is LANGUAGE_NONE, but perhaps this is always the case with the registration form? Doesn't seem likely..
Comment #27
joachim commentedWell spotted! I confirm this:
This bit expects a structure like $entity->FIELDNAME[SOMETHING???][und][0][value]
which makes it pretty specific.
We should at the very least use field_get_items(), but that won't solve the 'value' problem IIRC.
So as we're already dependent on Entity API, we should in fact use an entity metadata wrapper.
Though I can't help but wonder if there's an altogether more elegant way to do this :/
Comment #28
clemens.tolboom@mrded what is the interdiff between #12 and #24? (what comment I did wrong)
I'm sorry for the obscure code as that's what it is. iirc I wrote it for a dutch only website 1 year ago :-/
Comment #29
mrded commented@clemens.tolboom, difference is just in coding standards. Functionality is still same.
Comment #30
OliverColeman commentedOkay, I've now spent many hours on this, trying many different approaches:
I now hate Drupal even more than I was already starting to...
Comment #31
spleshkaI think that I finaly found a proper solution.
The idea is that we disable e-mail notifications during execution of user_register_submit() callback, but send this notifications right after the saving of a user profile in profile2_form_submit_handler(). So we don't loose any e-mails or smth like that, but now all tokens from profile2 are available.
Patch that solves this issue attached.
Comment #33
spleshka31: profile2-tokens-1097684-31.patch queued for re-testing.
Comment #35
spleshkaOkey, applied new patch with a small fix.
Comment #36
clemens.tolboom@Spleshka interesting approach.
I'm not sure whether your approach could have side effects on other modules behavior.
The other approach which still has issues just fixed tokens which is a solution for other modules too.
my 2cents
Comment #37
spleshka@clemens.tolboom,
I tried to keep the contrib's behavior here. For example, if some contrib disables the notification - the profile2 also will not send this notification. So this behavior is pretty save.
Comment #38
joachim commentedIt's definitely a very clever approach.
Are there other use cases besides emailing new users, which this wouldn't cover?
Comment #39
spleshkaIn theory, we may miss a case when some contrib needs profile2 tokens while user registration process, and its callback executes after
user_register_submit()but beforeprofile2_form_submit_handler(). To avoid this case we may ensure thatprofile2_form_submit_handler()executes right afteruser_register_submit(), and all other callbacks - later.Comment #40
PascalAnimateur commentedHmm.. the patch doesn't fix the issue in my case. Do I have to clear the cache or something ?
Comment #41
spleshka@PascalAnimateur,
No, you don't need to clear a cache. Please tell about your case.
Comment #42
PascalAnimateur commentedThe problem I have is with realname_registration which allows to generate username from profile2 tokens (i.e. first and last names). When sending the notification email, [user:name] doesn't pick up the realname tokens at all.
Comment #43
spleshkaOkey.. so I see only one solution here: We need to have three form submit callbacks:
* First - right before
user_register_submit()- disables e-mail notification.* Second - right after
user_register_submit()- creates user profile.* Third - the last of all callbacks - sends e-mail notification.
Comment #44
spleshka@PascalAnimateur,
I've installed both profile2 and realname_registration modules. Name generation works successfull for me in both cases: with and without patch. The real name generates during
hook_user_presave(), which is invoked inuser_register_submit(). So your issue is not related with this issue.The conclution: my patch is still waiting for a maintainers' reviews.
Comment #45
RunePhilosof commentedMaintainers often filter on "reviewed and tested by the community".
So to make maintainers see this faster, some of the followers of this issue needs to review and test it.
Comment #46
spleshkaAgree. So guys, please make a patch review to finally solve this issue ;)
Comment #47
podarok#39 looks good for me
Good to see unit tests here
Comment #48
spleshkaAny progress here?
Comment #49
skdrupal88#39 Works good for me.
Comment #50
spleshkaCool, thanks! Now we need to get this commited ;)
Comment #51
loze commentedUsing Logintoboggan the patch in #39 causes registrations to send 2 emails.
I believe this is due to LT unsetting the core user_register_submit handler and uses its own.
Comment #52
spleshkaThanks @Loze! So we need to figure out why it doesn't work with LT.
Comment #53
mibfire commented+1 for LT duplicate email. Any success on this?
Comment #54
spleshkaOkey, I got this problem. LoginToBoggan replaces default user_register_submit() function with its own one. So we need to catch this case. Also we have to ensure that form alter hook from Profile2 executes the latest to prevent possible issues when Profile2 executes before LT (or other contribs that may do some work aroung user_register_submit() function).
I've made some more coding and tests around this issue with LT and found a proper solution. See patch below.
Comment #55
mibfire commentedSpleshka it looks good so far, VERY GOOD JOB!!!
Comment #56
spleshkaThank you. Please, test and set proper issue status if it is works for you.
Comment #57
mibfire commentedComment #58
loze commentedThis appears to be working for me as well using LT. Great job!
Comment #59
loze commentedI spoke too soon. While its working on the registration end. I am getting the following error when I visit the edit profile page.
These errors are gone when I switch back to the unpatched version.
Comment #60
spleshkaThank you for tests, @loze! We are so close to the finish :)
I could not reproduce your bug, but I believe that this situation is possible if #submit handler is missing in the form for some reasons. So I've made small fix to ensure that this case never happens.
Comment #61
loze commentedThanks Spleshka, I think that's it!
Seems to be working with my initial test. Thanks for the quick response on this!
Comment #62
spleshkaWelcome, @loze. Please, do not forget to set a proper issue status if you tested the patch :)
Comment #63
loze commentedComment #64
charlietoleary commentedAlso confirming patch #60 works for me.
Comment #65
spleshkaAny progress here? Do we need more testers or patch is waiting maintainers to be commited?
Comment #66
RunePhilosof commented@Spleshka The last commit to the projects repository was 11 months ago. So the problem is probably that this module needs new maintainers.
Comment #67
spleshkaSince I am a new co-maintainer for Profile2, I was able to commit/push this patch to 7.x-1.x.
Thank you all indeed for your manual tests and reasonable advices!
Comment #68
roball commentedExcellent, Spleshka, thank you! Good to know that this important module got live again after being unmaintained for 14 months.
Comment #70
roball commentedJust wanted to confirm that this issue has indeed been fixed using the latest dev snapshot - 7.x-1.3+4-dev (2014-Mar-02).
Comment #71
spleshkaThank you, @roball, for confirmation!
Comment #72
jonloh commentedUnfortunately it's still not working as expected from my end. What I'm basically doing is programatically creating the user:
The rules fired as expected with the event "After saving new profile", but isn't able to validate the condition as expected. Here's the report from the Rules log:
Comment #73
IWasBornToWin commentedConfirming latest dev works great!
Comment #74
grandmaster commentedJust installed the latest dev version, and it's still not working as many have stated it is. Any tips or magic tricks would be greatly appreciated.
Comment #75
ireneb commentedLast version not working here either. And when installed the automatic e-mail with a welcome message is no longer send, when admin creates an account.
Comment #76
roball commentedUnfortunately, I have to confirm that on my new sites, having the latest Profile 2 7.x-1.x-dev installed, tokens such as [user:profile-profile:field-first-name] will no longer be resolved into the correct field value (instead, they are resulting in an empty string) in welcome e-mails.
Comment #77
daniel-san commentedI have been trying to resolve this issue on a site for days now as well. Using the latest dev version of Profile2 does not help for me.
Drupal 7.31
Profile 2 version 7.x-1.3+5-dev
Realname 7.x-1.2
Add a new user with generic username - testuser1
Edit Profile
add first name - Test
Add last name - User1
Realname not updated
Resave Profile page and the Realname is updated.
Comment #78
shi99 commentedI also still have the issue.
By saving the user's profile twice the Realname is updated. Same as the steps outlined in #77.
Comment #79
shi99 commentedNot a solution to the issue, but putting this here in case it helps anybody.
I implemented a rule outlined by Dave Reid here https://www.drupal.org/node/1414140#comment-6133400 which works as a workaround by updating the realname when the user's profile is updated.
{ "rules_rule_update_realname" : {
"LABEL" : "Update Real name",
"PLUGIN" : "reaction rule",
"REQUIRES" : [ "realname", "profile2" ],
"ON" : [ "profile2_update" ],
"DO" : [ { "user_realname_update" : { "account" : [ "profile2:user" ] } } ]
}
}
Comment #80
kyoder commentedThis is still not working with latest dev version.
Upon initial user creation, realname is not populated. Requires re-saving the profile.
Comment #81
dmsmidtLatest dev version is working for me neither.
Comment #82
spleshkaOh, that's bad. It seems that I have to digg into this issue again :)
Comment #83
dmsmidtThat would be really appreciated, I temporarily fixed it with a rule like the one mentioned in #79.
I also had a caching problem with entitycache, but fixed it with a custom cache clear action in rules as well.
Comment #84
lingaraj_m commented#5 Works good for me.
Comment #85
shashwat purav commented#5 worked for me too. :)
Comment #86
kiwad commentedAnother Workaround :
Create an hidden field in your user account
Populate that field in a custom module
This code is partly working, would need another condition for cases where $account is not new where it uses profile2_by_uid_load or it could be done wit a rule... it is clearly not a clean workaround, but at least headaches are fading away...
Comment #87
takectrl commented#5 worked for me with token pattern: [user:profile-name:field_name]
Comment #88
Road Kill commentedSorry if this sounds like a stupid question but how do you re-save a profile in rules.
Comment #89
giorgoskCan't get token [user:profile-name:field_name]
to appear in Rules action : Send mail to all users of a role
tried all the patches but nothing works
However tokens of the form [user:profile-name:field_name]
in emails from admin/config/people/accounts work as expected
any directions ? what is missing ?
EDIT
in rules action token has to be of the type
[account:profile-name:field_name]
So everything works
Comment #90
Jacqs commentedI have the problem of not mails not being sent. I dig a little into my configuration and it seems that profile2_attach_form its called twice don't know why yet. I think this is related to use also Profile2_regpath module.
The problem then is that the handlers are called twice and the $changed_ops static var is populated twice, the first time correctly but the second time is incorrectly populated with the previous unset($conf), so $notify is always false and no _user_mail_notify is called.
Anyway I just added a small check to avoid adding twice the hooks and avoid this silent error, validation is still added twice.
Cheers,
Jacq
Comment #91
elfakhar commentedThanks @Dippers #5 works for me :)
Comment #92
phandolin commentedJust to let you all know what I did to get this working. If you use Rules and create a reaction rule like this:
Event: After saving a new profile
Elements: Data Comparison (Data to compare, [profile2:type], Data value: (Profile 2 type here)
Elements: Send mail
Then all of the profile2 are available as tokens in the email.
At first I tried to set this up using Event: After saving a new user account, but the fields were showing up blank and unavailable, as described above. Since new users on my site are also entering profile2 information at the same time, this got it working as the way I wanted.
Comment #93
ClemensM commentedHi all!
This is a long discussion thread with many contributions. I tried to follow all aspects and also tried some (last) patches, but I couldn't get fix my problem, which is the following:
Diretly after an user has been registered, his/her profile2 field's content is not shown and can't be manipulated(=>no effect) inside a View (nothing to do with Rule or mail sending, though I tried to fix my problem with Rule: after saving a new user account|new profile approaches).
As soon as I save the profile2 by selecting it (/user/userID/edit/profile2name) and press the button "Save" manually: the fields becoming available and view based changeable...
But how can I simulate/automate this saving process??? :(
It leads to despair, so I appreciate your help, seriously!
Edit:
To be precise: This is only the case for profile2 variables not haven been asked for during the registration process.
After registration, those variables should show the default-value (like they do at /user/userID), but in the View they just don't.
Comment #94
josephcheek#60 did not work for me as it disabled all emails. #12 worked for me.
Comment #95
raxxraxx commentedSo... I have another use case that isn't working properly on registration of a new user. I have a site that uses 2 Profile2 types for each user. The first one is an 'Application' that the user fills out when they are registrating for the site. This 'Application' is hidden from the user after they fill out the information and apply to the site. Then I have the 'Member Profile' that contains some of the fields of the 'Application' profile. This can be edited freely by the user to show their information to other users of the site.
I would like to copy some fields from the 'Application' Profile2 to the 'Member Profile' Profile2 on user registration; however, at user creation time the 'Member Profile' doesn't exist at all and I can't figure out how to create it.
Using a rule on Create New Profile (with the type 'Application') works great, allowing me to access the data in the 'Application'; however, I can't figure out how to programmatically (in Rules) create the 'Member Profile' for the user. I can create _a_ 'Member Profile' but I can't use 'Set a data value' to set the User's 'Member Profile' because it says 'The selected property doesn't support writing.' Also, I can't set any of the individual values of the User's 'Member Profile' because it says 'Parent data structure has not been set yet'
In fact, if I add a Rules Trigger to Create New Profile with the type of 'Member Profile' the trigger doesn't get called until I go in and manually create the 'Member Profile' by hand and then save it.
Any ideas?
Thanks,
Raxxraxx
Comment #96
bmango commented#5 worked for me - thank you to Dippers :)
Comment #97
kevinquillen commentedI don't get any options in Drupal 8 for this. Is there a way to integrate Profile2 for Realname or should I just get rid of Profile2?
Comment #98
rickj commentedThe fix in #60 has now been released into 7.x-1.4, as it was already in the dev build 4 years ago. However, it seems it wasn't a complete fix, and has caused a new issue for a user who's gone straight from 1.3 to 1.4. See issue #2954186.
The problem is the profile2_module_implements_alter() hook, for reasons I can't work out but discovered by a bit of guesswork! I've posted a patch in the other issue to remove this hook. If anyone here is using 1.4, could you feed back whether applying the patch in issue #2954186 affects behaviour, either positively or negatively?
Thanks
Comment #99
rickj commentedThe changes in 7.x-1.4 seem to have introduced several other issues, with quite a lot of module conflicts going on. AFAICT, much of it results from the use of
hook_module_implements_alter(), removing it clears most of these other issues.I'm therefore attaching a patch against latest dev (7.x.1.4-dev+3), which removes the hook function, but leaves everything else in #60, and also includes the fix in #90 which appears sensible.
Please try this patch with latest dev, and let us know the results.
Related issues: #2449913, #2954186, #2956961
Thanks
Comment #100
Max_Headroom commentedI came here from #2954186
So I only tested what is related to there.
Patch dev, works fine.
Comment #101
rickj commentedFix also confirmed in #2962543. Marking RTBC.
Comment #103
thirdender commentedCan confirm patch #99 fixes an issue we had with Profile2 7.x-1.4. Field Group was not able to render fields inside the Profile2 fields on a user registration page. Downgrading the Profile2 module to 1.3 or 7.x-1.4+6-dev (which includes patch #99) fixed the issue.
Comment #104
rickj commentedComment #106
rickj commentedComment #107
noopal commentedHi, Has this been fixed.
I am having the same issue with Profile2 7.x-1.7.
Thanks
Comment #108
rickj commentedHi noopal
I came into this issue quite late, as I was in the process of tidying up the module. I don't have a configuration that showed up the problem, I followed reports from affected users, and it also interacted with other issues so was a bit mind-bending!
Reading the original report again, it sounds like the core of the problem is that, on registration, the profile entity is created after the user entity. However, I don't see how that can be changed, as the profile needs the user uid - that is generated by the database and doesn't exist until the user has been created. So if the profile were written first it would be incomplete.
Is that a fair description, or is there another problem here?