5

There are hundreds of users on my Drupal 7 website. I don't want to manually enter the birthday value for each user in their profile. Hence why I created a webform where the user puts the user's birthday.

This value should then after validation by the current user be stored permanently in the user's profile where I created an empty birthday field.

How do I pass the value?

0

2 Answers 2

3

Using the "Webform Rules" module, you should be able to have the Rules module react on an event like "Webform submitted". So create a custom rule using this Rules Event, and then add the required Rules Actions. Here is a blueprint of these Rules Actions:

  • Fetch Entity by id, of type user, using the uid of the user who submitted the form (= current user).
  • Set a data value, for the field of the user's profile where you want to save that birthday, where the value is the birthday entered in the form.
  • Possibly (not 100% sure) you may also want to add Save entity, to actually save that field in the profile.

Make it right before you make it better ...

But what about a user submitting the same form again, with a different value for birthday? If you don't want that to happen, you might want to add the Flag module like so:

  • Flag a user after submitting the form (using Rules), using some global flag.
  • Improve the 1st rule above, to add a Rules Condition to verify that the user has not been flagged yet.

PS: This answer applies to D7 ...

1
  • Thanks. This is a well round answer and it exactly suited my needs.
    – Vasmir
    Feb 10, 2017 at 15:32
2

You can alter the Webform and update the user fields. You need to use form_alter hook for that. Added some sample code you can modify as per your need.

function hook_form_alter(&$form,&$form_state,$form_id)
    {
        if($form_id == 'webform_id')
        {
         $form['#validate'][] = 'hook_form_validate'; //function name
         $form['#submit'][] = 'hook_form_submit'; //function name
        }
    }

function hook_form_validate($form,&$form_state){
        // validate you dob field
}

function hook_form_submit($form,&$form_state){
    global $user;
    $account = user_load($user->uid);
          $edit = array();
        if(!empty($form_state['input']['submitted']['first_name'])){
            $edit['field_dob'] = array(LANGUAGE_NONE => array(array('value' => $form_state['input']['submitted']['dob']))); 
       }
        user_save($account, $edit);
    }

You can also take a look on Webform Rules module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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