---
title: "Drupal Tip: Using $form['#after_build'] · Appnovation"
url: "https://www.appnovation.com/insights/drupal-tip-using-formafterbuild"
description: "How to use Drupal's $form['#after_build'] property to modify forms after they're built, a handy tip for form_alter work."
---

Article

# Drupal Tip: Using $form\['#after\_build'\]

[![](https://cdn.sanity.io/images/ey850001/production/f19d29fd30e3e4d3e07d971ea644d23a0c414afb-68x68.png?w=68&auto=format) Justin Tsang Team Lead](/people/justin-tsang.md) Jan 26, 2011  
 2 min read 

![Appnovation Blog Default Header](https://cdn.sanity.io/images/ey850001/production/9efe1c26d9584ecfb3dfef5a63e698bf4567d305-1440x600.jpg?w=1400&auto=format) 

Modifying a form in Drupal may be a little challenging sometimes, so here is a small tip for those working with form\_alter or forms in general.

There is an optional form element called $form\['#after\_build'\] it takes an array of functions to call (similar to $form\['#submit'\]). What this does is it runs after the form or element is built for display.

When is it a good time to use this? The answer is most of the times when a modification to CCK fields should be done, this is to ensure that the entire form and all the other form alterations have already occurred from all modules. Lets say you want to form\_alter an attribute or value of a field in a form, but it is altered in another contributed module already. Depending on the weight of the modules your form\_alter may get overwritten so the solution is to run it after the form has been built. At this point you want to make sure it runs the alterations in the contributed modules first before your change occurs, then it is best to use #after\_build.

I have also encountered times when I want to load a js or css file whenever the form is rendered. If I place it into a form\_alter they are loaded fine, but if I submit the form and the form does not pass validation the page is reloaded without the js or css file added. Placing the drupal\_add\_js or drupal\_add\_css into the after\_build solves these issues.

Here is some basic code for reference, which goes into one of your custom modules.

`function yourmodulename_form_alter(&$form, $form_state, $form_id) {` `if($form_id == 'thisformid'){` `$form['field']['#prefix'] = 'This text gets overwritten by the after build';` `$form['#after_build'][] = 'yourmodulename_after_build'; //Calls function` `//drupal_add_css('hello.css'); This line does not work when validation fails` `}` `}` `function yourmodulename_after_build($form, &$form_state) {` `$form['field']['#prefix'] = 'This text will rule them all!';` `drupal_add_css('hello.css'); //This line works even after validation fails.` `return $form;` `}`

[Drupal API #after\_build (opens in new window)](http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6#after_build)

[![](https://cdn.sanity.io/images/ey850001/production/f19d29fd30e3e4d3e07d971ea644d23a0c414afb-68x68.png?w=68&auto=format) Justin Tsang Team Lead · Appnovation](/people/justin-tsang.md) 

## Keep reading

[![What is Sovereign AI](https://cdn.sanity.io/images/ey850001/production/a5889abdd5e80376912546622e046e3fbef234b4-2496x1664.png?w=640&auto=format)](/insights/what-is-open-source-sovereign-ai.md)

 Article · 7 min AI 

### [What Is Open Source, Sovereign AI?](/insights/what-is-open-source-sovereign-ai.md)

Every company is encouraging more AI, and almost every AI bill is climbing. Sovereign AI is about two kinds of control: over your data and over your costs. Here's why open source sits at its foundation.

[Iain MacNeil](/people/iain-macneil.md) Article 

[![AI-first experience design](https://cdn.sanity.io/images/ey850001/production/8bbc2418e87924ef10f36115478a2e10d5bed95e-1584x624.jpg?w=640&auto=format)](/insights/how-build-ai-first-experience-without-throwing-out-everything-youve-already-built.md)

 Article · 6 min AI Sep 10, 2026 

### [How to Build an AI-First Experience Without Throwing Out Everything You've Already Built](/insights/how-build-ai-first-experience-without-throwing-out-everything-youve-already-built.md)

Users now expect a direct answer, not a list of links to click through. Here's how organizations can layer an AI-First experience onto their existing website or app, in weeks rather than years.

[Andrew Douglas](/people/andrew-douglas.md) Article 

[![What Becomes Possible When AI Can Read All the Data](https://cdn.sanity.io/images/ey850001/production/c28ab0bd26753421ee42937e13adc7a7f19b47f0-1296x510.jpg?w=640&auto=format)](/insights/what-becomes-possible-when-ai-can-read-all-data.md)

 Article · 4 min AI Sep 09, 2026 

### [What Becomes Possible When AI Can Read All the Data](/insights/what-becomes-possible-when-ai-can-read-all-data.md)

Most of the world's data has never been analyzed, only sampled. AI changes that. How AI-ready data platforms unlock questions no one could ask before.

[Appnovation](/people/appnovation.md) Article 

## Get the thinking,  
*before everyone else.*

One considered email a month. New essays, field notes from live engagements, and the occasional strong opinion. No noise.
