1

Is that possible that I set default fields for entityform types when it's created? There will be site admin users. They will create registration forms for events, but there are 2 mandatory fields that they forget about sometimes, but they have to add it, because it breaks a few modules. Like voucher code and the email fields. Also, they sometimes recreate a field with different names. Like they add a new email field, with the name "field_email" address, but the field I'm checking is the "field_email_address". I want to add these fields to the form programmatically, right after the site admin clicks the "Save entityform type" button, but only when he creates it first. Is it possible to add a field programmatically to an entityform? And if it is, then how? Thanks in advance!

3
+50

Yes it's possible here's the steps on how to achieve that functionality.

1. First you need to alter the entityform and add your custom submit handler

Example:

/**
 * Implement hook_form_alter().
 */
function example_form_alter(&$form, &$form_state, $form_id) {
  if($form_id == 'entityform_type_form') {
    $form['#submit'][] = 'add_new_fields';
  }
}

2. Create your custom submit handler, you need field_create_field(), field_create_instance() in creating your field, define all the details of your field inside the $field and $instance array

/**
 * Submit callback adding custom field when saved entityform type.
 */
function add_new_fields($form, &$form_state) {
  // Create email address field.
  if(!field_info_field('field_email_address')) {
    // Create your new field.
    $field = array(
      'field_name' => 'field_email_address',
      'type' => 'text',
    );

    field_create_field($field);

    // Create field instance.
    $instance = array(
      'field_name' => 'field_email_address',
      'entity_type' => 'entityform',
      'bundle' => $form_state['values']['type'],
      'label' => 'Email address',
      'settings' => array(
        'entityform_type_form' => 1,
      ),
      'widget' => array(
        'type' => 'textfield',
      ),
    );

    field_create_instance($instance);
  }
   // Create voucher code field.
  if(!field_info_field('field_voucher_code')) {
    // Create your new field.
    $field = array(
      'field_name' => 'field_voucher_code',
      'type' => 'text',
    );

    field_create_field($field);

    // Create field instance.
    $instance = array(
      'field_name' => 'field_voucher_code',
      'entity_type' => 'entityform',
      'bundle' => $form_state['values']['type'],
      'label' => 'Voucher code',
      'settings' => array(
        'entityform_type_form' => 1,
      ),
      'widget' => array(
        'type' => 'textfield',
      ),
    );

    field_create_instance($instance);
  }
}

Here's the link of working custom module that i created https://github.com/umandalroald/entityform-custom-callback

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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