1

I am working on entity forms in drupal 7, I want anonymous users to be able to post content and then create an account, the problem is that by the time they create an account, any previous submission is already attached to "anonymous user".

I want to change the code so that when anonymous user posts, their content can be attached to the latest created users

Below is my code so far but it is not working.

if (empty($user->uid))  {
  $result = db_query('SELECT u.uid FROM {user} u ORDER BY u.uid DESC LIMIT :d',array(":d"=>1));
  while ($record = db_fetch_object($result)) {
    $uid = $record->uid; // last created user id .
  }   
  $entityform->uid = $uid;       
} 
1

You should rather use the following code, but there is a problem I will describe.

if (empty($user->uid))  {
  $entityform->uid = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());
}

The problem with that code is that it will use the user ID of the next user account that is being created, which could not be the user who created the entity. This is true when:

  • The anonymous user creates an entity instance
  • After the code I shown is executed, a new user creates an account
  • The anonymous user who created the entity instance creates an account

I would rather do the following:

  • When an anonymous user creates an entity instance, asks for an email address
  • Save the email address as part of the entity

When a user account is created, and the user logs in the very first time:

  • Search for any entity instance where the email address is equal to the email address used for the user account
  • Change the user ID with the user ID associated to that email address

Instead of the email address, you could ask anything else, as long as it is clear to the user that the entered value is requested when s/he creates an account to assign to her/his account any entity s/he creates. As the email address is required to create a user account, I would ask the email address to the anonymous users who creates entities.

0

In your entity property you can add option of author. Now if user is creating the entity then users id will be stored in author. So you dont need to do it with code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.