0

I'm having an issue with theming our node edit forms. We are converting a Drupal 6 theme to Drupal 7 and its causing a lot of problems.

In our template.php we have

function dupublic_theme($existing, $type, $theme, $path) {
    return array(
            'page_node_form' => array(
                    'arguments' => array('form' => NULL),
                    'template' => 'page-node-form',
                    'render_element' => 'form',
            ),
    );
}

This successfully points Drupal at 'page-node-form.tpl.php'

Our goal is to pull out some fields into labeled areas to make our editor's job easier. We had a more complex layout, but I've simplified it for the following example. In page-node-form.tpl.php I have the following:

<div class='title'>
    <?php print drupal_render($form['title']);?>
</div>
<div id='custom-form'>
    <?php print drupal_render_children($form); ?>
</div>

This, however, does not work. $form does not appear to exist, and dpm($form); displays as an empty array. We do not get any form fields at all - unless I remove everything from the tpl file, then I get some sort of default form display, which doesn't make sense. If the tpl file is empty no form should be displayed, but everything I am reading states that drupal_render($form['field']) and drupal_render_children($form) should work! They aren't!

This is what is rendered on page load:

<form class="node-form node-page-form" action="/node/5/edit" method="post" id="page-node-form" accept-charset="UTF-8" data-thmr="thmr_33">
    <div>
        <div class="title" data-thmr="thmr_32"></div>
        <div id="custom-form" data-thmr="thmr_32"></div>
    </div>
</form>

As you can see, no form fields!

4

In your template.php the hook_theme implementation should look like that:

function dupublic_theme($existing, $type, $theme, $path) {
  return array(
    'page_node_form' => array(
      'template' => 'page-node-form',
      'render element' => 'form',
    ),
  );
}

Notice that in the return array 'render element' key is without underscore, and there is no 'arguments' key in Drupal 7 version of hook_theme(). It is replaced by 'variables' key, but the documentation states that:

Each information array must contain either a 'variables' element or a 'render element' element, but not both. Use 'render element' if you are theming a single element or element tree composed of elements, such as a form array, a page array, or a single checkbox element.

Another point that I notice is, that you used in your page-node-form.tpl.php file statements like:

<?php print drupal_render($form['title']);?>

I prefer to print the elements like that, and recommend you to do so:

<?php print render($form['title']);?>

It's a new function in Drupal 7 and it calls drupal_render() but first checks if the passed element is an array and also calls show() function if the element is set hidden.

  • Thanks for the suggestions! Mysteriously when I reverted all my changes to where I started it began to work. I have no idea what the problem was! :( – oranges13 Jun 18 '13 at 21:01
  • Your answer helped a lot. Can you tell me how to display error messages in the page? print $messages did not work here. – Dushyant Joshi Aug 19 '14 at 9:10
  • @DushyantJoshi you should put print $messages into page.tpl.php file (for your administration theme, if your node add/edit forms are set to open in admin theme) – Елин Й. Aug 20 '14 at 8:54

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.