سؤال

I'm trying to register a theme function for a simple form in a custom module, but the theme function is not being called. I just get the basic form.

Here's my hook_theme():

function kss_membership_theme($existing, $type, $theme, $path){
    $items = array();
    $items['kss_membership_payment_form'] = array(
        'render element' => 'form',
    );
    return $items;
}

for this form:

/**
 * Returns the form for the second page of the payment process
 */
function kss_membership_payment_form($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('We currently accept Paypal payments'),
  );

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

  $form['#theme'] = array('theme_kss_membership_payment_form');
  return $form;
}

and here is the theme function:

function theme_kss_membership_payment_form($variables) {
    // Isolate the form definition form the $variables array
    $form = $variables['form'];

    $output = '<h2>' . t('Please enter your information below') . '</h2>';
            $output .= '<div id="personal_details">';

    $output .= drupal_render($form['description']);
    $output .= drupal_render($form['submit']);
    // Additional template output goes here....
    $output .= '</div>';

    $output .= drupal_render_children($form);
    return $output;
}
هل كانت مفيدة؟

المحلول

You are very near to solution, Only one problem is there.

theme calling is wrong

$form['#theme'] = array('theme_kss_membership_payment_form');

you are required to call

$form['#theme'] = array('kss_membership_payment_form');

After that You must required to clear the cache from the admin => configuration => Performance => Clear Cache button.

/**
 * Returns the form for the second page of the payment process
 */
function kss_membership_payment_form($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('We currently accept Paypal payments'),
  );

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

  $form['#theme'] = array('kss_membership_payment_form');
  return $form;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top