Sometimes I hate the Form API and CCK

Yes, the Drupal Form API (FAPI) are great for many things, and most of the time we work great together. Sometimes though, we get into really big fights. FAPI breaks electronic things, I break real world things.

The goal: Create two uneditable text fields that would display in the node edit form with default values and in the node view and be saved in the node object.

As an example, check out the but it's still downright confusing.

Well here is what I ended up. I have a custom content type 'news' and two CCK textfields: "name" and "email".

First setup a .module file:


* Implementation of hook_form_alter().
* For the news story content type
* Make some fields not editable and have default values

function formedits_form_alter(&$form, &$form_state, $form_id) {
if (($form_id == 'news_node_form' && arg(2) == 'edit') || ($form_id == 'news_node_form' && arg(1) == 'add')) {
drupal_add_js(drupal_get_path('module', 'formedits') .'/formedits.js');
$form['#after_build'][] = 'formedits_after_build';

function formedits_after_build($form, &$form_state) {
$form['field_email'][0]['value']['#value'] = $_SERVER['mail'];
$form['field_email'][0]['#value']['value'] = $_SERVER['mail'];
$form['field_name'][0]['value']['#value'] = $_SERVER['fname'] .' '. $_SERVER['lname'];
$form['field_name'][0]['#value']['value'] = $_SERVER['fname'] .' '. $_SERVER['lname'];
$form_state['values']['field_email'][0]['value'] = $_SERVER['mail'];
$form_state['values']['field_name'][0]['value'] = $_SERVER['fname'] .' '. $_SERVER['lname'];
return $form;

Then also our JavaScript file:

// $Id: $
if (Drupal.jsEnabled) {
$(document).ready(function() {
$("#edit-field-name-0-value").attr("disabled", "disabled")

Now, why didn't I just do this in webform, or create the content type via a module? Well, two reasons, one is I need the data to be nodes so an admin can easily work with them and pull them into a news view and perhaps more importantly, I need to create about 1000 nodes from importing news story data. Hours wasted or hours worth the learning? I'm leaning to the former.


another alternative

Here's perhaps a better more Drupal compliant method:

I had taken a look at this

I had taken a look at this and can acknowledge that it is another, and yes possibly better way to approach a solution to the same problem. However, it came down to a judgement call of how confused I thought the next person would be when they looked at my code, and the fact that it has to cycle through all the elements.

I'd like to think so, had I

I'd like to think so, had I not tried every combination of setting #disabled that I could think of. No matter how I tried to set disabled on the a CCK form field it would not actually disable. If you have working code on a CCK field in a custom content type where setting #disabled works, then I'd be happy to change things, however please note that I put a lot of due diligence into trying.

Agreed that the #after_build

Agreed that the #after_build dance to alter CCK fields is unfortunate.

FWIW, we made sure that D7 Fields API is way simpler on this regard. Everything is accessible in hook_form_alter() (or hook_field_attach_form())

And I do love you guys for

And I do love you guys for this fact. I'll be mentioning this tonight in a presentation I am giving on the use of Drupal 7 for

wow, there are really a lot

wow, there are really a lot of universities with an Drupal-CMS on your site.
Did you actually made them, or just collected them on a place?

I'm thinking of trying Drupal, over here in Germany we use Typo3 often, which is to much for me, and more specially Joomla.

best regards,


true dat. I feel your pain.

true dat. I feel your pain.

I have a CCK field with

I have a CCK field with (manually entered) allowed values. On one content type, all field option values need to display. But on another content type, several options (that are no longer available) should not be shown. I was so looking for a option disabled solution. I even tried CSS and pseudo selector; no go. Required a custom module and javascript to hide those few values after page load.

Speaking of which, I should

Speaking of which, I should add the js as well to the post.

Take sure you have a

Take sure you have a validation. Disabled is just a html attribute which can be changed easy with firebug or anything else.
People can make some input .

Your concern is well placed,

Your concern is well placed, but the above method appears to overwrite any values the user adds by setting the value again, at least in my tests, and I tried it a few times using Firebug before posting. I wholeheartedly agree about using validation, and it's pretty simple to setup the a custom validation callback using ['#validate'][], but I ran into problems with the validation being inconsistent, mainly in terms of the values present in the $form and $form_state variables at the time of the validation step. I also didn't need it for my purposes. So the fight continues. If I get a good snippet that works every time I will add it to the post.

the question

Has someone fired an issue to cck module for this? To be able to modify cck simply is a must that drupal should achieve!

Well, I'm not sure it's

Well, I'm not sure it's really an issue for CCK to tackle, though I suppose it would be a feature request to have CCK created fields have the same ability that webform fields have, where one can simply select a checkbox when creating the field, to have it be shown but disabled. The problem discussed here though is more with the Form API.

Some thing different Code....God one

This is really something different code.Actually I learn some thing different code for the API...I will copy this link and paste as my Facebook status....Please keep such kind of the post in the future also.....:)

Add new comment

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.

Comment using an existing account (Google, Twitter, etc.)