Advertisement
Guest User

Untitled

a guest
Sep 27th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. /**
  2. * {@inheritdoc}
  3. */
  4. public function buildForm(array $form, FormStateInterface $form_state) {
  5.  
  6. $form['#tree'] = true; //Make the form fields a hierachical array*
  7.  
  8. $form['#prefix'] = '<div id="sponsorship-form-wrapper">';
  9. $form['#suffix'] = '</div>';
  10.  
  11. $form['sponsor'] = [
  12. '#type' => 'fieldset',
  13. '#title' => $this->t('You (sponsor)'),
  14. ];
  15. $form['sponsor']['civility'] = [
  16. '#type' => 'select',
  17. '#title' => $this->t('Civility'),
  18. '#options' => array('Miss' => $this->t('Miss'), 'Mister' => $this->t('Mister')),
  19. '#size' => 1,
  20. /* .......... */
  21. $form['sponsor']['submit'] = [
  22. '#type' => 'submit',
  23. '#value' => t('Send'),
  24. ];
  25.  
  26.  
  27. $form['sponsee'] = [
  28. '#type' => 'fieldset',
  29. '#title' => $this->t('Your sponsee'),
  30. '#prefix' => '<div id="sponsees-fieldset-wrapper">',
  31. '#suffix' => '</div>',
  32. ];
  33.  
  34. //Default value of nb_sponsees to 1
  35. if ($form_state->getValue('nb_sponsee') == null) {
  36. $form_state->setValue('nb_sponsee', 1);
  37. }
  38.  
  39. for ($i = 0; $i < $form_state->getValue('nb_sponsee'); $i++) {
  40. $form['sponsee'][$i]['civility'] = [
  41. '#type' => 'select',
  42. '#title' => $this->t('Civility'),
  43. '#options' => array('Miss' => $this->t('Miss'), 'Mister' => $this->t('Mister')),
  44. '#size' => 1,
  45. ];
  46. $form['sponsee'][$i]['firstname'] = [
  47. '#type' => 'textfield',
  48. '#title' => $this->t('Firstname'),
  49. '#maxlength' => 64,
  50. '#size' => 64,
  51. ];
  52. $form['sponsee'][$i]['name'] = [
  53. '#type' => 'textfield',
  54. '#title' => $this->t('Name'),
  55. '#maxlength' => 64,
  56. '#size' => 64,
  57. ];
  58. $form['sponsee'][$i]['phone'] = [
  59. '#type' => 'textfield',
  60. '#title' => $this->t('Phone'),
  61. '#maxlength' => 64,
  62. '#size' => 64,
  63. ];
  64. $form['sponsee'][$i]['e_mail'] = [
  65. '#type' => 'email',
  66. '#title' => $this->t('E-mail'),
  67. ];
  68. }
  69.  
  70. //If less of 3 sponsee we add the button
  71. if ($form_state->getValue('nb_sponsee') < 3) {
  72. $form['sponsee']['add_sponsee'] = [
  73. '#type' => 'submit',
  74. '#name' => 'add_sponsee',
  75. '#submit' => [$this, 'ajaxHandleSponseesSubmit'],
  76. '#value' => t('Add a sponsee'),
  77. '#ajax' => array(
  78. 'callback' => [$this, 'ajaxHandleSponseesCallback'],
  79. 'wrapper' => 'sponsees-fieldset-wrapper',
  80. 'event' => 'click',
  81. 'effect' => 'fade',
  82. 'progress' => array('message' => '', 'type' => 'throbber'),
  83. ),
  84. ];
  85. }
  86.  
  87. return $form;
  88. }
  89.  
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function validateForm(array &$form, FormStateInterface $form_state) {
  94. parent::validateForm($form, $form_state);
  95. }
  96.  
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function submitForm(array &$form, FormStateInterface $form_state) {
  101. // Display result.
  102. foreach ($form_state->getValues() as $key => $value) {
  103. drupal_set_message($key . ': ' . $value);
  104. }
  105. }
  106.  
  107. /*
  108. * Custom Submit to increment nb_sponsees
  109. */
  110. function ajaxHandleSponseesSubmit(array &$form, FormStateInterface &$form_state) {
  111. $form_state->setValue('nb_sponsee', $form_state->getValue('nb_sponsee') + 1);
  112. $form_state->setRebuild(true);
  113. }
  114.  
  115. /**
  116. * Ajax submit handler that will return the form structure
  117. */
  118. public function ajaxHandleSponseesCallback(array &$form, FormStateInterface &$form_state) {
  119. return $form['sponsee'];
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement