2

I want to add a placeholder to my all comment form , here is what i did :

template.php

function ThemeName_form_alter(&$form, &$form_state, $form_id) {
    if ($form_id == 'search_block_form') {
        // HTML5 placeholder attribute
        $form['search_block_form']['#attributes']['placeholder'] = t('Serach ...');

    }


    switch ($form_id)  {
        case 'comment_form':
            $form['name']['#attributes']['placeholder'] = t( 'Name' );
            $form['subject']['#attributes'] = array('placeholder' => t('Subject'));
            $form['comment_body']['#attributes'] = array('placeholder' => t('comment'));

            break;
    }

}  

nothing happend , Any Help ?

Thanks

Update
Finally i could add placeholder to comment form like this

function sizin_form_comment_form_alter(&$form, &$form_state)
{
    $form['author']['name']['#attributes']['placeholder'] = t( 'Name' );
    $form['author']['name']['#title']= FALSE;

    //Subject
    $form['your_comment']['subject'] = $form['subject'];
    $form['your_comment']['subject']['#title'] = FALSE;
    $form['your_comment']['subject']['#attributes'] = array('placeholder' => t('title'));
    unset($form['subject']);
    $form['your_comment']['subject']['#weight'] = -10;

    //Comment
    $form['your_comment']['comment_body'] = $form['comment_body'];
    $form['your_comment']['comment_body'][LANGUAGE_NONE][0]['value']['#attributes'] = array('placeholder' => t('comment'));
    unset($form['comment_body']);


}  

But i couldn't add to textarea(comment Body) ? AnyHelp ?

  • 1
    Read through this, it has the answer in a round about way – Clive Jun 25 '15 at 11:33
  • Sorry, i didn't get the point – user23811 Jun 25 '15 at 11:41
  • Comment body is a field, so you need to target the element, which will be further down the array. Maybe something like $form['comment_body'][LANGUAGE_NONE][0]['value']['#attributes']['placeholder'] = 'foo'; – Clive Jun 25 '15 at 11:43
  • so if i was wrong about the body field , the others not working ? – user23811 Jun 25 '15 at 11:45
  • here is my comment name property : comment_body[und][0][value] – user23811 Jun 25 '15 at 11:48
1

Reading through Placeholder module I come to know this

Once FAPI uses HTML5 to render elements, this module can be replaced by the "placeholder" attribute available as part of the HTML5 spec. You can update the FAPI to use the "placeholder" attribute right now by using elements module. This means that this module is only useful if you are concerned about older browsers that do not support "placeholder".

try using elements module so the support for HTML5 attribute can become available in your installation.

Here is the way to add default placeholder attribute.

| improve this answer | |
1

Thanks @tonystar

Instead of:

$form['your_comment']['comment_body'][LANGUAGE_NONE][0]['value']['#attributes'] = array('placeholder' => t('comment'));

add this:

$form['your_comment']['comment_body'][LANGUAGE_NONE][0]['#attributes']['placeholder'] = t('comment');
| improve this answer | |
  • Please add some more info: is this code a part that should be replaced? Or new code that should be placed instead of... what? And how did it help, exactly? – Mołot Jun 26 '15 at 9:15
  • updated my answer , please see this : i've asked my question one more time : drupal.stackexchange.com/questions/163312/… – user23811 Jun 26 '15 at 9:36
0

Install Placeholder Module

Then you can use

$form['name']['#placeholder'] = t( 'Name' );
$form['subject']['#placeholder'] = t('Subject');
$form['comment_body']['#placeholder'] = t('comment');
| improve this answer | |
  • 2
    That won't work! It will for name/subject, but not for the field. You need to attach the property to the form element, which is further down – Clive Jun 25 '15 at 11:45
  • Thanks but i don't want to use module for that – user23811 Jun 25 '15 at 11:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy