Drupal 6 :: Custom Search Box

本文介绍如何通过创建自定义模块并使用form_alter钩子来修改Drupal 6中的搜索框,包括更改默认按钮文本、搜索框提示文本,并在用户点击时清除默认文本。

hello Friends,

I came across a scenario where it was needed to customize Drupal 6 search box for my current drupal based theme and this customization was needed to :

  1. Change Default text on submit button
  2. Change Default text in search box (Search this site)
  3. When user clicks on search box, default search box text should disappear

All of above tasks should be easily attainable if you are not working in Drupal but it doesn’t mean that these are very difficult while working in Drupal. Somehow, it is a little tricky.

Most recommendable and suitable way that i found was to create a new module and use form_alter hook to track the search form events and bring all above changes in these events.

I’ll recommend you to read this thread: http://drupal.org/node/214592 where Heinehelped alot to get clear ideas about form_alter hooks and their usage. Hopefully it will help you too.

Now lets move towards the solution.

First of all create a new module under sites/all/modules with any name. Say it is named ‘abc’ (supposing that you will be aware of the process of module creation and all files needed to create for a new module in Drupal). Open your abc.module file and create a new function there named ‘abc_form_alter‘ having parameters (&$form, $form_state, $form_id).  Basically, ‘abc_form_alter‘ will implement hook_form_alter (explained well http://drupal.org/node/214592) and will use this function to grab search form events and implement my search box changes on triggering of those events.  So this is the summary of our solution and following code you’ll write in abc.module file.

function abc_form_alter(&$form, $form_state, $form_id) {   if($form_id=='search_theme_form')
  { 	$form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));
	$form['search_theme_form']['#default_value'] = 'Search My Site';
	$form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );
  } }

and That’s All :) .

Lets make a short review on above code,

This condition if($form_id==’search_theme_form’) will check if ‘search_theme_form’ is about to render then do following stuff:

$form['submit'] = array('#type' => 'submit', '#value' => t('[ GO ]'));

Above Line will change the default submit button text and make it to be [ GO ]

 $form['search_theme_form']['#default_value'] = 'Search My Site';

This code Line will Change Default text in search box and make it to be ‘Search My Site’

 $form['search_theme_form']['#attributes'] = array('onfocus' => "if (this.value == 'Search My Site') {this.value = '';}" );

This final code line gives the way to change any attribute for this text box (you may use it to change the default css class used for search box, too). Here i changed the ‘onfocus’ attribute and wrote the javascript code so when user click on search box for searching some keyword, search box’s default text (Search My Site) should get vanished.

One last thing, if you’r at very basic level in Drupal or even not a programmer then i’ll recommend you to go for this module Custom Search Box. This module is good but keep in mind that you won’t have everything in your hand while using this module but above process can help you to play freely with search form attributes.

I hope this post will open new horizons for you to customize any Drupal based Form

转载于:https://my.oschina.net/u/187928/blog/34364

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值