Skip to main content
3 of 4
Corrected spelling.
Free Radical
  • 15.1k
  • 9
  • 48
  • 83

How do I programatically add a field to a node?

I'm trying to create custom Drupal 7 module. In the form setting for the module I have checkboxes where the admin can select multiple content types:

enter image description here

After installing the module I need to automatically create two fields (Price of type decimal (4,2), and currency of type text) only in the 'content types' the admin has selected from the list above.

And this is how I set the content type to the checkboxes list in the mymodule_payment_admin_settings_form hook in mymodule.module:

 $form['test_content_types'] = array(
      '#type' => 'checkboxes',
      '#options' => drupal_map_assoc(node_type_get_names()),
      '#default_value' => variable_get('test_content_types'),
      '#required' => TRUE
    );