troubleshooting Question

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

Avatar of namsu55
namsu55Flag for United Kingdom of Great Britain and Northern Ireland asked on
6 Comments1 Solution692 ViewsLast Modified:
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";

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';

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?

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 6 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 6 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros