You are here

public function MandrillTemplateMapForm::form in Mandrill 8

Gets the actual form array to be built.

Overrides EntityForm::form

See also

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

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

File

modules/mandrill_template/src/Form/MandrillTemplateMapForm.php, line 26
Contains \Drupal\mandrill_template\Form\MandrillTemplateMapForm.

Class

MandrillTemplateMapForm
Form controller for the MandrillTemplateMap entity edit form.

Namespace

Drupal\mandrill_template\Form

Code

public function form(array $form, FormStateInterface $form_state) {
  $form = parent::form($form, $form_state);

  /* @var $map \Drupal\mandrill_template\Entity\MandrillTemplateMap */
  $map = $this->entity;

  /* @var $mandrill_api \Drupal\mandrill\MandrillAPI */
  $mandrill_api = \Drupal::service('mandrill.api');
  $templates = $mandrill_api
    ->getTemplates();
  $form['label'] = array(
    '#type' => 'textfield',
    '#title' => $this
      ->t('Label'),
    '#default_value' => $map->label,
    '#description' => t('The human-readable name of this Mandrill Template Map entity.'),
    '#required' => TRUE,
  );
  $form['id'] = array(
    '#type' => 'machine_name',
    '#default_value' => $map->id,
    '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH,
    '#machine_name' => array(
      'source' => array(
        'label',
      ),
      'exists' => array(
        $this,
        'exists',
      ),
    ),
    '#description' => t('A unique machine-readable name for this Mandrill Template Map entity. It must only contain lowercase letters, numbers, and underscores.'),
    '#disabled' => !$map
      ->isNew(),
  );
  $form['map_settings'] = array(
    '#type' => 'fieldset',
    '#title' => t('Template Map Settings'),
    '#collapsible' => FALSE,
    '#prefix' => '<div id="template-wrapper">',
    '#suffix' => '</div>',
  );
  $template_names = array();
  foreach ($templates as $template) {
    $template_names[$template['slug']] = $template;
  }

  // Check if the currently configured template still exists.
  if (!empty($map->template_id) && !array_key_exists($map->template_id, $template_names)) {
    $this
      ->messenger()
      ->addWarning(t('The configured Mandrill template is no longer available, please select a valid one.'));
  }
  if (!empty($templates)) {
    $options = array(
      '' => t('-- Select --'),
    );
    foreach ($templates as $template) {
      $options[$template['slug']] = $template['name'];
    }
    $form['map_settings']['template_id'] = array(
      '#type' => 'select',
      '#title' => t('Email Template'),
      '#description' => t('Select a Mandrill template.'),
      '#options' => $options,
      '#default_value' => isset($map->template_id) ? $map->template_id : '',
      '#required' => TRUE,
      '#ajax' => array(
        'callback' => '::template_callback',
        'wrapper' => 'template-wrapper',
        'method' => 'replace',
        'effect' => 'fade',
        'progress' => array(
          'type' => 'throbber',
          'message' => t('Retrieving template information.'),
        ),
      ),
    );
    $form_template_id = $form_state
      ->getValue('template_id');
    if (!$form_template_id && isset($map->mandrill_template_map_entity_id)) {
      $form_template_id = $map->template_id;
    }
    if ($form_template_id) {
      $regions = array(
        '' => t('-- Select --'),
      ) + $this
        ->parseTemplateRegions($template_names[$form_template_id]['publish_code']);
      $form['map_settings']['main_section'] = array(
        '#type' => 'select',
        '#title' => t('Template region'),
        '#description' => t('Select the template region to use for email content. <i>Note that you can populate more regions by attaching an array to your message with the index "mandrill_template_content", using region names as indexes to the content for that region.'),
        '#options' => $regions,
        '#default_value' => isset($map->main_section) ? $map->main_section : '',
        '#required' => TRUE,
      );
    }
    $usable_keys = mandrill_template_map_usage();
    $module_names = mandrill_get_module_key_names();
    $mandrill_in_use = FALSE;
    $available_modules = FALSE;
    $mailsystem_options = array(
      '' => t('-- None --'),
    );
    foreach ($usable_keys as $key => $sys) {
      $mandrill_in_use = TRUE;
      if ($sys === NULL || isset($map) && $sys == $map->mandrill_template_map_entity_id) {
        $mailsystem_options[$key] = $module_names[$key];
        $available_modules = TRUE;
      }
    }
    if ($mandrill_in_use) {
      $form['mailsystem_key'] = array(
        '#type' => 'select',
        '#title' => t('Email key'),
        '#description' => t('Select a module and mail key to use this template for outgoing email. Note that if an email has been selected in another Template Mapping, it will not appear in this list. These keys are defined through the %MailSystem interface.', array(
          '%MailSystem' => Link::fromTextAndUrl(t('MailSystem'), Url::fromRoute('mailsystem.settings'))
            ->toString(),
        )),
        '#options' => $mailsystem_options,
        '#default_value' => isset($map->mailsystem_key) ? $map->mailsystem_key : '',
      );
      if (!$available_modules) {
        $this
          ->messenger()
          ->addWarning(t("All email-using modules that have been assigned to Mandrill are already assigned to other template maps"));
      }
    }
    if (!$mandrill_in_use) {
      $this
        ->messenger()
        ->addWarning(t("You have not assigned any Modules to use Mandrill: to use this template, make sure Mandrill is assigned in Mailsystem."));
    }
  }
  else {
    $form['email_options']['#description'] = t('The template selection is only available if the Mandrill API is correctly configured and available.');
  }
  return $form;
}