博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
How to customise the search box in Drupal 6
阅读量:7108 次
发布时间:2019-06-28

本文共 3029 字,大约阅读时间需要 10 分钟。

hot3.png

By default, Drupal allows you to include a search box directly into your theme. In most themes, when enabled, this search box will show up in the primary navigation bar as an input box labeled "Search this site", with a submit button labeled "Search". But what if you want to alter or completely hide these labels, or even add a new class to the input box?

How to do it

There have been loads of suggestions about how to do this on drupal.org, including altering the core Drupal source (not a good idea!), using a string replace function in a custom template file or using the . But, the best suggestion came from  and is the most Drupal friendly way of doing it.

Use Drupal 6's  in your theme's template.php file to modify the template variables before they are passed into the template files.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
* Override or insert PHPTemplate variables into the search_theme_form template.
*
* @param $vars
*   A sequential array of variables to pass to the theme template.
* @param $hook
*   The name of the theme function being called (not used in this case.)
*/
  
function
mytheme_preprocess_search_theme_form(&
$vars
,
$hook
) {
  
  
// Modify elements of the search form
  
$vars
[
'form'
][
'search_theme_form'
][
'#title'
] = t(
'Search mysite.com'
);
  
  
// Set a default value for the search box
  
$vars
[
'form'
][
'search_theme_form'
][
'#value'
] = t(
'Search'
);
  
  
// Add a custom class to the search box
  
$vars
[
'form'
][
'search_theme_form'
][
'#attributes'
] =
array
(
'class'
=> t(
'cleardefault'
));
  
  
// Change the text on the submit button
  
$vars
[
'form'
][
'submit'
][
'#value'
] = t(
'Go'
);
  
  
// Rebuild the rendered version (search form only, rest remains unchanged)
  
unset(
$vars
[
'form'
][
'search_theme_form'
][
'#printed'
]);
  
$vars
[
'search'
][
'search_theme_form'
] = drupal_render(
$vars
[
'form'
][
'search_theme_form'
]);
  
  
// Rebuild the rendered version (submit button, rest remains unchanged)
  
unset(
$vars
[
'form'
][
'submit'
][
'#printed'
]);
  
$vars
[
'search'
][
'submit'
] = drupal_render(
$vars
[
'form'
][
'submit'
]);
  
  
// Collect all form elements to make it easier to print the whole form.
  
$vars
[
'search_form'
] = implode(
$vars
[
'search'
]);
}

Note: you must rename the preprocess function so it's right for your theme (replace mytheme with the name of your theme)

In order for this to work, you must also ensure you have a copy of search-theme-form.tpl.php in your theme directory (you can grab it out of modules/search/ from your Drupal core files). You can then edit this as you like for further theming.

Extending it further.

The same techniques can also be applied for the search block. Just use the code from above, but replace all instances of search_theme_form with search_block_form, and make sure that you take a copy of search-block-form.tpl.php into your theme directory.

On a similar note, you might be interested in my article on Customizing the comment form in Drupal 6.

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

你可能感兴趣的文章
错误页定义方法
查看>>
Guid.NewGuid() 和 new Guid()的区别
查看>>
我的友情链接
查看>>
vim技巧
查看>>
DotNetTextBox V3.0 所见即所得编辑器控件 For Asp.Net2.0(ver 3.1.2Beta)
查看>>
mysql-5.6.25-linux-glibc2.5-i686.tar.gz错误
查看>>
nginx 服务并发过10万的linux内核优化配置
查看>>
Oracle数据库体系架构概要
查看>>
extjs4视频学习笔记2
查看>>
【学神-RHEL7】1-28-mariadb数据库自动备份和expect的使用
查看>>
2017年要学习的三个CSS新特性
查看>>
C#的Unit Test如何根据exception来判断函数是否执行正确
查看>>
SQL优化
查看>>
RMAN 还原与恢复
查看>>
mysql 远程连接数据库的二种方法
查看>>
[你必须知道的.NET]第二十一回:认识全面的null
查看>>
Lexus Extroic OpenCart 2.X 自适应主题模板 ABC-0648-01
查看>>
awk案例
查看>>
linux中vmstat命令详解
查看>>
软件测试基础知识整理三----白盒测试
查看>>