0

here is my template.php code:

function sizin_preprocess_page(&$variables){
  $search_box = drupal_render(drupal_get_form('search_form'));
  $variables['search_box'] = $search_box;            
}  



function sizin_form_alter(&$form, &$form_state, $form_id) {
  if ($form_id == 'search_form') {
      // HTML5 placeholder attribute
      $form['search_form']['#attributes']['placeholder'] = t('Search ...');    
  }    
}  

and in my page.tpl code:

  <?php print $search_box; ?>  

but I can't able to see placeholder. Any Idea?

0

Install Placeholder module. Using this module you can convert any label of any elements of any form in your site in to a placeholder. Just add the CSS style of the form element in to this module configuration and it will do the rest of the jobs


You can also try this

$form['search_form']['#attributes'] = array('placeholder' => 'Search ...');
3
  • thanks, but i don't want to install a module for this purpose
    – user23811
    Aug 3 '15 at 6:37
  • I updated the answer
    – M a m a D
    Aug 3 '15 at 6:44
  • nope , it's not working yet
    – user23811
    Aug 3 '15 at 7:55
0

Your first function is right. You just need to change a form id in second function like this;

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

You were using search_form id but you need to got search_block_form . Try this and inform here.

3
  • didn't work yet , the form id is not search_block_form !
    – user23811
    Aug 3 '15 at 7:53
  • is your theme's name correct? Did you flush your cache?
    – WaQaR Ali
    Aug 3 '15 at 7:58
  • For debugging purpose simply enable core search block in any region and see if placeholder take place in that box ?
    – WaQaR Ali
    Aug 3 '15 at 8:06
0

You need to add 'placeholder' attribute in the theme_textfield function

function yourtheme_textfield($variables) {
  $element = $variables ['element'];
  $element ['#attributes']['type'] = 'text';
  element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength', 'placeholder'));
  _form_set_class($element, array('form-text'));

  $extra = '';
  if ($element ['#autocomplete_path'] && drupal_valid_path($element ['#autocomplete_path'])) {
    drupal_add_library('system', 'drupal.autocomplete');
    $element ['#attributes']['class'][] = 'form-autocomplete';

    $attributes = array();
    $attributes ['type'] = 'hidden';
    $attributes ['id'] = $element ['#attributes']['id'] . '-autocomplete';
    $attributes ['value'] = url($element ['#autocomplete_path'], array('absolute' => TRUE));
    $attributes ['disabled'] = 'disabled';
    $attributes ['class'][] = 'autocomplete';
    $extra = '<input' . drupal_attributes($attributes) . ' />';
 }

  $output = '<input' . drupal_attributes($element ['#attributes']) . ' />';

  return $output . $extra;
}

Your Answer

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