How to save a vote of an anonymous user only after registering using modal window? Here is my usecase:


Only authenticated user can vote.
If an anonymous user tries voting, after the voting is done, the user is represented a modal window for login/register.
If the user registers or loges in, the vote is saved.


The vote calculation is done using cron or other module. I do not have tie up with any particular module (and fine with writing a basic one). I am currently trying my luck with Rules, Voting Rules, Fivestar and obviously, Voting API. For login/registration persona and fbauth is used, so no email verification is requierd.

A similar problem is discussed in A module that allows user to create content anonymously, then if they register, assigns the content to them, however my use case involved voting and email-free registration. Voting makes it more complicated while email-free registration (hopefully) makes it easier.

PS: I want to follow the same work flow for different nodes with plus1 (or similar) module, so a generic approach is requested.
You could store anonymous votes in a cookie, then on login/register, check to see if that cookie exists and cast the votes accordingly.


When an anon user tries to vote, use user_cookie_save() to save the Entity Type and Entity ID of the Entity that the user was trying to vote on in a cookie. Name it something like pending_votes. At the same time, you can trigger your modal login window. It's possible that a user won't login/register at that time and it's also possible that they will attempt to vote on other Entities before finally logging in. So you'll probably want to store the Entity Type and ID as a serialized array.
Then you can hook into a user logging in with hook_user_login() and check for the existence of the pending_votes cookie. If it's there, use it's contents along with votingapi_set_votes() to cast the pending votes. (delete the pending_votes cookie afterwards so they don't cast more votes each time they log in)