You are here

public function SmartImporterConfigurationForm::buildForm in Commerce Smart Importer 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

File

src/Form/SmartImporterConfigurationForm.php, line 89

Class

SmartImporterConfigurationForm
Smart importer config form.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, Request $request = NULL) {
  $config = $this
    ->config('commerce_smart_importer.settings');
  $importer_config = $this->smartImporterService
    ->getConfig();
  $sql = $this->database
    ->query("SELECT store_id, name FROM {commerce_store_field_data}")
    ->fetchAll();
  $options['all'] = 'All';
  foreach ($sql as $stores) {
    $options[$stores->store_id] = $stores->name;
  }
  if (count($sql) != 0) {
    if ($config
      ->get('store') == NULL) {
      $default_value = 1;
    }
    else {
      $default_value = $config
        ->get('store');
    }
    $form['store'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose your store'),
      '#options' => $options,
      '#default_value' => $default_value,
    ];
    $form['expose_store'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Expose store in CSV template'),
      '#default_value' => $config
        ->get('expose_store'),
    ];
    $bundles = $this->smartImporterService
      ->getEntityBundles('commerce_product');
    $form['commerce_product_bundle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose product bundle'),
      '#options' => $bundles,
      '#default_value' => $importer_config['commerce_product_bundle'],
    ];
    $bundles = $this->smartImporterService
      ->getEntityBundles('commerce_product_variation');
    $form['commerce_product_variation_bundle'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Choose product variation bundle'),
      '#options' => $bundles,
      '#default_value' => $importer_config['commerce_product_variation_bundle'],
    ];
    if ($config
      ->get('batch_products') == NULL) {
      $default_value = 50;
    }
    else {
      $default_value = $config
        ->get('batch_products');
    }
    $form['batch'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Choose how many products you want to import per one batch operation'),
      '#description' => $this
        ->t('Higher this number is faster import will be, but it is more likely that timeout error will occur'),
      '#default_value' => $default_value,
    ];
    $form['sku_container'] = [
      '#type' => 'fieldset',
      '#title' => $this
        ->t("SKU generate"),
      '#description' => $this
        ->t('SKU will only be generated if field is left empty'),
    ];
    if ($config
      ->get('sku_prefix') == NULL) {
      $default_value = 'si_';
    }
    else {
      $default_value = $config
        ->get('sku_prefix');
    }
    $form['sku_container']['sku_prefix'] = [
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
      '#type' => 'textfield',
      '#title' => $this
        ->t('SKU prefix'),
      '#size' => 5,
      '#default_value' => $default_value,
    ];
    if ($config
      ->get('sku_method') == NULL) {
      $default_value = 1;
    }
    else {
      $default_value = $config
        ->get('sku_method');
    }
    $form['sku_container']['sku_method'] = [
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
      '#type' => 'radios',
      '#title' => $this
        ->t('Choose generating method'),
      '#options' => [
        0 => $this
          ->t('Auto increment'),
        1 => $this
          ->t('Random digits'),
      ],
      '#default_value' => $default_value,
    ];
    if ($config
      ->get('sku_random_digits') == NULL) {
      $default_value = 6;
    }
    else {
      $default_value = $config
        ->get('sku_random_digits');
    }
    $form['sku_container']['sku_digits'] = [
      '#prefix' => '<div class="container-inline">',
      '#suffix' => '</div>',
      '#type' => 'textfield',
      '#title' => $this
        ->t('Number of random digits'),
      '#size' => 4,
      '#description' => $this
        ->t('(default: 6, must be between 3 and 20)'),
      '#states' => [
        'invisible' => [
          ':input[name="sku_method"]' => [
            'value' => 0,
          ],
        ],
      ],
      '#default_value' => $default_value,
    ];
    $form['external_folders'] = [
      '#type' => 'textfield',
      '#title' => $this
        ->t('External folders'),
      '#default_value' => implode(',', $config
        ->get('external_folders')),
      '#description' => $this
        ->t('Define external folders in Public file system folder, that will be scanned for images and files. Delimit with comma.'),
      '#suffix' => $this
        ->t('Your current  Public file system folder is ') . str_replace($_SERVER['DOCUMENT_ROOT'] . '/', '', $this->fileSystem
        ->realpath('public://')),
    ];
    $form['flush_image_cache'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Flush image styles when adding new image'),
    ];
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t("In order to use this module you'll have to create at least one store"));
  }
  return parent::buildForm($form, $form_state);
}