doujingjiao0015
2010-09-20 22:47 阅读 66
已采纳

如何在评论表单上方添加免责声明?

I'm looking for a good, standards friendly way to alter the default comments form, such that there is a disclaimer immediately below the "Reply" header. I only want this disclaimer to appear above the comments form itself, not meerly when viewing comments.

This thread ( Drupal: adding disclaimer text above the submit button in a webform ) partially answers what I want but I'm not sure about how to apply the solution specifically to the comments form.

I know that this could be a rank amateur question, but any and all help is appreciated. Thanks.

Edit: I've tried implementing hook_form_alter as suggested and have managed to get the disclaimer to appear within the form just fine. One problem: the first draft of my disclaimer seems to be trapped above the comment form when replying to a comment. Clearing the cache, resetting the theme registry (on every page load, thanks to devel module) all have no effect.

Reply to Comment:
[first version of disclaimer] // won't go away, ever
[comment form]
[current version of disclaimer] // this one is fine
[submit button]

Any help here would again be most appreciated.

Edit (redux): Implemented the template.php centric solution. It was able to work just fine without any of the external effects as described above. Still not sure about a solution to the above problem. Thanks everyone.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    dongle2627 dongle2627 2010-09-21 01:35

    The comment form is a unique animal among Drupal forms in that it's not themeable by default, so the usual methods won't work without a little bit of extra help.

    In short, you'll first need to register the form as themeable in your template.php file:

    /**
    * Implementation of hook_theme().
    */
    function mytheme_theme(){
      return array(
        'comment_form' => array(
          'arguments' => array('form' => NULL),
        ),
      );
    }
    

    Next, you can add a theme function to drop in some additional elements to the form:

    /**
    * Theme the output of the comment_form.
    *
    * @param $form
    *   The form that  is to be themed.
    */
    function mytheme_comment_form($form) {
      $form['new_element'] = array(
        '#type' => 'markup',
        '#title' => t('Disclaimer'),
        '#value' => '<p>You have been disclaimed, sir!</p>',
        '#weight' => -20 // Lighter elements float to the top of the form
      );
      return drupal_render($form);
    }
    

    An understanding of how the Forms API (http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html) works will help you through making these edits. But, if you're just adding some HTML it should be pretty easy.

    Of course, be sure to rebuild your theme registry after you've added the theme function.

    点赞 1 评论 复制链接分享
  • duanliaoyu8419 duanliaoyu8419 2010-09-21 07:29

    While what anschauung has posted will work, it's a lot of extra work compared to using hook_form_alter in a custom module. In your case, it would look something like this, using the close related hook_form_FORM_ID_alter:

    function module_name_form_comment_form_alter(&$form, &$form_state) {
        $form['disclaimer'] = array(
          '#value' => t('Disclaimer text'),
          '#prefix' => '<div>',
          '#suffix' => '</div>',
          '#weight' => 0,  
        );
    }
    
    • In the above example, you need to replace module_name with the name of the custom module you create.
    • The #weight will determine the placement, the lower it is the heigher up it will be placed. So tweak it until the placement is right.
    • #prefixand #suffix is the markup that is created before and after the element, can be anything.
    • #value the value of the form element, the actual disclaimer text.
    • This form element hasn't a #type, so it uses the default which is markup, which means it's just some custom html.
    点赞 7 评论 复制链接分享

相关推荐