You are here

public function MailchimpEcommerceAdminSettings::buildForm in Mailchimp E-Commerce 8

Form constructor.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Return value

array The form structure.

Overrides ConfigFormBase::buildForm

2 calls to MailchimpEcommerceAdminSettings::buildForm()
MailchimpEcommerceCommerceAdminSettings::buildForm in modules/mailchimp_ecommerce_commerce/src/Form/MailchimpEcommerceCommerceAdminSettings.php
Form constructor.
MailchimpEcommerceUbercartAdminSettings::buildForm in modules/mailchimp_ecommerce_ubercart/src/Form/MailchimpEcommerceUbercartAdminSettings.php
Form constructor.
2 methods override MailchimpEcommerceAdminSettings::buildForm()
MailchimpEcommerceCommerceAdminSettings::buildForm in modules/mailchimp_ecommerce_commerce/src/Form/MailchimpEcommerceCommerceAdminSettings.php
Form constructor.
MailchimpEcommerceUbercartAdminSettings::buildForm in modules/mailchimp_ecommerce_ubercart/src/Form/MailchimpEcommerceUbercartAdminSettings.php
Form constructor.

File

src/Form/MailchimpEcommerceAdminSettings.php, line 95

Class

MailchimpEcommerceAdminSettings

Namespace

Drupal\mailchimp_ecommerce\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $form['mailchimp_ecommerce_notice'] = [
    '#markup' => t('This page will allow you to create a store. Once created, you cannot change the audience associated with the store.'),
  ];
  $form['mailchimp_ecommerce_store_name'] = [
    '#type' => 'textfield',
    '#title' => t('Store Name'),
    '#required' => TRUE,
    '#default_value' => \Drupal::config('mailchimp_ecommerce.settings')
      ->get('mailchimp_ecommerce_store_name'),
    '#description' => t('The name of your store as it should appear in your Mailchimp account.'),
  ];
  $mailchimp_lists = mailchimp_get_lists();
  $list_options = [
    '' => '-- Select --',
  ];
  foreach ($mailchimp_lists as $list_id => $list) {
    $list_options[$list_id] = $list->name;
  }
  if (!empty(\Drupal::config('mailchimp_ecommerce.settings')
    ->get('mailchimp_ecommerce_list_id'))) {
    $existing_store_id = \Drupal::config('mailchimp_ecommerce.settings')
      ->get('mailchimp_ecommerce_list_id');
    $form['mailchimp_ecommerce_list_id_existing'] = [
      '#markup' => t('Once created, the audience cannot be changed for a given store. This store is connected to the audience named') . ' ' . $list_options[$existing_store_id],
    ];
  }
  else {
    $form['mailchimp_ecommerce_list_id'] = [
      '#type' => 'select',
      '#title' => t('Store Audience'),
      '#required' => TRUE,
      '#options' => $list_options,
      '#default_value' => \Drupal::config('mailchimp_ecommerce.settings')
        ->get('mailchimp_ecommerce_list_id'),
    ];
  }
  $list_options_currency = [
    '' => '-- Select --',
  ] + mailchimp_ecommerce_get_currency_codes();
  $form['mailchimp_ecommerce_currency'] = [
    '#type' => 'select',
    '#options' => $list_options_currency,
    '#title' => t('Store Currency Code'),
    '#required' => TRUE,
    '#description' => t('This is overridden if you have selected to use the default currency from Commerce.'),
  ];
  $options = [
    '' => t('-- Select --'),
  ];
  $has_images = false;
  $desired_type = '';
  $field_map = \Drupal::entityManager()
    ->getFieldMap();
  $moduleHandler = \Drupal::service('module_handler');
  if ($moduleHandler
    ->moduleExists('mailchimp_ecommerce_commerce')) {
    $desired_type = 'commerce_product';
  }
  elseif ($moduleHandler
    ->moduleExists('mailchimp_ecommerce_ubercart')) {
    $desired_type = 'node';
  }
  $field_definitions = [];
  foreach ($field_map as $entity_type => $fields) {
    $field_definitions[$entity_type] = \Drupal::entityManager()
      ->getFieldStorageDefinitions($entity_type);
  }
  foreach ($field_map as $entity_type => $fields) {
    if ($entity_type == $desired_type) {
      foreach ($fields as $field_name => $field_properties) {
        if ($field_properties['type'] == 'image') {
          $options[$field_name] = $field_name;
          $has_images = true;
        }
      }
    }
  }
  if ($has_images) {
    $form['product_image'] = [
      '#type' => 'select',
      '#title' => t('Product Image'),
      '#multiple' => FALSE,
      '#description' => t('Please choose the image field for your products.'),
      '#options' => $options,
      '#default_value' => \Drupal::config('mailchimp_ecommerce.settings')
        ->get('product_image'),
      '#required' => TRUE,
    ];
  }
  if (!empty(\Drupal::config('mailchimp_ecommerce.settings')
    ->get('mailchimp_ecommerce_store_id'))) {
    $form['sync'] = [
      '#type' => 'fieldset',
      '#title' => t('Product sync'),
      '#collapsible' => FALSE,
    ];
    $form['sync']['products'] = [
      '#markup' => \Drupal::l(t('Sync existing products to Mailchimp'), Url::fromRoute('mailchimp_ecommerce.sync')),
    ];
    $form['sync-orders'] = [
      '#type' => 'fieldset',
      '#title' => t('Order sync'),
      '#collapsible' => FALSE,
    ];
    $form['sync-orders']['orders'] = [
      '#markup' => \Drupal::l(t('Sync existing orders to Mailchimp'), Url::fromRoute('mailchimp_ecommerce.sync_orders')),
    ];
  }
  $form['platform'] = [
    '#type' => 'hidden',
    '#default_value' => '',
  ];
  $settings_form = parent::buildForm($form, $form_state);
  return $settings_form;
}