Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2016
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. namespace Drupal\bulk_email\Form;
  3. use Drupal\Core\Form\FormBase;
  4. use Drupal\Core\Form\FormStateInterface;
  5. use Drupal\Component\Utility\UrlHelper;
  6.  
  7. class EmailForm extends FormBase {
  8.   public function getFormId() {
  9.     return "bulk_email_form";
  10.   }
  11.  
  12.   public function buildForm(array $form, FormStateInterface $form_state) {
  13.     $form['#tree'] = TRUE;
  14.  
  15.     $form['users'] = array(
  16.       '#type' => 'fieldset',
  17.       '#title' => t('Users'),
  18.       '#prefix' => '<div id="users-wrapper">',
  19.       '#suffix' => '</div>',
  20.     );
  21.     $form['temptest'] = array(
  22.       '#type' => 'textBox
  23.    
  24.    $form['num_users'] = array(
  25.      '#type' => 'textfield',
  26.      '#title' => 'test',
  27.       '#description' => 'enter anything to add a user',
  28.     );
  29.  
  30.     $num_users = $form_state->getValue('num_users');
  31.     if (!empty($form_state->getValue("temptest"))) {
  32.       $num_users++;
  33.       $form_state->setValue('num_users', $num_users);
  34.       $form_state->setRebuild(TRUE);
  35.     }
  36.     if (empty($num_users)) {
  37.       $num_users = 1;
  38.     }
  39.     for ($i = 0; $i < $num_users; $i++) {
  40.       $form['users'][$i] = array(
  41.         '#type'  => 'textfield',
  42.         '#description' => t('Select a user.'),
  43.       );
  44.     }
  45.  
  46.     $form['users']['users_more'] = array(
  47.         '#type' => 'submit',
  48.         '#value' => t('Add one'),
  49.       '#submit' => array($this, 'bulk_email_addfieldsubmit'),
  50.         '#ajax' => array(
  51.                'callback' => array($this, 'bulk_email_add_more_callback'),
  52.            'wrapper' => 'users-wrapper',
  53.       ),
  54.      );
  55.  
  56.    $form['actions'] = array('#type' => 'actions');
  57.     $form['submit'] = array(
  58.       '#type' => 'submit',
  59.       '#value' => t('Send'),
  60.     );
  61.     return $form;
  62.   }
  63.  
  64.   public function submitForm(array &$form, FormStateInterface $form_state) {
  65.     foreach ($form_state->getValues() as $key => $value) {
  66.       drupal_set_message($key . ': ' . $value);
  67.     }
  68.   }
  69.  
  70.   public function bulk_email_addfieldsubmit(array &$form, FormStateInterface $form_state) {
  71.     $num_users = $form_state->getValue('num_users') + 1;
  72.     $form_state->setValue('num_users', $num_users);
  73.     $form_state->setRebuild(TRUE);
  74.   }
  75.  
  76.   public function bulk_email_add_more_callback(array &$form, FormStateInterface $form_state) {
  77.     return $form['users'];
  78.   }
  79. }
  80. ?>
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement