1

Have a custom module with a block. Added form and form theme functions. Using hook_theme() to register form theme function.

Trying to put form content in the _block_view() function into block content. But finaly it doesn't use form theme function - theme_mymodule_custom_form()

The form is displayed on a block, but theme function is not in use, Have no idea how to make this work. Found a good sample here http://www.jaypan.com/comment/1758, but it didn't help me.

 function mymodule_block_view($delta = '') {
  switch($delta){
    case 'mymodule':
      if(user_access('access content')){
        $block['subject'] = 'Block header'; 
        $block['content'] = drupal_get_form('mymodule_custom_form');
      }
    return $block;
  }
 }

function mymodule_theme()
{
    return array
    (
        'mymodule_custom_form' => array
        (
            'render element' => 'form'
        ),
    );
}


/**
*  Create a form
*/
function mymodule_custom_form($form, $form_state) {
  // Form elements
  $form = array();

  // Text field
  $form['Input'] = array(
    '#title' => t('Input field'),
    '#type' => 'textfield',
    '#required' => TRUE,
  );

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

    // Get the path to the module
    $path = drupal_get_path('module', 'mymodule');
    $form['#attached'] = array
    (
        'css' => array
        (
            'type' => 'file',
            'data' => $path . '/css/style.css',
        ),
        'js' => array
        (
            'type' => 'file',
            'data' => $path . '/js/sample.js',
        )
    );

  return $form;
}


function theme_mymodule_custom_form($variables )
{
    // Isolate the form definition form the $variables array
    $form = $variables['form'];
    $output = '<h3>Themed form</h3>';
    $output .= drupal_render($form['Input']);
    $output .= drupal_render_children($form);
    return $output;
}
  • Have you tried theme_form($output)? – Alex Gill Aug 29 '12 at 7:50
  • 1
    Also you could try setting the #theme property on your form build. $form['#theme'] = 'something'; – Alex Gill Aug 29 '12 at 7:53
  • It's strange, but I tried same code on different server with Drupal 7.15 installed via hosting console - it's working. Not sure what is the difference as I installed on localhost same version downloaded 7.15... – James Aug 29 '12 at 18:32
  • Somehow this even start working on my localhost. So, I think, this was cache issue. SO if you have sameproblem try to look at the cache – James Aug 29 '12 at 22:45
0

Add $form['#theme'] = "my_theme_form"; in your form.

I always use this base for theming forms and work without problem.

function contact_form() {

    $form['name'] = array(
        '#title' => t('Name'),
        '#type' => 'textfield',
        '#required' => TRUE,
    );

    $form['mail'] = array(
        '#title' => t('e-mail'),
        '#type' => 'textfield',
        '#required' => TRUE,
    );

    $form['comment'] = array(
        '#title' => t('Comment'),
        '#type' => 'textarea',
        '#required' => TRUE,
   );

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

    $form['#theme'] = "my_theme_form";

    return $form;
}

function MYMODULE_theme() {
  return array(
    'my_theme_form' => array(
      'render element' => 'form'
    ),
  );
}

function theme_my_theme_form(&$vars) {

    $form = $vars['form'];
    $output = "";

    //Render separated fiels
    $output .= drupal_render($form["Input"]);
    $output .= drupal_render($form["submit"]);

    //And the rest of your form
    foreach (element_children($form) as $key) {
        $output .= drupal_render($form[$key]);
    }

return $output;
}

That's all. just call the form in a block for example:

$form = drupal_get_form("contact_form");
0

Somehow, this started working on my localhost. I think this was a cache issue. If you have the same problem, try clearing the cache.

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.