Hi!

Ok, I've been trying to figure this out a while know, so I thoguht I'd do post here.
What I'm trying to do is:

a) change the data (array) that's building the comment_form form
b) place THAT form on a specific place on a specific node type page

So, b) I've already figured out...

function phptemplate_preprocess_node(&$vars) {

  $vars['comments'] = $vars['comment_form'] = '';
  
  /* I will change this if-statement to match my specific node type */
  if (module_exists('comment') && isset($vars['node'])) {
    $vars['comments'] = comment_render($vars['node']);
    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
  }

 /** Now I can place the $comment_form variable in my template file wherever I want... **/

...but I can't seem to wrap my thought's about a).

SO THE QUESTION IS:
########################################################
How do I alter a form (the comment_form form) before I pass it to the template file ?
(meaning that the form I override doesn't affect the same form on any other pages than
what the template file targets)
########################################################

Help

Comments

nevets’s picture

You would want to write a module that implements hook_form_alter(). You can use form_id to determine it is comment form and will need to look at $form to see what you can use to check for node type.

DrupalNovice’s picture

Thanks for the answer. I'm aware of the hook_form _alter() function.

I'm wondering: is there any way to do this in the theme layer? I realize that Adding elements must be done in a dedicated module, but I'm just going to Remove some elements from the array.. and I'm just going to use this feature in the selected theme, so building a module for this task is a bit overkill..

I'm aware of the [themename]_[form_name] and the hook_theme functions..my question is: Are these the only ways to alter the $form array in the theme layer? And how do I use these functions together with my phptemplate_preprocess_node() function to achieve a form override only for the content type I specified?

Maybe I should nest some functions like this?

/** Preproces function which will target the content-type... **/
function phptemplate_preprocess_node(&$vars) {
  
  //Checks if comments module exists, if the page that's being loaded is a node and matches the content type specified
  if (module_exists('comment') && isset($vars['node']) && $vars['type'] == 'my-content-type') {
   //Implementation of the [themename]_[form_name](?) function
    function mytheme_comment_form($form) {
      //do the form override here
    }
    //Retrieve the freshly overrided  (?) comment_form
    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
  }
}

?

nevets’s picture

Nesting is not the answer, using [themename]_[form_name]will allow you to render only selected elements from the form. See http://drupal.org/node/112358 for an example. You can use unset() to remove elements you do not want.

DrupalNovice’s picture

You're right nevets, nesting is not the answer..I tried the following...:

function phptemplate_preprocess_node(&$vars) {
 
  if (module_exists('comment') && isset($vars['node']) && $vars['type'] == 'my-custom-content-type') {
  
    function mytheme_comment_form($form) {
      unset($form['#submit']);
	  unset($form['name']);
	  unset($form['preview']);
	  unset($form['submit']);
	  return dsm($form);
    }
    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
  }
}

...and no changes was made to the form whatsoever.
I even tried the function on it's own:


function mytheme_comment_form($form) {
  unset($form['#submit']); 
  unset($form['name']);
  unset($form['preview']);
  unset($form['submit']);
}

...and it didn't work either.

Anyway, you probably meant doing this in the dedicated module..

I don't understand why the Drupal Core developers removed the two arguments from drupal_get_form()..:

4.7
drupal_get_form($form_id, &$form, $callback = NULL)
5 – 7
drupal_get_form($form_id)

...I could have used it to pass in a modified $forms array..(but something else would probably have messed it up so it wouldn't have worked anyway..=P(

So the Only solution to my problem is to build a custom module to achieve the results? (a last nail in the coffin'? plz ;)

nevets’s picture

I think if you change mytheme_comment_form() to

function mytheme_comment_form($form) {
  unset($form['#submit']);
  unset($form['name']);
  unset($form['preview']);
  unset($form['submit']);

  return form_render($form);
}

you will closer. (I wonder about unsetting #submit and submit)

DrupalNovice’s picture

Thanks nevets, I got it working now..

It seemed that the only way to achieve what I want on the theme layer was to use a preprocess_node function and a theme_form function nested inside the preprocessor function. I then assigned the modified form to the referenced $var parameter in the preprocess_node function through drupal_render('form_name'). And last but not least, I printed out the form on the specific location I wanted on the node page.

robert-hartl’s picture

With your code I'm able to print comments and comment_form in node(-xxx).tpl.php exactly where I want.
The comments.tpl.php defines the html output. Allright.

But I can not define the output of the comment_form. It is always the same with standard fields. Also the allowed memory is in this constellation not enough for a preview. But unsetting preview would ...
And the (core) pagination links are disapeared. Hm.

There are comment-form.tpl.php, no cache, all modules up to date, ...
In my template.php:

function phptemplate_preprocess_node(&$vars) {
    $vars['node']->comment = 0;
    if (module_exists('comment') && isset($vars['node'])) {
    function mytheme_comment_form($form) {
      unset($form['homepage']);
      unset($form['preview']);
      return drupal_render($form);
    }
    $vars['comments'] = comment_render($vars['node']);
    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));
    }
}

Also this won't let the comment-form-template work:

function phptemplate_comment_form($form) {
  return _phptemplate_callback('comment-form', array('form' => $form));
}

Any ideas, what I could try? I have spent already hours searching a solution. Thank you very much.

DrupalNovice’s picture

Yup, _phptemplate_callback is obsolete to Drupal 6.x.
Instead use the theme or the theme_render_template functions.

Remember that you must always register any custom theme functions before creating them.
So try adding this to you template file (without the php-tags):

function themename_theme() {
  return array(
    // The form ID.
    'comment_form' => array(
      // Forms always take the form argument.
      'arguments' => array('form' => NULL),
    ),
  );
}

I hope this will help you!

robert-hartl’s picture

Thank you very much,
I'll give it a try.

robert-hartl’s picture

function mytheme_theme() {
  return array(
    'comment_form' => array(
      'arguments' => array('form' => NULL),
      'template' => 'comment-form', // this is the name of the template
    )
  );
}

does not work.

Also I have detected, that the line

    $vars['comments'] = comment_render($vars['node']); 

above kills the pagination at the homepage etc.;

and the line

    $vars['comment_form'] = drupal_get_form('comment_form', array('nid' => $vars['node']->nid));

let me place the comment_form but without formatting

:-/