Creating A Dynamic Required CCK Field in Drupal

Sometimes you need to make a CCK field required only when certain conditions are met (e.g., a particular user or role). In these cases, simply marking a field 'Required' in the field's configuration page is just not enough. Enter the #after_build FAPI attribute.

With the #after_build attribute you can modify your CCK fields to your hearts content. Although in this example I am only going to cover making the field required even though it is not set as such in the field's configuration page.

This method requires the following:

I'm assuming you already know how or have created a custom module. If you haven't please read up on it.

First you must add the #after_build function to your node form using hook_form_alter:

<?php
/**
 * Implementation of hook_form_alter
 *
 * Modify the registration form for use during registration process
 *
 * @param array $form
 * @param array $form_state
 */
function example_form_alter(&$form, &$form_state){
   
// Add an after_build function to process when everything's complete.
   
$form['#after_build'][] = 'example_after_build';
}
?>

Now, here's where the altering of the CCK fields come in to play. You see, when the form is available in hook_form_alter(), not all of the CCK fields have been fully constructed therefore, they are not available to be altered. If you have tried modifying the CCK fields in hook_form_alter() you may been greeted by errors or nothing at all.

example_after_build()

<?php
/**
 * Modify CCK form elements on the example node form.
 */
function example_after_build($form, &$form_state) {
   
// Value CCK field
   
$form['field_value'][0]['value']['#required'] = TRUE;

    // Node reference CCK field
   
$form['field_nid']['nid']['nid']['#required'] = TRUE;

    // User regerence CCK field
   
$form['field_user']['uid']['uid']['#required'] = TRUE;
}
?>

That's it! Now you can set your required fields dynamically!

Categories: 

5 comments

by Jeffrey (not verified) on Wed, 07/20/2011 - 01:38

Thanks, now I understand this, but no way that I would have come to that, by myself =)
I think programming is a mindset, some have it, others not.

best regards,
Jeffrey
http://www.icancreative.de

by Anonymous (not verified) on Thu, 12/01/2011 - 08:27

You need to return the form

by Allen (not verified) on Fri, 06/22/2012 - 08:21

The function example_after_build() will need to return the $form value, like so:

return $form;

Otherwise you're likely to get a white-screen (WSOD, White Screen of Death).

Reference: http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc... or http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc...

by Derek (not verified) on Fri, 07/27/2012 - 05:21

Which module are you using to display the code on this page?

by gbrands on Fri, 07/27/2012 - 07:04

Hey Derek!

The module I'm using is Code Filter: http://drupal.org/project/codefilter

Creating A Dynamic Required CCK Field in Drupal | Gerrit Brands

Error message

  • Warning: Cannot modify header information - headers already sent by (output started at /homepages/29/d196880538/htdocs/drupal7/includes/common.inc:2681) in drupal_send_headers() (line 1212 of /homepages/29/d196880538/htdocs/drupal7/includes/bootstrap.inc).
  • PDOException: SQLSTATE[42000]: Syntax error or access violation: 1142 INSERT command denied to user 'dbo354826752'@'74.208.16.6' for table 'watchdog': INSERT INTO {watchdog} (uid, type, message, variables, severity, link, location, referer, hostname, timestamp) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7, :db_insert_placeholder_8, :db_insert_placeholder_9); Array ( [:db_insert_placeholder_0] => 0 [:db_insert_placeholder_1] => cron [:db_insert_placeholder_2] => Attempting to re-run cron while it is already running. [:db_insert_placeholder_3] => a:0:{} [:db_insert_placeholder_4] => 4 [:db_insert_placeholder_5] => [:db_insert_placeholder_6] => http://gerritbrands.com/blog/creating-dynamic-required-cck-field-drupal [:db_insert_placeholder_7] => [:db_insert_placeholder_8] => 54.156.92.46 [:db_insert_placeholder_9] => 1503039484 ) in dblog_watchdog() (line 154 of /homepages/29/d196880538/htdocs/drupal7/modules/dblog/dblog.module).

Error

The website encountered an unexpected error. Please try again later.