1

I know variations of this question have been asked but I haven't found any with this specific set of characteristics. Also, the only programming language I know is python. I don't know PHP at all.

I want to be able to pass the value from a url parameter into a custom hidden field in the registration form.

I've tried using theme functions, template files, and hook_form_alter() but I haven't been able to get any of these to work.

So far, I've been able to add the custom field but I can't seem to hide it, let alone be able to pass the value to it. I am able to add html to the form via the template file but that's about it.

I based my attempts on this site: themery

This is the code I tried to see if I could alter the label for email.

function acq_dmc1_form_user_register_form_alter(&$form, &$form_state) {


$form['mail']['#title'] = t('E-mailzzzzzz');
$form['field_recipientid']['#title'] = t('QualID'); //this is the custom field


}

...but it didn't work.

this is the form id:

<input type="hidden" name="form_id" value="user_register_form" />

this is the HTML that's rendered for the custom field:

<input class="text-full ckeditor-mod form-text" type="text" id="edit-field-recipientid-und-0-value" 
name="field_recipientid[und][0][value]" value="[url:path]" size="90" maxlength="25">

EDIT: June 17, 2013 Someone gave me this code as a solution but I don't understand if it will work or how it will work:

function passval_user_presave(&$edit, $account, $category) {
    $fields = array(
    'RecipientID' => array('fieldname'=>'field_recipientid', "type"=>'complex'),
        'mail' => array('fieldname'=>'mail', "type"=>'simple'),
    );
    $urlargs = drupal_get_query_parameters();
    foreach($fields as $key=>$field) {
        if(isset($urlargs[$key])) {
            $value = ($urlargs[$key]);
            $value = intval($value);
            if($field['type'] == 'complex') {
            $edit[$field['fieldname']] = array('und'=>array(0=>array('value'=>$value, 'format'=>'', 'safe_value'=>$value)));
        }
        else {
            $edit[$field['fieldname']] = $value;
            }               
        }
    }
}
  • What do you mean by URL paramater "I want to be able to pass the value from a url parameter into a custom hidden field"? Would a URL querystring work? – longboardnode May 2 '13 at 6:33
  • I know how to do it with PHP. Is that using a token? – 655321 May 2 '13 at 18:19
  • Can you give example of your URL parameter? – longboardnode May 4 '13 at 20:52
  • mydrupalsite.com/?RecipientID=MLRP_8UByesUEfeeIA – 655321 May 6 '13 at 17:31
  • try to add this PHP code before the form_alter function: if (!empty($_GET['RecipientID'])) { $RecipientID = check_plain($_GET['RecipientID']); } Then you should be able to use $RecipientID value to assign it to whatever field you want in your form_alter. Hope this helps. – longboardnode May 8 '13 at 2:46
2

You can create hidden form elements in drupal simply by setting the type to hidden. So in your case, it could be achieved like so:

$form['field_recipientid']['#type'] = 'hidden';

You can access URL arguments in drupal via the arg() function. So if you want to pass something to the user register page and set it in your field_recipientid, you could do it by sending the user to the registration page via "http://example.com/user/register/MLRP_8UByesUEfeeIA".

Then from your same form alter function, just call arg(2) to get the third item in the path and set the value like so:

if (isset(arg(2))) {
  $form['field_recipientid']['#value'] = arg(2);
}

Note for anyone else thinking of doing this on other form elements that aren't hidden (e.g. text fields), use #default_value. If you set #value on text fields, drupal forcable only accepts that value, and disregards any edits to the text field value, the user might have made.

For more information on drupal's form api, see the forms api reference page on the drupal api site.

  • The url I am sending has to be in this format because of the restrictions that the other system is imposing on it: user/register/?RecipientID=MLRP_8UByesUEfeeIA&mail=drwho@bbc.co.uk Will the new code I added work? – 655321 Jun 17 '13 at 21:11
  • Great. So use drupal_get_query_parameters instead of args. Google the above function. – rreiss Aug 20 '13 at 5:59
0

show us how you try to do this on hook_form_alter().

You need to change '#default_value' in hook_form_alter() for your field.

  • I added some code I tried. – 655321 May 1 '13 at 20:23
0

I think this is the module you want Node Reference URL. Create a field in your content type called RecipientID and set it to use the "Node Reference from URL" widget. You can then pass your variable via URL "node/add/MLRP_8UByesUEfeeIA" and the module populates your RecipientID field automatically.

  • Thanks however I have to have it formatted like this: user/register/?RecipientID=MLRP_8UByesUEfeeIA&mail=drwho@bbc.co.uk – 655321 Jun 17 '13 at 23:08

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.