You are here

public function MailchimpCampaignForm::preview in Mailchimp 2.x

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

Generates a preview of the campaign template content.

File

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

Class

MailchimpCampaignForm
Form controller for the MailchimpCampaign entity edit form.

Namespace

Drupal\mailchimp_campaign\Form

Code

public function preview(array $form, FormStateInterface $form_state) {
  $raw_template_content = $form_state
    ->getValue('content');
  if (empty($raw_template_content)) {
    $this->messenger
      ->addWarning(t('The chosen template has no editable content areas to preview. Update the template from Mailchimp or try a different template.'));
  }
  else {
    $text = '';
    $template_content = $this
      ->parseTemplateContent($raw_template_content);
    $content = mailchimp_campaign_render_template($template_content);
    foreach ($content as $key => $section) {
      $text .= "<h3>{$key}</h3>" . $section;
    }
    $form_state
      ->setValue('mailchimp_campaign_campaign_preview', $text);
  }
  $form_state
    ->setRebuild(TRUE);
}