2

On a site I would like a "quick entry" form for a custom content type on the front page, while also keeping the "normal" non-customized "node/add/mycontenttype" form. I'm able to customize the "normal" form by using the theme_hook function returning the proper array which points to my function that renders the form ('template' => 'some-node-form').

But ideally I would like to keep both versions of the form, rendering my customized form on the frontpage in a block, and with the normal form at the normal place "node/add/mycontenttype". I figured I could try to detect at runtime in the 'some-node-form' function if for instance it was invoked through a special path "node/add/mycontenttype/frontpage", by passing a variable or similar. I haven't quite managed to do this yet, but at least in theory it should be doable.

But do any experienced Drupal developers have better ways of accomplishing what I'm doing, things like having multiple paths pointing to different template rendering functions or similar?

0

My first thought (in Drupal 6) would be to use hook_form_alter to set a different #theme value in the form depending on the path.

eg

mymodule_form_alter(&$form, &$form_state, $form_id) {
 if ($form_id = 'my_form_id') {
  if ($_GET['q'] == 'somepath') {
   $form['#theme'] = 'function_one';
  } elseif ($_GET['q'] == 'someotherpath') {
    $form['#theme'] = 'function_two';
  }    
 }
}
2
  • I would rather use mymodule_form_my_form_id_alter(), and avoid to verify the form ID is the correct one. – apaderno Mar 29 '11 at 22:09
  • Definitely a better way to go. – cam8001 Mar 30 '11 at 9:17
0

cam8001 clued me in regarding the '#theme' setting. In my template.php I have:

<?php

function groc_theme($existing, $type, $theme, $path) {
  return array(
    'purchase_node_form_fp' => array(
      'render element' => 'form',
      'template' => 'groc-purchase-node-form'
    )
   );
}

function groc_form_alter(&$form, &$form_state, $form_id) {
  if (arg(3) == 'fp') {
    $form['#theme'] = 'purchase_node_form_fp';
  }
 }
?>

and in my groc-purchase-node-form.tpl.php I then render the special form. In the groc_form_alter above I have hardcoded a path element instead of using a request parameter, but it's similar to the accepted solution. The way the code above works is that if I want the "special" form, I just add on a "/fp" to the normal add node link.

0

What I ended up doing in my implementation was using entityform (D7) and using rules to map and create the appropriate node when the entity form saves.

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.