background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount

Linking to a form element by adding an anchor

Eric.London's picture

This code snippet shows you how you can modify a preexisting form and insert an anchor, which will allow you to link to a specific form element from your content. This could be useful if you'd like a user to update their profile details or fill out a certain section of a form. In this example, I add an anchor to the user profile form picture fieldset.

<?php
function MYMODULE_form_alter(&$form, $form_state, $form_id) {
 
// check for the user profile form
 
if ($form_id == 'user_profile_form') {
   
// add a new anchor form element
   
$form['picture']['anchor'] = array(
     
'#value' => '<a name="MYMODULE_picture"></a>',
    );
  }
}
?>

Then in my content, I added a link with an anchor.

<?php
$html
.= l('Upload profile picture', 'user/' . $GLOBALS['user']->uid . '/edit',
  array(
'fragment' => 'MYMODULE_picture')
);
?>

Good tip

Hey, cool I might use this! I almost understand even! I am so behind on changes and fixes to the site :( Someday we will hire you away and have making mad sites for us!

-Ryan W.