You are here

public function MailchimpCampaignForm::form in Mailchimp 8

Same name and namespace in other branches
  1. 2.x modules/mailchimp_campaign/src/Form/MailchimpCampaignForm.php \Drupal\mailchimp_campaign\Form\MailchimpCampaignForm::form()

Gets the actual form array to be built.

Overrides ContentEntityForm::form

See also

\Drupal\Core\Entity\EntityForm::processForm()

\Drupal\Core\Entity\EntityForm::afterBuild()

File

modules/mailchimp_campaign/src/Form/MailchimpCampaignForm.php, line 113

Class

MailchimpCampaignForm
Form controller for the MailchimpCampaign entity edit form.

Namespace

Drupal\mailchimp_campaign\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);
  $site_config = $this->config
    ->get('system.site');

  // Attach campaign JS and CSS.
  $form['#attached']['library'][] = 'mailchimp_campaign/campaign-form';

  /* @var \Drupal\mailchimp_campaign\Entity\MailchimpCampaign $campaign */
  $campaign = $this
    ->getEntity();
  $form['title'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Title'),
    '#description' => $this
      ->t('An internal name to use for this campaign. By default, the campaign subject will be used.'),
    '#required' => FALSE,
    '#default_value' => !empty($campaign->mc_data->settings->title) ? $campaign->mc_data->settings->title : '',
  ];
  $form['subject'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Subject'),
    '#required' => TRUE,
    '#default_value' => !empty($campaign->mc_data->settings->subject_line) ? $campaign->mc_data->settings->subject_line : '',
  ];
  $form['preview_text'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Preview text'),
    '#description' => $this
      ->t('Text that shows as preview in e-mail clients, but not in the mail itself.'),
    '#required' => FALSE,
    '#default_value' => !empty($campaign->mc_data->settings->preview_text) ? $campaign->mc_data->settings->preview_text : '',
  ];
  $mailchimp_lists = mailchimp_get_lists();
  $form['list_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Audience'),
    '#description' => $this
      ->t('Select the audience this campaign should be sent to.'),
    '#options' => $this
      ->buildOptionList($mailchimp_lists),
    '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data->recipients->list_id : -1,
    '#required' => TRUE,
    '#ajax' => [
      'callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::listSegmentCallback',
    ],
  ];
  if (!empty($form_state
    ->getValue('list_id'))) {
    $list_id = $form_state
      ->getValue('list_id');
  }
  elseif ($campaign && $campaign->list) {
    $list_id = $campaign->list->id;
    if (isset($campaign->mc_data->recipients->segment_opts->saved_segment_id)) {
      $segment_id = $campaign->mc_data->recipients->segment_opts->saved_segment_id;
    }
  }
  $list_segments = [];
  if (isset($list_id)) {
    $list_segments = mailchimp_campaign_get_list_segments($list_id, NULL);
  }
  $form['list_segment_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Audience Tags'),
    '#description' => $this
      ->t('Select the audience tags this campaign should be sent to.'),
  ];
  if (!empty($list_segments)) {
    $form['list_segment_id']['#options'] = $this
      ->buildOptionList($list_segments, '-- Entire list --');
    $form['list_segment_id']['#default_value'] = isset($segment_id) ? $segment_id : '';
  }
  $form['list_segment_id']['#prefix'] = '<div id="list-segments-wrapper">';
  $form['list_segment_id']['#suffix'] = '</div>';
  $form['from_email'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('From Email'),
    '#description' => $this
      ->t('the From: email address for your campaign message.'),
    '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data->settings->reply_to : $site_config
      ->get('mail'),
    '#size' => 40,
    '#maxlength' => 255,
    '#required' => TRUE,
  ];
  $form['from_name'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('From Name'),
    '#description' => $this
      ->t('the From: name for your campaign message (not an email address)'),
    '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data->settings->from_name : $site_config
      ->get('name'),
    '#size' => 40,
    '#maxlength' => 255,
    '#required' => TRUE,
  ];
  $template_type_labels = [
    'user' => 'My Custom Templates',
    'basic' => 'Basic Templates',
    'gallery' => 'Themes',
  ];
  $form['template_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Template'),
    '#description' => $this
      ->t('Select a Mailchimp user template to use. Due to a limitation in the API, only templates that do not contain repeating sections are available. If empty, the default template will be applied.'),
    '#options' => $this
      ->buildOptionList(mailchimp_campaign_list_templates(), '-- Select --', $template_type_labels),
    '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data->settings->template_id : -1,
    '#ajax' => [
      'callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::templateCallback',
    ],
  ];
  $form['content'] = [
    '#id' => 'content-sections',
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Content sections'),
    '#description' => $this
      ->t('The HTML content or, if a template is selected, the content for each section.'),
    '#tree' => TRUE,
  ];
  $mc_template = NULL;
  if (!empty($form_state
    ->getValue('template_id'))) {
    $mc_template = mailchimp_campaign_get_template($form_state
      ->getValue('template_id'));
  }
  else {
    if ($campaign && $campaign->mc_template) {
      $mc_template = $campaign->mc_template;
    }
  }
  if (isset($list_id)) {
    $merge_vars_list = mailchimp_get_mergevars([
      $list_id,
    ]);
    $merge_vars = $merge_vars_list[$list_id];
  }
  else {
    $merge_vars = [];
  }
  $campaign_template = $campaign
    ->getTemplate();
  $campaign_content = $form_state
    ->getValue('content');
  $entity_type = NULL;
  if ($mc_template) {
    foreach ($mc_template->info->sections as $section => $content) {
      if (substr($section, 0, 6) == 'repeat') {
        $this->messenger
          ->addWarning($this
          ->t('WARNING: This template has repeating sections, which are not supported. You may want to select a different template.'));
      }
    }
    foreach ($mc_template->info->sections as $section => $content) {

      // Set the default value and text format to either saved campaign values
      // or defaults coming from the Mailchimp template.
      $default_value = $content;
      $format = 'mailchimp_campaign';
      if ($campaign_template != NULL && isset($campaign_template[$section])) {
        $default_value = $campaign_template[$section]['value'];
        $format = $campaign_template[$section]['format'];
      }
      $form['content'][$section . '_wrapper'] = [
        '#type' => 'details',
        '#title' => Html::escape(ucfirst($section)),
        '#open' => FALSE,
      ];
      $form['content'][$section . '_wrapper'][$section] = [
        '#type' => 'text_format',
        '#format' => $format,
        '#title' => Html::escape(ucfirst($section)),
        '#default_value' => $default_value,
      ];
      if (isset($campaign_content[$section . '_wrapper']['entity_import']['entity_type'])) {
        $entity_type = $campaign_content[$section . '_wrapper']['entity_import']['entity_type'];
      }
      $form['content'][$section . '_wrapper'] += $this
        ->getEntityImportFormElements($entity_type, $section);
      if (!empty($list_id)) {
        $form['content'][$section . '_wrapper'] += $this
          ->getMergeVarsFormElements($merge_vars, $mailchimp_lists[$list_id]->name);
      }
    }
  }
  else {
    $section = 'html';
    $form['content']['html_wrapper'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('Content'),
      '#open' => FALSE,
    ];
    $form['content']['html_wrapper']['html'] = [
      '#type' => 'text_format',
      '#format' => $campaign_template != NULL ? $campaign_template['html']['format'] : 'mailchimp_campaign',
      '#title' => $this
        ->t('Content'),
      '#description' => $this
        ->t('The HTML content of the campaign.'),
      '#access' => empty($form_state
        ->getValue('template_id')),
      '#default_value' => $campaign_template != NULL ? $campaign_template['html']['value'] : '',
    ];
    if (isset($campaign_content[$section . '_wrapper']['entity_import']['entity_type'])) {
      $entity_type = $campaign_content[$section . '_wrapper']['entity_import']['entity_type'];
    }
    $form['content'][$section . '_wrapper'] += $this
      ->getEntityImportFormElements($entity_type, $section);
    $list_name = !empty($list_id) ? $mailchimp_lists[$list_id]->name : '';
    $form['content'][$section . '_wrapper'] += $this
      ->getMergeVarsFormElements($merge_vars, $list_name);
  }

  // Message preview:
  if (!empty($form_state
    ->getValue('mailchimp_campaign_campaign_preview'))) {
    $form['preview_wrapper'] = [
      '#title' => $this
        ->t('Campaign content preview'),
      '#type' => 'details',
      '#open' => TRUE,
    ];
    $form['preview_wrapper']['preview'] = [
      '#markup' => $form_state
        ->getValue('mailchimp_campaign_campaign_preview'),
    ];
  }
  return $form;
}