Custom submit handler entityform

Public

Adding default fields when creating new entityform

</> CopyGet raw version
php
  1. <?php
  2.  
  3. /**
  4.  * Implement hook_form_alter().
  5.  */
  6. function example_form_alter(&$form, &$form_state, $form_id) {
  7. if($form_id == 'entityform_type_form') {
  8. $form['#submit'][] = 'add_new_fields';
  9. }
  10. }
  11.  
  12. /**
  13.  * Submit callback adding custom field when saved entityform type.
  14.  */
  15. function add_new_fields($form, &$form_state) {
  16. // Create email address field.
  17. if(!field_info_field('field_email_address')) {
  18. // Create your new field.
  19. $field = array(
  20. 'field_name' => 'field_email_address',
  21. 'type' => 'text',
  22. );
  23.  
  24. field_create_field($field);
  25.  
  26. // Create field instance.
  27. $instance = array(
  28. 'field_name' => 'field_email_address',
  29. 'entity_type' => 'entityform',
  30. 'bundle' => $form_state['values']['type'],
  31. 'label' => 'Email address',
  32. 'settings' => array(
  33. 'entityform_type_form' => 1,
  34. ),
  35. 'widget' => array(
  36. 'type' => 'textfield',
  37. ),
  38. );
  39.  
  40. field_create_instance($instance);
  41. }
  42. // Create voucher code field.
  43. if(!field_info_field('field_voucher_code')) {
  44. // Create your new field.
  45. $field = array(
  46. 'field_name' => 'field_voucher_code',
  47. 'type' => 'text',
  48. );
  49.  
  50. field_create_field($field);
  51.  
  52. // Create field instance.
  53. $instance = array(
  54. 'field_name' => 'field_voucher_code',
  55. 'entity_type' => 'entityform',
  56. 'bundle' => $form_state['values']['type'],
  57. 'label' => 'Voucher code',
  58. 'settings' => array(
  59. 'entityform_type_form' => 1,
  60. ),
  61. 'widget' => array(
  62. 'type' => 'textfield',
  63. ),
  64. );
  65.  
  66. field_create_instance($instance);
  67. }
  68. }