I need to theme certain form fields on the user profile form page differently from other fields, for which I am trying to create a theme function that outputs html its own way, not using theme_form_element(). The reason for defining a new theme function alternative to theme_form_element() is because the html output is dramatically different.

This is my code:

function mymodule_theme() {
  return array(
    'profile_form_element' => array(
      'variables' => array(
        'element' => NULL,

function mymodule_form_user_profile_form_alter(&$form, &$form_state) {
    $form['field_user_first_name']['#theme'] = 'profile_form_element';

function theme_profile_form_element(&$variables) {
  $element = &$variables['element'];
  // my html processing would start here...

...but the theme function has NULL for $variables['element']. What am I doing wrong? I am attaching the structure of my form field (field_user_first_name), which was created through the UI at configuration > people > account settings > manage fields.

form field dpm'd

form field dpm'd part2

1 Answer 1


I think one of your problem is with this:

$element = &$variables['element'];

I don't think you can assign a reference to an array element (the &), so try changing it to:

$element = $variables['element'];

and I don't see a 'element' element, so unless I'm blind you're trying to access an element that isn't there, so it will always return NULL;

  • So what do I need to pass in my hook_theme()? I thought 'element' would just collect the form field itself...
    – itsututa
    May 4, 2015 at 19:14
  • Im far from an expert on the Form API, but it looks to me like you are assigning $form['field_user_first_name']['#theme'] to the return value of the function, but in your dpm $form['field_user_first_name']['#theme'] doesn't have any sub-elements, so when you try to assign your variable, you're assigning something that isn't there. To know what $variables contains in your callback, dpm it
    – Geoff
    May 4, 2015 at 19:17
  • $variables comes blank, it has 3 keys 'element, 'context', 'theme_hook_original'->profile_form_element. Nothing for 'element', still don't know what am I doing wrong...
    – itsututa
    May 5, 2015 at 7:20

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.