You are here

function views_send_config_form in Views Send 8

Same name and namespace in other branches
  1. 7 views_send.module \views_send_config_form()

Implements the form for the "configure" step.

1 string reference to 'views_send_config_form'
ViewsSend::viewsFormSubmit in src/Plugin/views/field/ViewsSend.php
Overrides \Drupal\system\Plugin\views\field\BulkForm::viewsFormSubmit().

File

./views_send.module, line 80
The Views Send module.

Code

function views_send_config_form(&$form, &$form_state, $view) {
  $display = $view->storage
    ->id() . ':' . $view->current_display;
  $form['display'] = array(
    '#type' => 'value',
    '#value' => $display,
  );
  $form['from'] = array(
    '#type' => 'details',
    '#title' => t('Sender'),
    '#open' => TRUE,
  );
  $config_basekey = $display . '.uid:' . \Drupal::currentUser()
    ->id();
  $site_config = \Drupal::config('system.site');
  $config = \Drupal::config('views_send.user_settings')
    ->get($config_basekey);
  $form['from']['views_send_from_name'] = array(
    '#type' => 'textfield',
    '#title' => t('Sender\'s name'),
    '#description' => t("Enter the sender's human readable name."),
    '#default_value' => isset($config['from_name']) ? $config['from_name'] : $site_config
      ->get('name'),
    '#maxlen' => 255,
  );
  $form['from']['views_send_from_mail'] = array(
    '#type' => 'textfield',
    '#title' => t('Sender\'s e-mail'),
    '#description' => t("Enter the sender's e-mail address."),
    '#required' => TRUE,
    '#default_value' => isset($config['from_mail']) ? $config['from_mail'] : $site_config
      ->get('mail'),
    '#maxlen' => 255,
  );
  $fields = _views_send_get_fields_and_tokens($view, 'fields');
  $tokens = _views_send_get_fields_and_tokens($view, 'tokens');
  $fields_name_text = _views_send_get_fields_and_tokens($view, 'fields_name_text');
  $fields_options = array_merge(array(
    '' => '<' . t('select') . '>',
  ), $fields);
  $form['views_send_tokens'] = array(
    '#type' => 'value',
    '#value' => $tokens,
  );
  $form['to'] = array(
    '#type' => 'details',
    '#title' => t('Recipients'),
    '#open' => TRUE,
  );
  $form['to']['views_send_to_name'] = array(
    '#type' => 'select',
    '#title' => t('Field used for recipient\'s name'),
    '#description' => t('Select which field from the current view will be used as recipient\'s name.'),
    '#options' => $fields_options,
    '#default_value' => isset($config['to_name']) ? $config['to_name'] : '',
  );
  $form['to']['views_send_to_mail'] = array(
    '#type' => 'select',
    '#title' => t('Field used for recipient\'s e-mail'),
    '#description' => t('Select which field from the current view will be used as recipient\'s e-mail.'),
    '#options' => $fields_options,
    '#default_value' => isset($config['to_mail']) ? $config['to_mail'] : '',
    '#required' => TRUE,
  );
  $form['mail'] = array(
    '#type' => 'details',
    '#title' => t('E-mail content'),
    '#open' => TRUE,
  );
  $form['mail']['views_send_subject'] = array(
    '#type' => 'textfield',
    '#title' => t('Subject'),
    '#description' => t('Enter the e-mail\'s subject. You can use tokens in the subject.'),
    '#maxlen' => 255,
    '#required' => TRUE,
    '#default_value' => isset($config['subject']) ? $config['subject'] : '',
  );
  $saved_message = isset($config['message']) ? $config['message'] : array();
  $form['mail']['views_send_message'] = array(
    '#type' => 'text_format',
    '#format' => isset($saved_message['format']) ? $saved_message['format'] : 'plain_text',
    '#title' => t('Message'),
    '#description' => t('Enter the body of the message. You can use tokens in the message.'),
    '#required' => TRUE,
    '#rows' => 10,
    '#default_value' => isset($saved_message['value']) ? $saved_message['value'] : '',
  );
  $form['mail']['token'] = array(
    '#type' => 'details',
    '#title' => t('Replacements'),
    '#description' => t('You can use the following tokens in the subject or message.'),
  );
  if (!\Drupal::moduleHandler()
    ->moduleExists('token')) {
    $form['mail']['token']['tokens'] = array(
      '#markup' => views_send_token_help($fields_name_text),
    );
  }
  else {
    $form['mail']['token']['views_send'] = array(
      '#type' => 'details',
      '#title' => t('Views Send specific tokens'),
    );
    $form['mail']['token']['views_send']['tokens'] = array(
      '#markup' => views_send_token_help($fields_name_text),
    );
    $form['mail']['token']['general'] = array(
      '#type' => 'details',
      '#title' => t('General tokens'),
    );
    $token_types = array(
      'site',
      'user',
      'node',
      'current-date',
    );
    $form['mail']['token']['general']['tokens'] = array(
      '#theme' => 'token_tree_link',
      '#token_types' => $token_types,
    );
  }
  if (VIEWS_SEND_MIMEMAIL && Drupal::currentUser()
    ->hasPermission('attachments with views_send')) {

    // set the form encoding type
    $form['#attributes']['enctype'] = "multipart/form-data";

    // add a file upload file
    $form['mail']['views_send_attachments'] = array(
      '#type' => 'file',
      '#title' => t('Attachment'),
      '#description' => t('NB! The attached file is stored once per recipient in the database if you aren\'t sending the message directly.'),
    );
  }
  $form['additional'] = array(
    '#type' => 'details',
    '#title' => t('Additional e-mail options'),
  );
  $form['additional']['views_send_priority'] = array(
    '#type' => 'select',
    '#title' => t('Priority'),
    '#options' => array(
      VIEWS_SEND_PRIORITY_NONE => t('none'),
      VIEWS_SEND_PRIORITY_HIGHEST => t('highest'),
      VIEWS_SEND_PRIORITY_HIGH => t('high'),
      VIEWS_SEND_PRIORITY_NORMAL => t('normal'),
      VIEWS_SEND_PRIORITY_LOW => t('low'),
      VIEWS_SEND_PRIORITY_LOWEST => t('lowest'),
    ),
    '#description' => t('Note that e-mail priority is ignored by a lot of e-mail programs.'),
    '#default_value' => isset($config['priority']) ? $config['priority'] : 0,
  );
  $form['additional']['views_send_receipt'] = array(
    '#type' => 'checkbox',
    '#title' => t('Request receipt'),
    '#default_value' => isset($config['receipt']) ? $config['receipt'] : 0,
    '#description' => t('Request a Read Receipt from your e-mails. A lot of e-mail programs ignore these so it is not a definitive indication of how many people have read your message.'),
  );
  $form['additional']['views_send_headers'] = array(
    '#type' => 'textarea',
    '#title' => t('Additional headers'),
    '#description' => t("Additional headers to be send with the message. You'll have to enter one per line. Example:<pre>Reply-To: noreply@example.com\nX-MyCustomHeader: Whatever</pre>"),
    '#rows' => 4,
    '#default_value' => isset($config['headers']) ? $config['headers'] : '',
  );
  $form['views_send_direct'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send the message directly using the Batch API.'),
    '#default_value' => isset($config['direct']) ? $config['direct'] : TRUE,
  );
  $form['views_send_carbon_copy'] = array(
    '#type' => 'checkbox',
    '#title' => t('Send a copy of the message to the sender.'),
    '#default_value' => isset($config['carbon_copy']) ? $config['carbon_copy'] : TRUE,
  );
  $form['views_send_remember'] = array(
    '#type' => 'checkbox',
    '#title' => t('Remember these values for the next time a mass mail is sent.'),
    '#default_value' => isset($config['remember']) ? $config['remember'] : FALSE,
  );
  $query = UrlHelper::filterQueryParameters($_GET, array(
    'q',
  ));
  $url = Url::fromRoute('<current>')
    ->setOption('query', $query);
  $form['actions'] = array(
    '#type' => 'container',
    '#attributes' => array(
      'class' => array(
        'form-actions',
      ),
    ),
    '#weight' => 999,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Next', [], [
      'context' => 'views_send: Go to preview',
    ]),
    '#validate' => array(
      'views_send_config_form_validate',
    ),
    '#suffix' => Link::fromTextAndUrl(t('Cancel'), $url)
      ->toString(),
  );
  return $form;
}