0

I have a view with an exposed filter for the node titles. I want to add X in that exposed filter.

I know html5 provides feature to have cross symbol when the input type is search. I tried changing #type in hook_form_alter() for the text-field to search, but it doesn't work.

Is it possible in Drupal 7?

1 Answer 1

1

You can do something like below:

write below code in any module

function modulename_theme() {
  $themes = array (
      'search_input' => array(
      'render element' => 'element',
    ),   
  );
  return $themes;
}

/**
 * Implementation of hook_element_info()
 */
function modulename_element_info(){
  $types['search_input'] = array
  (
    '#input' => TRUE,
    '#theme' => 'search_input',
    '#theme_wrappers' => array('form_element'),
  );

  return $types;
}

write below code in template.php:

function themename_search_input($variables){ //echo "<pre>";print_r($variables);exit;
  $element = $variables['element'];
  $element['#attributes']['type'] = 'search';
  element_set_attributes($element, array('id', 'name', 'value', 'size', 'maxlength'));
  _form_set_class($element, array('form-number'));

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

    $attributes = array();
    $attributes['type'] = 'hidden';
    $attributes['id'] = $element['#autocomplete_input']['#id'];
    $attributes['value'] = $element['#autocomplete_input']['#url_value'];
    $attributes['disabled'] = 'disabled';
    $attributes['class'][] = 'autocomplete';
    $extra = '<input' . drupal_attributes($attributes) . ' />';
  }

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

  return $output . $extra;
}

In cs file:

input[type=search]::-webkit-search-cancel-button {
    -webkit-appearance: searchfield-cancel-button;
}

and use #type = search_input. It may help you

3
  • Hi Suraj, Thanks for helping ..But I use this code in my custom module..and its not working. Its not giving any error..and Field not rendered.
    – Milind
    Commented May 25, 2017 at 13:29
  • Hey sorry buddy, now i added one more function plz add that also to module file. then it will work perfectly.
    – Suraj
    Commented May 26, 2017 at 6:34
  • @Milind, Did u tried this?
    – Suraj
    Commented May 29, 2017 at 4:50

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.