Alter form submit buttons in Drupal 6

  • Posted on: 14 April 2012
  • By: Antti

I love little snippets of code that I can just copy-paste in whenever needed. One customisation that I've often needed in Drupal is altering the default form submit button values. In this article you will find how to change the submit button values in Search, Node and Views forms.

Alter Search form

Probably the most common case is having the need to alter the core search form. By default the value of the submit button is 'Search' which will do in most cases but sometimes you might want to have it say 'Go' or something more specific like 'Search this site'.

Copy the following code(s) to the template.php file located in your theme folder. Change the MYTHEMENAME to your theme name and the submit button text to your liking. Note that the only difference in the codes is the function name.

In a Block

/* Alter Core Search Block form */

function MYTHEMENAME_preprocess_search_block_form(&$vars, $hook) {
 
  // Change the text on the submit button
  $vars['form']['submit']['#value'] = t('Go');
 
  // Rebuild the form
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);
  $vars['search_form'] = implode($vars['search']);
  
}

In Theme

/* Alter Core Search Theme form */

function MYTHEMENAME_preprocess_search_theme_form(&$vars, $hook) {
 
  // Change the text on the submit button
  $vars['form']['submit']['#value'] = t('Go');
 
  // Rebuild the form
  unset($vars['form']['submit']['#printed']);
  $vars['search']['submit'] = drupal_render($vars['form']['submit']);
  $vars['search_form'] = implode($vars['search']);
  
}

Alter Node form

Another commonly asked question is how to alter the node form submit buttons. The default value in node submit buttons is 'Save' which works well for admin use but if you are allowing your site visitors to post content you might want to change this to e.g. 'Submit' instead.

In order to accomplish this we will need to create a module. Don't worry, it's not hard at all. If you already have a custom module available then you can just copy-paste the function in.

At the minimum a module requires 2 files; modulename.info and modulename.module. I'll be creating a module called 'custom_functions'. Having your custom module for a site is really handy as many Drupal customisations require you to place the code in a module. Note that if the code doesn't seem to work you may need to clear the site cache.

custom_functions.info

name = Custom Functions
description = Helper functions for my Drupal site
core = 6.x

custom_functions.module

<?php

/* Alter Node forms */

function custom_functions_form_alter(&$form, $form_state, $form_id) { 

	switch($form_id) {
		// Page node
		case "page_node_form":         
		$form['buttons']['submit']['#value'] = "Submit";         
		break;
	}
	
}

?>

Download custom_functions module

If you prefer you can also download the custom_functions module and save in your modules folder.

Alter Views form

The Views module enables you to create custom forms by exposing the form elements to the user. By default the Views submit button displays 'Apply' which is not that great. I typically change it to 'Filter' when having a list or a more common 'Search' for custom searches.

Copy the following code to the template.php file located in your theme folder. Change the MYTHEMENAME to your theme name, the form id and the submit button text to your liking. To find out your form id view the page source and look for the id value of your form.

/* Alter Views Exposed forms */

function MYTHEMENAME_preprocess_views_exposed_form(&$vars, $hook) {

	switch($vars['form']['#id']) {            
		// Views search page
		case "views-exposed-form-views-custom-search-page-1": 
        
		// Change the text on the submit button
		$vars['form']['submit']['#value'] = t('Filter');

		// Rebuild the form
		unset($vars['form']['submit']['#printed']);
		$vars['button'] = drupal_render($vars['form']['submit']);      
		break;
	}
}