i used the hook_alter_form to add a field to the comment, when i submit a comment. why the field's content that i added can't show. why?

Comments

Raf’s picture

Did you add a custom submit function as well? ($form['#submit'][] = 'yourmodule_customsubmit')

enjoylife-2’s picture

no, how to write that line. could you make me an example. if i want to the added field content show on the comment . many thanks.

Raf’s picture

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'my_form') {
    $form['extra_field] = array(
      '#type' => 'textfield',
      '#title' => 'The extra field',
    )

    $form['#submit'][] = 'my_submit_function';
  }
}


function my_submit_function($form, &$form_state) {
  // Handle the extra field you added
  // Probably store it in the database
  // Could also just be setting it for display
  // Depends on what the field is used for
}
?>

If you store the values from that extra field, you'll want to load that when a comment gets loaded. That part is tricky. I'm trying to find a way to do that at the moment (didn't find any hooks like hook_comment_load or such, so it might have to be placed in hook_nodeapi, but let's hope it won't be thousands of comments, cause that'll hurt performance)

enjoylife-2’s picture

got it,many thanks.but hook_nodeapi is used to node, the comment in drupal isn't node. am i right?

oh,i am sorry., when i test it to my commet, it can't work. this is my code.

''''''
case 'comment_form':
$form['hello'] = array(
 '#title'=>t('test'),
'#type'=>'textfield',
'#description'=>t('for a test'),
);
break;
$form['#submit'][]='my_submit_test';
}
function my_submit_test($form,&$form_state){
return 'hello world';
}

my comment save button is disappeared.

Raf’s picture

There's something odd with your switch statement. Dunno if it causes the save button to disappear, but it's bound to do odd things nonetheless.

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case 'comment_form':
      $form['hello'] = array(
        '#title'=>t('test'),
        '#type'=>'textfield',
        '#description'=>t('for a test'),
      );
    break;
  $form['#submit'][]='my_submit_test';
}
?>

Normally, the program you're writing the code in, should complain about that, and if not, at least the server will throw a big error when trying to run that code. Basically, you got to close the switch statement before closing the function. Also, I'd put $form['#submit'][] = 'my_submit_test'; within the case, not below it. Right now, it'll perform that submit function for every form on your site. If it's in the case, it'll do that only for comment_form.

You're right. Comments aren't nodes. They're attached to nodes, though. If comments are enabled, and you load a node using node_load(), you'll get all the comments as well. Same goes with the node object in hook_nodeapi. Try this code (but in a testing environment!), you'll see what I mean:

<?php
function mymodule_nodeapi(&$node, $op) {
      switch($op) {
        case 'view':
          print('<pre>');
          print_r($node);
          print('</pre>)';
        break;
    }
}
?>

That code will show the entire structure of the node that's being shown (something regular visitors shouldn't see :P ). A quick search in there for comments will show you how you can alter comments using hook_nodeapi.

enjoylife-2’s picture

when i put the $form['#submit'][]='my_submit_test'; on the break, it still can't work.

Raf’s picture

Can you post your entire hook_form_alter function here? Something's got to be wrong in it. It'll help seeing it completely, like how you run it, and not just a piece.

enjoylife-2’s picture

function mymodule_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case 'comment_form':
$form['hello'] = array(
'#title'=>t('test'),
'#type'=>'textfield',
'#description'=>t('for a test'),
);
$form['#submit'][]='my_submit_test';
break;
}
function my_submit_test($form,&$form_state){
return 'hello world';
}
Raf’s picture

Errors must be turned off on your server, cause that code should give a fatal one :P You forgot a } to close the mymodule_form_alter function.

Try this instead:

<?php
function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch($form_id) {
    case 'comment_form':
      $form['hello'] = array(
      '#title'=>t('test'),
      '#type'=>'textfield',
      '#description'=>t('for a test'),
      );
      $form['#submit'][]='my_submit_test';
    break;
  }
}

function my_submit_test($form,&$form_state){
  return 'hello world';
}
?>
enjoylife-2’s picture

yeah,you're right. you're my hero. but when i click the "save" button of the comment. i can't see the "hello world". why?

<function my_submit_test($form,&$form_state){
  return 'hello world';
}

where is the hello world ?

Raf’s picture

Forms work as follow:

  1. Show the page with the form
  2. When submitted, load another page (one the user won't see, and can't get to with the "back" button) where the submit function is handled
  3. Show the first page again, or the page #redirect points to

Step 2 is there to make sure people don't re-send a form when they refresh the page after having sent it.

What your submit function does, is return 'hello world' (might have meant print or echo, though) to that invisible page.

Usually, if you want to show the results from a submit callback, you'll have to set them in a session var, then print (or echo) that session var on a visible page (like the one where the form is shown). Don't forget to unset it afterwards.

Here's some example code to show you:

<?php
function my_submit_test($form, &$form_state) {
  $_SESSION['my_submit_test_result'] = 'hello world';
}
?>
<?php
function some_function_showing_your_form() {
  if(isset($_SESSION['my_submit_test_result'])) {
    print $_SESSION['my_submit_test_result'];
    unset($_SESSION['my_submit_test_result']);
   // makes sure the result gets shown once,
   // not every time the page is visited.
  }
}
?>
enjoylife-2’s picture

got it,if i put the some_function_showing_your_form() in a tpl.php file, i will get the hello world. am i right?

Raf’s picture

Technically, yes, but it's not good practice. Drupal follows the MVC principle. That means the presentation, actual code and data are all separated. It's better to call your form (using drupal_get_form) and passing that on as argument using the theme() function. I had a look at some other topics you made, and I guess you already understand hook_theme, so you'll know how to do this ;) If I'm mistaken, I don't mind explaining it, though.

enjoylife-2’s picture

many many thanks,you're a warm-hearted man. i know a little about the theme() function.only know all the drupal's output are by means of it. when i want to use a theme()function, i must be register it before(hook_theme). then call the theme function in this style: theme('hook_name','argument'). expect you can give me some more details to make me
refresh it. thank you.

Raf’s picture

Hook_theme():

<?php
function example_theme() {
	return array(
    'example_page' => array(
      'template' => 'example_page',
      'arguments' => array('some_text' => NULL, 'form' => NULL),
    ),
  );
}
?>

Page callback:

<?php
function example_page() {
  $text = 'Some text';
  $form = drupal_get_form('my_form');
  return theme('example_page', $text, $form);
}
?>

example_page.tpl.php:

<h1><?php print t('A static title'); ?></h1>
<p><?php print $some_text; ?></p>
<?php print $form; ?>
enjoylife-2’s picture

in your example where is the theme function?your have registered a theme named 'example_page' that takes two argument, 'some_text' 'form' .

"some one told me before"

<?php

function mymodule_theme()
{
  return array
  (
    'my_theme' => array
    (
      'arguments' => array('my_argument' => NULL),
    ),
  );
}

I have registered a theme named 'my_theme' that takes one argument, 'my_argument'. So maybe my theme function looks like this:
<?php
function theme_my_theme($my_argument)
{
  return '<p>' . $my_argument . '</p>';
}
?>
Raf’s picture

Had to look that way of doing it up :P I actually've never used theme_my_theme, but've always used .tpl.php files.

Basically, the example_page.tpl.php file does what your theme_my_theme function does. It's the same thing, except that you can give the .tpl.php file to a designer and they can do their job on it. With a theme_my_theme function, that's not possible. If it's to just show some text and a form, without a need for a designer to work on it, it's just as good, though. The only real difference is that you can pass a .tpl.php file on to a designer, while you can't pass the function on to him / her.

In your case, theme_my_theme might actually be the better way to go.

enjoylife-2’s picture

i know.you make me know a lot.