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

1 Answer 1


You need your own form ideally, right now you have a system_settings_form. That's typically for only setting variables and really simple stuff like that. see the documentation on system_settings_forms.

Instead you probably want to create and use a custom menu callback (you've already done that) which has a callback to a custom form you've defined. See the general drupal FAPI guide.

With your own form you create a submit callback. In that callback you take the selected node types and just create and assign instances of drupal fields to them. So you have to do 2 things: 1) create the fields, 2) assign them to the content types (entity bundles).

The form submit callback could look like this (not 100% tested code, fill in the blanks, add validation, etc):

function mymodule_form_submit($form, &$form_state) {
  if (!field_info_field('field_price')) {
    # Create field singleton
    $field = array('field_name' => 'field_price', 'type' => 'number_decimal',);
    $field = field_create_field($field);
  if (!field_info_field('field_price')) {
    # Create field singleton
    $field2 = array('field_name' => 'field_type_of_currency', 'type' => 'option',);
    $field2 = field_create_field($field2);

  foreach ($form_state['values']['content_type'] as $machine_id => $machine_name) {
    # Create instance on bundle.
    $instance['field_price'] = array(
      'field_name' => $field['field_name'],
      'entity_type' => 'node',
      'bundle' => $machine_id,
      'label' => 'Price',
      'description' => 'The price of an item.',
      'required' => FALSE,
      'settings' => array(),
      'widget' => array(
        'type' => 'textfield',
      'display' => array(
        'default' => array('label' => 'hidden'),
    // ... same instance code for currancy type field instance.
    // $instance['field_currency_type'] = ...;

    foreach ($instance as $instance_field){
  • thanks man ^^ i have removed the $form['submit'] from the my_module_admin_settings_form and instead i have added the return system_settings_form($form); and then following your answer i have created mymodule_form_submit with your code , and then for testing i have added drupal_set_message to see if the module is entering to the hook in the submit or not .. nothing appear or happens to the selected content types Jun 4, 2013 at 23:53
  • i have created an another post for this : (drupal.stackexchange.com/questions/75197/…) Jun 5, 2013 at 0:51
  • please check the new post , coze when i changed the $form['#submit'][] = 'my_module_form_submit'; in the hook mymodule_payment_admin_settings_form i have called the funtion in the submit $form['#submit'][] = 'my_module_form_submit'; then the fields were successfully created(cozwhen i want to re submit the form with the same field name,i get this msg FieldException:Attempt to create field name<em class="placeholder">field_price</em>existing and active.in field_create_field()(ligne 85 inC:\xampp\htdocs\..\modules\field\field.crud.inc),but i can't see the fields in the selected content type Jun 5, 2013 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.