You are here

public function MailchimpCampaignForm::form 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::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',
    'base' => 'Basic Templates',
    'gallery' => 'Themes',
  ];
  $is_new = empty($campaign
    ->getMcCampaignId());
  $no_selection_label = NULL;
  if ($is_new || $form_state
    ->getValue('template_id') === '') {
    $no_selection_label = 'No Template';
  }
  $form['template_id'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Template'),
    '#description' => '<p>' . $this
      ->t('Select an email template to use.') . '</p><p>' . $this
      ->t('Only a new campaign can be created without a template. A template must use custom HTML, have editable content areas, and not repeat.') . '</p>',
    '#options' => $this
      ->buildOptionList(mailchimp_campaign_list_templates(), $no_selection_label, $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['template_refresh'] = [
    '#value' => $this
      ->t('Refresh current template and template list from Mailchimp'),
    '#type' => 'submit',
    '#submit' => [
      '::submitForm',
      '::refreshTemplates',
    ],
    '#attributes' => [
      'class' => [
        'button',
        'button-action',
        'button--small',
      ],
    ],
  ];
  $form['content'] = [
    '#id' => 'content-sections',
    '#type' => 'fieldset',
    '#title' => $this
      ->t('Content sections'),
    '#description' => $this
      ->t('The editable content areas from your template or a generic HTML container if no template is selected.'),
    '#tree' => TRUE,
  ];
  $selected_template = $form_state
    ->getValue('template_id');
  $mc_template = NULL;
  if (empty($selected_template)) {
    if ($selected_template !== '') {
      $mc_template = $campaign->mc_template;
    }
  }
  else {
    $mc_template = mailchimp_campaign_get_template($selected_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) {
    $template_name = $mc_template->name;
    if ($mc_template->drag_and_drop) {
      $form['content'] = [
        '#id' => 'content-sections',
        '#type' => 'fieldset',
        '#title' => $this
          ->t('The \':template_name\' template is drag and drop', [
          ':template_name' => $template_name,
        ]),
        '#description' => $this
          ->t('Only Custom HTML Templates can provide editable content areas.<p><strong>Related Mailchimp documentation:</strong><br><a href=":custom_link" target="_blank">Custom HTML Templates</a><br><a href=":editable_link" target="_blank">Editable Content Areas</a></p>', [
          ':custom_link' => 'https://mailchimp.com/help/import-a-custom-html-template/',
          ':editable_link' => 'https://mailchimp.com/help/create-editable-content-areas-with-mailchimps-template-language/',
        ]),
        '#tree' => TRUE,
      ];
    }
    elseif (empty((array) $mc_template->info->sections)) {
      $form['content'] = [
        '#id' => 'content-sections',
        '#type' => 'fieldset',
        '#title' => $this
          ->t('The \':template_name\' template has no editable content areas', [
          ':template_name' => $template_name,
        ]),
        '#description' => $this
          ->t('If you add one through Mailchimp, use the Refresh button above. <p><a href=":link" target="_blank">Mailchimp documentation on Editable Content Areas</a>.</p>', [
          ':link' => 'https://mailchimp.com/help/create-editable-content-areas-with-mailchimps-template-language/',
        ]),
        '#tree' => TRUE,
      ];
    }
    else {
      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;
}