In drupal the form is rendered using the form rendering system, which doesn’t include template files.
To theme the profile 2 form we use “hook_theme()”.
Go through the steps:
1. In this step copy the code in template.php of the theme or any php file and include the file in template, php used with proper theme name and form id. Here we are using ZUK theme and ‘user-profile2-form’ form id

function zuk_theme($existing, $type, $theme, $path){
return array(
‘user-profile2-form ‘ => array(
‘render element’ => ‘form’,
‘template’ => ‘user-profile-form’,// new form template name
‘path’ => drupal_get_path(‘theme’, ‘zuk’) . ‘/templates’,
//add ‘/templates’ only if you store template files in an additional folder
),
);
}

This tells the Drupal theming system that there’s a template file waiting for it in zuk/templates.

2. Next we define variables to pass into that template in a “hook_preprocess_HOOK” function. You can find the structured array of the form in “$variables['form']” and assign the form elements to the variables we’d like to throw around our tpl.php file.

function
zuk_preprocess_user-profile2-form (&$variables) {
$variables['rendered'][‘first_name’] = drupal_render($variables['form']['first_name']);
$variables['rendered'][‘last_name’] = drupal_render($variables['form']['last_name']);
}

use var_dump(($variables['form']) to see all the element of the array (i.e form element)

3. Create a new template file named .tpl.php in ‘YOURTHEME/templates’. Same as name of template mentioned in zuk_theme() function.

{div id=”new-form_template”}
{?php echo $rendered[first_name]; ?}
{?php echo $rendered[last_name]; ?}
{/div}

In the above code “< >” are replaced by “{ }”
Now you can see the changes in edit tab of the profile pages.

Note: The hidden file like form_id and token must be there inside the form. Otherwise the form saves not working.

Latest posts by rupak

  • Share/Bookmark