You are here

public function EmailBodyForm::buildForm in Mass Contact 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

File

src/Form/EmailBodyForm.php, line 80

Class

EmailBodyForm
Email body settings form.

Namespace

Drupal\mass_contact\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form = parent::buildForm($form, $form_state);
  $config = $this
    ->config('mass_contact.settings');

  // HTML options.
  $form['html_settings'] = [
    '#type' => 'details',
    '#open' => $this->massContact
      ->htmlSupported(),
    '#title' => $this
      ->t('HTML Settings'),
  ];
  if ($this->massContact
    ->htmlSupported()) {
    $format = $config
      ->get('message_format');
    $options = array_map(function (FilterFormatInterface $filter_format) {
      return $filter_format
        ->label();
    }, filter_formats());
    $form['html_settings']['message_format'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('The default text format'),
      '#default_value' => $format,
      '#description' => $this
        ->t('This is the text format that will be initially selected. If you do not want to allow HTML messages, then specify a plain text text format and do not allow it to be overridden below. Keep in mind that the user sending the message may not have access to all the text formats that are available here.'),
      '#options' => $options,
    ];
  }
  else {
    $form['html_settings']['message_format'] = [
      '#type' => 'hidden',
      '#value' => 'plain_text',
    ];
    $form['html_settings']['no_mimemail'] = [
      '#type' => 'item',
      '#description' => $this
        ->t('To use HTML email for mass contact messages, the <a href="@mime">Mime Mail</a> module or <a href="@swift">Swiftmailer</a> module is required', [
        '@mime' => Url::fromUri('https://www.drupal.org/project/mimemail')
          ->toString(),
        '@swift' => Url::fromUri('https://www.drupal.org/project/swiftmailer')
          ->toString(),
      ]),
    ];
  }

  // Supplemental texts that are prepended and/or appended to every message.
  $form['supplemental_texts'] = [
    '#type' => 'details',
    '#open' => $config
      ->get('message_prefix.value') || $config
      ->get('message_suffix.value'),
    '#title' => $this
      ->t('Supplemental message body texts'),
    '#description' => $this
      ->t('You may specify additional text to insert before and/or after the message text of every mass email that is sent.'),
  ];
  $form['supplemental_texts']['message_prefix'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Text to be prepended to all messages'),
    '#default_value' => $config
      ->get('message_prefix.value'),
    '#format' => $config
      ->get('message_prefix.format'),
    '#description' => $this
      ->t('The text you specify in this field will be added to all Mass Contact messages sent out and will be placed before the message text entered in by the sender.'),
  ];
  $form['supplemental_texts']['message_suffix'] = [
    '#type' => 'text_format',
    '#title' => $this
      ->t('Text to be appended to all messages'),
    '#default_value' => $config
      ->get('message_suffix.value'),
    '#format' => $config
      ->get('message_suffix.format'),
    '#description' => $this
      ->t('The text you specify in this field will be added to all Mass Contact messages sent out and will be placed after the message text entered in by the sender.'),
  ];
  if (!$this->massContact
    ->htmlSupported()) {
    $form['supplemental_texts']['message_prefix']['#allowed_formats'] = [
      'plain_text',
    ];
    $form['supplemental_texts']['message_suffix']['#allowed_formats'] = [
      'plain_text',
    ];
  }
  if ($this->moduleHandler
    ->moduleExists('token')) {
    $form['supplemental_texts']['token_tree'] = [
      '#theme' => 'token_tree_link',
      '#token_types' => [
        'global',
      ],
      '#theme_wrappers' => [
        'form_element',
      ],
    ];
  }

  // Attachment options.
  // @todo Port attachment options.
  // @see https://www.drupal.org/node/2867544
  return $form;
}