Recently I wanted to programmatically add a checkbox field to a node type, depending on a certain set of circumstances.
To do this I needed to use the field_create_field() function to create the field, and then field_create_instance() to add that field to a content type. I created the following field array:
$field = array( 'field_name' => 'field_premium_content', 'cardinality' => 1, 'entity_type' => 'node', 'type' => 'list_boolean', 'description' => 'Is this content premium content requiring subscription?', 'label' => 'Premium Content', 'widget' => array( 'type' => 'options_onoff', ), );
After I'd ran this array through the two functions the field displayed correctly in a node edit form for any node of that particular node type, however upon saving the node and going back to edit it - no value had been saved. After some time I finally figured out what the problem was, and it is that for a list_boolean field type, there are no default values set, and you have define what the allowed values will be at the creation stage. So I changed my field array to the following:
$field = array( 'field_name' => 'field_premium_content', 'cardinality' => 1, 'entity_type' => 'node', 'entity_type' => 'node', 'type' => 'list_boolean', 'description' => 'Is this content premium content requiring subscription?', 'label' => 'Premium Content', 'widget' => array( 'type' => 'options_onoff', ), 'settings' => array( 'allowed_values' => array( '0' => 'No', '1' => 'Yes', ), ), );
Once I'd done this and re-ran the code to add the field to that content type - the field now displays the 'Yes' value next to the tick box and saving the node now records the value for that field.
- Log in to post comments
Comments
I have enjoyed your post and have read a plethora of posts on this and it seems nobody EVER says where they put their code. hook_form_alter? the install file? Nobody ever seems to say WHERE they used there code. Since Drupal has an onslaught (to my tiny brain) of hooks, may I have the audacity to ask where you used this code? No harshness in tended just trying to be funny and express some frustration. :) I know it can be used from the install file. Can this be used elsewhere? Many thanks!
Hi Paul, you can use it where ever you like. In this instance I wanted to add this new field when a set of conditions were met (in my case when an option in the node create/edit form was selected). I passed that field array to field_create_field($field); which then generated that field, and then I used field_create_instance($instance); to add that field to a specific bundle. Does that make sense?
I'm looking for a quick solution as to how to add a checkbox using Drupal 7 fields per node type. How can I do it?
Hi Jon, are you looking to do this programmatically or using the back end interface?
Hi James, I also have a problem same as Jon, how to add a checkbox using Drupal 7 fields per node programmatically?
I'm not sure if this is what 'jon fitness' was looking for, but the book, Pro Drupal 7 Development, outlines a method for adding a field to specific node types. It provides a complete management system for selecting which nodes would have the new field assigned to them.
To summarize, you'd need to implement hook_install() to create the new field (the book implements it in the module.install file), then use hook_menu() to create a settings page to manage which nodes you want to assign the new field. For example, you'd create a new menu item, such as: $items['admin/config/<your_module_name>/settings']
I hope that helps.