/**
  * {@inheritdoc}
  */
 public function view(EntityInterface $entity, $view_mode = 'full', $langcode = NULL)
 {
     $build = parent::view($entity, $view_mode, $langcode);
     // Attach campaign JS and CSS.
     $build['#attached']['library'][] = 'mailchimp_campaign/campaign-view';
     // Prepare rendered content.
     /* @var $entity \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
     $content = $this->renderTemplate($entity->getTemplate());
     $rendered = '';
     foreach ($content as $key => $section) {
         $rendered .= "<h3>{$key}</h3>" . $section;
     }
     // Get the template name.
     $mc_template = mailchimp_campaign_get_template($entity->mc_data['template_id']);
     $mc_template_name = isset($mc_template) ? $mc_template['name'] : '';
     $list_segment_name = 'N/A';
     $list_segments = mailchimp_campaign_get_list_segments($entity->list['id'], 'saved');
     if (isset($entity->mc_data['saved_segment']['id'])) {
         foreach ($list_segments as $list_segment) {
             if ($list_segment['id'] == $entity->mc_data['saved_segment']['id']) {
                 $list_segment_name = $list_segment['name'];
             }
         }
     }
     $list_url = Url::fromUri('https://admin.mailchimp.com/lists/dashboard/overview?id=' . $entity->list['web_id'], array('attributes' => array('target' => '_blank')));
     $archive_url = Url::fromUri($entity->mc_data['archive_url']);
     $fields = array('subject' => array('label' => t('Subject'), 'value' => $entity->mc_data['subject']), 'list' => array('label' => t('MailChimp List'), 'value' => \Drupal::l($entity->list['name'], $list_url)), 'list_segment' => array('label' => t('List Segment'), 'value' => $list_segment_name), 'from_email' => array('label' => t('From Email'), 'value' => $entity->mc_data['from_email']), 'from_name' => array('label' => t('From Name'), 'value' => $entity->mc_data['from_name']), 'template' => array('label' => t('Template'), 'value' => $mc_template_name), 'type' => array('label' => t('List type'), 'value' => $entity->mc_data['type']), 'status' => array('label' => t('Status'), 'value' => $entity->mc_data['status']), 'emails_sent' => array('label' => t('Emails sent'), 'value' => $entity->mc_data['emails_sent']), 'send_time' => array('label' => t('Send time'), 'value' => $entity->mc_data['send_time']), 'content' => array('label' => t('Rendered template HTML (!archive)', array('!archive' => \Drupal::l('View MailChimp archive', $archive_url, array('attributes' => array('target' => '_blank'))))), 'value' => $rendered));
     foreach ($fields as $key => $field) {
         $build[$key] = array('#prefix' => "<div class=\"field campaign-{$key}\"><h3 class=\"field-label\">{$field['label']}</h3>", '#markup' => "<p>{$field['value']}</p>", '#suffix' => '</div>');
     }
     return $build;
 }
 /**
  * {@inheritdoc}
  */
 public function form(array $form, FormStateInterface $form_state)
 {
     $form = parent::form($form, $form_state);
     $site_config = \Drupal::config('system.site');
     // Attach campaign JS and CSS.
     $form['#attached']['library'][] = 'mailchimp_campaign/campaign-form';
     /* @var $campaign \Drupal\mailchimp_campaign\Entity\MailchimpCampaign */
     $campaign = $this->entity;
     $form_state->set('campaign', $campaign);
     $form['title'] = array('#type' => 'textfield', '#title' => t('Title'), '#description' => t('An internal name to use for this campaign. By default, the campaign subject will be used.'), '#required' => FALSE, '#default_value' => $campaign ? $campaign->mc_data['title'] : '');
     $form['subject'] = array('#type' => 'textfield', '#title' => t('Subject'), '#required' => TRUE, '#default_value' => $campaign ? $campaign->mc_data['subject'] : '');
     $mailchimp_lists = mailchimp_get_lists();
     $form['list_id'] = array('#type' => 'select', '#title' => t('List'), '#description' => t('Select the list this campaign should be sent to.'), '#options' => $this->buildOptionList($mailchimp_lists), '#default_value' => $campaign ? $campaign->mc_data['list_id'] : -1, '#required' => TRUE, '#ajax' => array('callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::listSegmentCallback'));
     if (!empty($form_state->getValue('list_id'))) {
         $list_id = $form_state->getValue('list_id');
     } elseif ($campaign && $campaign->mc_data) {
         $list_id = $campaign->mc_data['list_id'];
         if (isset($campaign->mc_data['saved_segment']['id'])) {
             $segment_id = $campaign->mc_data['saved_segment']['id'];
         }
     }
     $list_segments = array();
     if (isset($list_id)) {
         $list_segments = mailchimp_campaign_get_list_segments($list_id, 'saved');
     }
     if (!empty($list_segments)) {
         $form['list_segment_id'] = array('#type' => 'select', '#title' => t('List Segment'), '#description' => t('Select the list segment this campaign should be sent to.'), '#options' => $this->buildOptionList($list_segments, '-- Entire list --'), '#default_value' => isset($segment_id) ? $segment_id : '');
     } else {
         $form['list_segment_id'] = array();
     }
     $form['list_segment_id']['#prefix'] = '<div id="list-segments-wrapper">';
     $form['list_segment_id']['#suffix'] = '</div>';
     $form['from_email'] = array('#type' => 'textfield', '#title' => t('From Email'), '#description' => t('the From: email address for your campaign message.'), '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data['from_email'] : $site_config->get('mail'), '#size' => 40, '#maxlength' => 255, '#required' => TRUE);
     $form['from_name'] = array('#type' => 'textfield', '#title' => t('From Name'), '#description' => t('the From: name for your campaign message (not an email address)'), '#default_value' => !empty($campaign->mc_data) ? $campaign->mc_data['from_name'] : $site_config->get('name'), '#size' => 40, '#maxlength' => 255, '#required' => TRUE);
     $template_type_labels = array('user' => 'My Custom Templates', 'basic' => 'Basic Templates', 'gallery' => 'Themes');
     $form['template_id'] = array('#type' => 'select', '#title' => t('Template'), '#description' => 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' => $campaign ? $campaign->mc_data['template_id'] : -1, '#ajax' => array('callback' => 'Drupal\\mailchimp_campaign\\Form\\MailchimpCampaignForm::templateCallback'));
     $form['content'] = array('#id' => 'content-sections', '#type' => 'fieldset', '#title' => t('Content sections'), '#description' => 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(array($list_id));
         $merge_vars = $merge_vars_list[$list_id]['merge_vars'];
     } else {
         $merge_vars = array();
     }
     $campaign_template = $campaign->getTemplate();
     $campaign_content = $form_state->getValue('content');
     $entity_type = NULL;
     if ($mc_template) {
         if (strpos($mc_template['info']['source'], 'mc:repeatable')) {
             drupal_set_message(t('WARNING: This template has repeating sections, which are not supported. You may want to select a different template.'), 'warning');
         }
         foreach ($mc_template['info']['default_content'] 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'] = array('#type' => 'details', '#title' => SafeMarkup::checkPlain(ucfirst($section)), '#open' => FALSE);
             $form['content'][$section . '_wrapper'][$section] = array('#type' => 'text_format', '#format' => $format, '#title' => SafeMarkup::checkPlain(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'] = array('#type' => 'details', '#title' => t('Content'), '#open' => FALSE);
         $form['content']['html_wrapper']['html'] = array('#type' => 'text_format', '#format' => $campaign_template != NULL ? $campaign_template['html']['format'] : 'mailchimp_campaign', '#title' => t('Content'), '#description' => 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'] = array('#title' => t('Campaign content preview'), '#type' => 'details', '#open' => TRUE);
         $form['preview_wrapper']['preview'] = array('#markup' => $form_state->getValue('mailchimp_campaign_campaign_preview'));
     }
     return $form;
 }