3

I built a custom login module for my Drupal7.26 install.

Extending it, I've added a *.tpl.php locally

ls sites/all/modules/custom/jl_login/templates/
    user-login-member-form.tpl.php

To get the tpl.php into theme registry, in "my_login_mod.module" I added

function jl_login_theme() {
    return array(
        'user_login_member_form' => array(
            'render element' => 'form',
            'template' => 'templates/user-login-member-form',
        )
    );
}

After module disable/uninstall/enable, and clear all caches, I load my site/page.

Checking output with

<?php print '<pre>'; var_dump(theme_get_registry()); print '</pre>'; ?>

I see the form in the registry

 ["user_login_member_form"]=>
  array(6) {
    ["render element"]=>
    string(4) "form"
    ["template"]=>
    string(76) "sites/all/modules/custom/jl_login/templates/user-login-member-form"
    ["type"]=>
    string(6) "module"
    ["theme path"]=>
    string(43) "sites/all/modules/custom/jl_login"
    ["preprocess functions"]=>
    array(1) {
      [0]=>
      string(19) "template_preprocess"
    }
    ["process functions"]=>
    array(2) {
      [0]=>
      string(16) "template_process"
      [1]=>
      string(14) "ctools_process"
    }
  }

Later in my .module, I use the form:

function _jl_login_login_block_view($delta='') {
    $this_user_login_form    = drupal_get_form('user_login_member_form');
    ...

But on load I see in drupal logs,

Feb 24 16:32:16 dev d7: http://dev.loc|1393288336|php|127.0.0.1|http://dev.loc/||0||Notice: Undefined index: user_login_member_form in drupal_retrieve_form() (line 771 of /home/jl/d7/includes/form.inc).
Feb 24 16:32:16 dev d7: http://dev.loc|1393288336|php|127.0.0.1|http://dev.loc/||0||Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'user_login_member_form' not found or invalid function name in drupal_retrieve_form() (line 806 of /home/jl/d7/includes/form.inc).

What do I need to change to be able to use/reference the local form?

  • is it a cut and paste typo that you seem to have user-login-alt-form.tpl.php yet referencing user-login-member-form, eg -alt- vs -member-? – Jimajamma Feb 25 '14 at 1:47
2

drupal_get_form() expects the name of a function that builds a form array, e.g.

function MYMODULE_some_form($form, &$form_state) {
  $form['#theme'] = 'user_login_member_form';

  $form['foo'] = array(
    '#type' => 'textfield',
    '#title' => t('Bar'),
    '#default_value' => t('Baz'),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );

  return $form;
}

function MYMODULE_some_form_submit($form, &$form_state) {
  // Process $form_state['values']
}

// In a hook...
$form = drupal_get_form('MYMODULE_some_form');

You don't need to add the form function to a registry as such, just giving it the same name as the string you pass to drupal_get_form() is enough.

hook_theme() is for registering theme functions, which is why you're currently getting an error.

The Form generation docs have a bit more information, and the FAPI reference is invaluable if you're new to the form API. For theming the form, there are some excellent answers to this question which should help.

  • Template files aren't synonymous with forms - you can use a template file to theme a form, but you need to use the form API to build the structure – Clive Feb 25 '14 at 1:10
  • I've added a bit to the code, is that what you're looking for? It's hard to understand what you're asking, as you say you don't want a form but you're using the drupal_get_form() function, which can only use a form. Could you possibly mean theme('user_login_member_form');? If so, you need to change 'render element' => 'form' to 'variables' => array() in your theme definition, and clear the caches – Clive Feb 25 '14 at 1:16

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.