You are here

function views_send_confirm_form in Views Send 8

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

Implements the form for the "confirm" step.

Allows the user to preview the whole message before sending it.

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

File

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

Code

function views_send_confirm_form(&$form, &$form_state, $view) {

  // TODO: Set title as #markup in stead.
  $form['#title'] = Html::escape(t('Review and confirm the message that is about to be sent'));

  // Values entered in the "config" step.
  $configuration = $form_state
    ->get('configuration');
  $selection = $form_state
    ->get('selection');
  if (!VIEWS_SEND_MIMEMAIL && $configuration['views_send_message']['format'] != 'plain_text') {
    \Drupal::messenger()
      ->addMessage(t("Only plain text is supported in the message. Any HTML will be converted to text. If you want to format the message with HTML, you'll have to install and enable the <a href=':url'>Mime Mail</a> module.", array(
      ':url' => 'http://drupal.org/project/mimemail',
    )));
  }

  // From: parts.
  $from_mail = trim($configuration['views_send_from_mail']);
  $from_name = trim($configuration['views_send_from_name']);
  $form['#attributes']['class'] = array(
    'views-send-preview',
  );
  $form['from'] = array(
    '#type' => 'item',
    '#title' => t('From'),
    '#markup' => '<div class="views-send-preview-value">' . Html::escape(_views_send_format_address($from_mail, $from_name, FALSE)) . '</div>',
  );
  $recipients = array();
  $to_name_field = $configuration['views_send_tokens'][$configuration['views_send_to_name']];
  $to_mail_field = $configuration['views_send_tokens'][$configuration['views_send_to_mail']];
  foreach ($selection as $row_id) {
    $to_name = _views_send_strip_html($view->style_plugin
      ->getField($row_id, $to_name_field));
    $to_mail = _views_send_strip_html($view->style_plugin
      ->getField($row_id, $to_mail_field));
    $to_mail_arr = explode(',', $to_mail);
    foreach ($to_mail_arr as $to_mail) {
      $recipients[] = Html::escape(_views_send_format_address($to_mail, $to_name, FALSE));
    }
  }
  $form['to'] = array(
    '#type' => 'item',
    '#title' => t('To'),
    '#markup' => '<div id="views-send-preview-to" class="views-send-preview-value">' . implode(', ', $recipients) . '</div>',
  );
  $form['subject'] = array(
    '#type' => 'item',
    '#title' => t('Subject'),
    '#markup' => '<div class="views-send-preview-value">' . Html::escape($configuration['views_send_subject']) . '</div>',
  );
  $form['message'] = array(
    '#type' => 'item',
    '#title' => t('Message'),
    '#markup' => '<div id="views-send-preview-message" class="views-send-preview-value">' . check_markup($configuration['views_send_message']['value'], $configuration['views_send_message']['format']) . '</div>',
  );
  $headers = array();
  foreach (_views_send_headers($configuration['views_send_receipt'], $configuration['views_send_priority'], $configuration['views_send_from_mail'], $configuration['views_send_headers']) as $key => $value) {
    $headers[] = Html::escape($key . ': ' . $value);
  }
  $form['headers'] = array(
    '#type' => 'item',
    '#title' => t('Headers'),
    '#markup' => '<div id="views-send-preview-headers" class="views-send-preview-value">' . implode('<br />', $headers) . '</div>',
  );
  if (VIEWS_SEND_MIMEMAIL && !empty($configuration['views_send_attachments']) && Drupal::currentUser()
    ->hasPermission('attachments with views_send')) {
    foreach ($configuration['views_send_attachments'] as $attachment) {
      $attachments[] = Html::escape($attachment
        ->getFilename());
    }
    $form['attachments'] = array(
      '#type' => 'item',
      '#title' => t('Attachments'),
      '#markup' => '<div id="views-send-preview-attachments" class="views-send-preview-value">' . implode('<br />', $attachments) . '</div>',
    );
  }
  $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('Send', [], [
      'context' => 'views_send: Send mail',
    ]),
    '#suffix' => Link::fromTextAndUrl(t('Cancel'), $url)
      ->toString(),
  );
  return $form;
}