Avatar of namsu55
namsu55
Flag for United Kingdom of Great Britain and Northern Ireland asked on

How can I add a new form field using hook_form_alter in Drupal

I am trying to do the following:

Load a page with a form which is built using CCK (Content Construction Kit). I have more than 10 plus fields displayed. I require an additional field to appear but require code to deal with this because of a condition that will display the field with filled values or even make it read only.

From what I know about hook_form_alter it is used to intercept a form and render something before hand.

Now I have tried the following for example:

This is obviously inside a function with a $node argument and this doesn't work.
$node->field_satellitechoice[0]['value'] = "Sat1";

Open in new window


Also inside hook_form_alter I have tried this:
function portal_order_form_form_alter(&$form, $form_state, $form_id) {

	if($form_id == 'new_service_order_form_node_form') {
	$form['#field_info']['field_satellitechoice']['type'] = "hidden";
    	$form['#after_build'][] = 'portal_order_form_after_build';
    	//Add in additional validation required for select boxes
    	$form['#validate'][] = 'portal_order_form_custom_validate';
	}
}

Open in new window


So based on an if condition I want to throw in my new field before the page is fully shown to the end user with a filled value.

How do I go about this?
DrupalPHP

Avatar of undefined
Last Comment
namsu55

8/22/2022 - Mon
sivagnanam chandrakanth

Check the array structure of your field by printing $form and assign your pre defined value for ex: $form['#field_info']['field_satellitechoice']['type']['value'] = 'some text';

Also I dont see "return $form;" at the end of your code... add that also in case if you missed
namsu55

ASKER
Currently I am using this:

 $node->field_satellitechoice[0]['value'] = $availableSat;

Open in new window


This sets the value on the field however I don't think changing the type works from using the $node value. Let me try hook_form_alter.
namsu55

ASKER
My structure is fine when I write to the $form array in hook_form_alter, I have an after build and validate function going straight after. I want to do this procedure before I get to those points.

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

        //Line I added
	$form['#field_info']['field_satellitechoice']['field_name'] = 'hidden';

    	$form['#after_build'][] = 'new_service_order_form_after_build';
    	$form['#validate'][] = 'new_service_order_form_custom_validate';
}

Open in new window


The structure is fine because I have done a print_r on it. So it definitely is not that.
Your help has saved me hundreds of hours of internet surfing.
fblack61
namsu55

ASKER
Have a look at this code:

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

$form['foo'] = array(
  '#type' => 'textfield',
  '#title' => t('bar'),
  '#default_value' => $edit['foo'],
  '#size' => 60,
  '#maxlength' => 64,
  '#description' => t('baz'),
);

}

Open in new window


This also uses CCK to develop the forms. I have managed to add a form using hook_form_alter. For some reason on the main environment I am working on it doesn't work.
ASKER CERTIFIED SOLUTION
namsu55

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
namsu55

ASKER
No one else found one and so found one from the internet.