0

I have the following code to alter the way comment form is displayed. After the bellow code everything is displayed as i want to but it does not add the comments.

 global $user;
     if ($user->uid) {
       $form['author']['_author'] = array
     (
        '#tittle' => 'Name',
        '#attributes' => array('id'=>array('name')),
        '#prefix' =>'<ul> <li class="name_field_c"><label for="name">Name</label>'.$form['author']['_author']['#markup'],
        '#suffix' => '</li>'
     );   
     }
     else {
     $form['author']['name'] = array
     (
        '#type' =>'textfield',
        '#attributes' => array('id'=>array('name')),
        '#prefix' =>'<ul> <li class="name_field_c"><label for="name">Name</label>',
        '#suffix' => '</li>'
     );  
     }

     $form['author']['mail'] = array(
        '#type' =>'textfield',
        '#attributes' => array('id'=>array('email')),
        '#prefix' => '<li class="email_field_c"><label for="email">Email (optional)</label>',
        '#suffix' =>'</li>',
        '#required'=>TRUE,
        '#weight' => 2
     );

    $form['comment_body'] = array
     (
        '#type' => 'textarea',
        '#attributes' => array('id'=>array('comment')),
        '#prefix' => '<li class="comment_field_c"><label for="comment">Comment</label>',
        '#suffix' => '</li>',
        '#weight' => 4,
        '#resizable' => FALSE
     );


     $form['author']['homepage']['#access'] = FALSE;
     $form['subject']['#access'] = FALSE;


     $form['captcha']['#prefix'] = '<li><div class="captcha">';
     $form['captcha']['#suffix'] = '</div>';
     $form['actions']['submit'] = array (
        '#prefix' => '<button type="submit">',
        '#suffix' => '</button></li></ul>',
        '#markup' => '<span>' . t('Say your piece') . '</span>'
     );

    $form['#action'] = $_SERVER['REQUEST_URI'];

To submit the form i have to press twice on the submit button ( how can i solve it ? ) and after i submit the form again i get the following errors and i cannot figure it out how to solve them:

Warning: array_values() [function.array-values]: The argument should be an array in _field_filter_items() (line 513 of /home/mcristi/public_html/jhub/modules/field/field.module).
Warning: Invalid argument supplied for foreach() in text_field_validate() (line 119 of /home/mcristi/public_html/jhub/modules/field/modules/text/text.module).
Notice: Undefined index: homepage in comment_form_validate() (line 2155 of /home/mcristi/public_html/jhub/modules/comment/comment.module).

This thing causes the problem that the form is not submitted/saved :

 $form['actions']['submit'] = array (
        '#prefix' => '<button type="submit">',
        '#suffix' => '</button></li></ul>',
        '#markup' => '<span>' . t('Say your piece') . '</span>'
     );

    $form['#action'] = $_SERVER['REQUEST_URI'];

Is there any possibility to save the comment on the node page ? ( to not be redirected to /comment/reply/$nid)

1
  • As side note, you are using #tittle as property, when it should be #title.
    – avpaderno
    Commented Jan 4, 2013 at 15:13

2 Answers 2

2

You're overwriting the form elements that are already there, some of which are fields and so need to have their array structured very specifically (Why is hook_form_alter so messy in d7? has some background for that).

You can alter existing elements in the form without overwriting the whole thing like so:

$form['comment_body']['#prefix'] = '<li class="comment_field_c"><label for="comment">Comment</label>';
$form['comment_body']['#suffix'] = '</li>',
2
  • Thank you for the answer . you were right , but now i have another problem ... I cannot submit the comment it redirects me to the same page with the form completed and the comment is not saved ... can you help me with a suggestion ?
    – Vlasin
    Commented Jan 4, 2013 at 12:01
  • 1
    It's pretty much impossible without reproducing your environment locally I'm afraid, but the best thing to do is comment out all your custom code, then un-comment each line one by one until you find which one is causing the problem. If you're unsure why that particular code is causing problems you can then update your question or ask a new one
    – Clive
    Commented Jan 4, 2013 at 12:11
2

I don't understand why you did #prefix and #suffix with <button> on $form['actions']['submit'] which have already generated a submit button:

  <input type="submit" id="edit-submit" name="op" value="Save" class="form-submit">

You destroyed the default submit button and replaced with

  <button type="submit"><span>Say your piece</span></button>

This would cause the problem of the comment not saved. You could update #value of the submit button.

   $form['actions']['submit']['#value'] = 'Say your piece';

Redirecting to "comment/reply/$nid" is not a problem since the related node is displayed on that page.

1
  • Thank you for the answer, but i am using views to design the nodes and redirecting to comment/reply/$nid it is a problem.
    – Vlasin
    Commented Jan 7, 2013 at 8:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.