For a website I installed Ubercart. Now I would like to add an extra field to the attribute-options-form of Ubercart. I succeeded with adding a field (I implemented hook_form_FORM_ID_alter()), but now I would like to adjust the view of the form, because the view isn't viewed correct on the following page:
admin/store/products/classes/testproduct/options
The added fields don't appear within the table, but underneath.

I already discovered that the view can be adjusted bij calling theme_FORM_ID(), where FORM_ID is the id of the form. The problem is that the function theme_uc_object_options_form() is already defined in uc_attribute.admin.inc. Now I'm looking for a way to override this function, because I don't prefer to adjust original code.

On the Drupal Form API I read that I needed to add '#theme' to the form-array and give the name of the function to be called:
http://api.drupal.org/api/drupal/developer--topics--forms_api.html/6
(look for the first bullet underneath the header 'Theming Forms').

I tried this, but the function I had given, doesn't seem to be called. The view of the form is different however and the function theme_uc_object_options_form() doesn't get called anymore after adding '#theme' to the form-array.

Code:

<?php
/**
* Implementation of hook_form_FORM_ID_alter()
* @param array $p_aForm
* @param array $p_aFormState
* @return void
*/
function uc_attribute_variable_price_form_uc_object_options_form_alter(&$p_aForm, $p_aFormState)
{
	// Code for adding the field
	// (...)
	
	// Add submit function
	$p_aForm['#submit'][] = 'uc_attribute_variable_price_form_uc_object_options_form_submit';

	// Custom theme-function
	$p_aForm['#theme'] = 'justatest';
}

// Just a few testfunctions, to test which function got called

/**
* theme_justatest()
*/
function theme_justatest($p_aForm)
{
	die('theme_justatest called');
}

/**
* theme_justatest_form()
*/
function theme_justatest_form($p_aForm)
{
	die('theme_justatest_form called');
}

/**
* justatest()
*/
function justatest($p_aForm)
{
	die('justatest called');
}

/**
* justatest_form()
*/
function justatest_form($p_aForm)
{
	die('justatest_form called');
}
?>

How can I use an own function for the view of the form? None of the functions in the code example got called.

Comments

jaypan’s picture

in your hook_form_alter(), you can add a theming function;

$form['#theme'][] = 'mytheme';

You will have to call hook_theme() to register this theme, then define the theme function as well.

Contact me to contract me for D7 -> D10/11 migrations.

megachriz’s picture

Thanks for your reply. It took a while before I figured out how to register a theme correctly. I had defined the theme function in the module-file, but it seems I only could use the name of the module as the name of the theme. Not handy if I want to overwrite multiple theme functions.

Code:

<?php
/**
* Implementation of hook_form_FORM_ID_alter()
* @param array $p_aForm
* @param array $p_aFormState
* @return void
*/
function uc_attribute_variable_price_form_uc_object_options_form_alter(&$p_aForm, $p_aFormState)
{
    // Code for adding the field
    // (...)
   
    // Add submit function
    $p_aForm['#submit'][] = 'uc_attribute_variable_price_form_uc_object_options_form_submit';

    // Custom theme-function
    $p_aForm['#theme'][] = 'uc_attribute_variable_price';
}

/**
* uc_attribute_variable_price_theme()
* @param array $p_aExisting
* @param string $p_sType
* @param string $p_sTheme
* @param string $p_sPath
*/
function uc_attribute_variable_price_theme($p_aExisting=array(), $p_sType=null, $p_sTheme=null, $p_sPath=null)
{
	return array(
		'uc_attribute_variable_price' => array (
			'arguments'	=> array('form' => NULL),
			'type'		=> 'module',
		),
	);
}

/**
* theme_uc_attribute_variable_price()
* @param array $p_aForm
* @return string $sOutput
*/
function theme_uc_attribute_variable_price($p_aForm)
{
	$sOutput = '';
	
	// (...)
	
	$sOutput .= drupal_render($p_aForm);
	return $sOutput;
}?>

Is this the only correct way for overriding theme functions (with the overriding-functions defined in the module-file)? Using an other string then 'uc_attribute_variable_price' didn't seem to work. I had cleared all caches after modifying.

EDIT: never mind, my mistake was that I had changed the name of the implementation-function of hook_theme() as well (I shouldn't have changed the function name of uc_attribute_variable_price_theme()).