Search code examples
drupaldrupal-7drupal-theming

Drupal 7 theming : hook_theme or drupal_get_form


I'm wondering which is the best way to display a form on a page (for example : a register form for vip users ... and not in a block but as the main content).

The user.module way in user_menu (*hook_menu*) :

  $items['vip/register'] = array(
    'title' => 'Create new vip account',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('vip_register_form'),
    'access callback' => 'user_register_access',
    'type' => MENU_LOCAL_TASK,
  );

Or by creating a theme via use_theme (*hook_theme*) (fictive) :

$items['vip/register'] = array(
    'title' => 'Create new vip account',
    'page callback' => 'theme',
    'page arguments' => array('vip_register'),
    'access callback' => 'user_register_access',
    'type' => MENU_LOCAL_TASK,
);

function user_theme() {
  return array(
    'vip_register' => array(
    )
  );
}

function theme_vip_register(){
    return drupal_get_form('vip_register_form');
}

I'm wondering about this for theming purpose, because a designer will do the graphic integration afterwards.

Thank you for advices.


Solution

  • This is not an actual answer but I'm not quite sure what is your question at first place. Drupal customs #1: Never hack core!

    As its name, theme functions are just to theme something. So you need the form built first.

    $items['vip/register'] = array(
        'title' => 'Create new vip account',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('vip_register_form'),
        'access callback' => 'user_register_access',
        'type' => MENU_LOCAL_TASK,
      );
    

    When a user accesses example.com/vip/register page, drupal_get_form function will be called with argument vip_register_form.

    So now you need to define a function to return this (vip user registration) form.

    function vip_register_form($form, &$form_state){
      ..your FAPI stuff here.
      return $form;
    }
    

    Now user who open the vip register page will see this form instead of the regular form. even password and username fields will not be available unless you add them. If you want to alter the existing form, just copy the menu hook to a new path:

    $user_menu_routers = user_menu();
    $items['vip/register'] = $user_menu_routers['user/register'];
    

    Now you can alter your the form at vip/register page (which is same as normal user register page) using a form_alter hook. You can theme the form manually without affecting existing one as well.