You are here

public function CSVIntroductionForm::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 FormInterface::buildForm

File

src/Form/CSVIntroductionForm.php, line 71

Class

CSVIntroductionForm
Introduction form.

Namespace

Drupal\commerce_smart_importer\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {

  /**
   * @todo Module will only work on PHP 7.0.
   * @todo Enable user to change csv delimiter.
   * @todo add save option.
   * @todo excel support
   * @todo Add support for files.
   * @todo Try formatting fields with field schema.
   */
  $sql = $this->database
    ->select('commerce_store_field_data')
    ->fields('commerce_store_field_data', [
    'store_id',
  ])
    ->execute()
    ->fetchAll();
  if (count($sql) != 0) {
    global $base_url;
    if ($this->moduleHandler
      ->moduleExists('pathauto')) {
      $this
        ->messenger()
        ->addStatus($this
        ->t('Note that if you already have defined pattern in pathauto for product, it will override URL alias added by importer.'));
    }

    // TODO Rewrite instructions.
    $instructions = '<br><br><img width="100%" src="' . $base_url . '/' . drupal_get_path('module', 'commerce_smart_importer') . '/files/smart-importer-gudlines.png" id="tutorial">';

    /*$instructions .= '<div id="instructions">' . $this->t('Instructions') . '</div><br>';
      $instructions .= '<li class="li-element" id="product-header">' .
        $this->t('Part of header rounded up with') .
        ' <span class="red">' . $this->t('red') . '</span> ' .
        $this->t('are header fields for product. Product fields are always from Title column to SKU column') . '</li>';
      $instructions .= '<li class="li-element" id="variation-header">' .
        $this->t('Part of header rounded up with') .
        ' <span class="green">' . $this->t('green') . '</span>  ' .
        $this->t('are header fields for variation. Variation fields are always from SKU column to end') . '</li>';
      $instructions .= '<li class="li-element">' .
        $this->t('If you want one product to have more than one variation just leave product data(in that row) empty and that variation will be added to last product with filled data.') .
        $this->t('For instance in our case') . ' <span class="purple">' .
        $this->t('Playstation 4') . '</span> ' . $this->t('will have 3 variations') . '<span class="blue">' . $this->t('(Pro,Slim,Classic)') . '</span> ' . $this->t('like arrows are pointing') . '</li>';
      $instructions .= '<li class="li-element" id="multiple-values">' .
        $this->t('Multiple values: if you want multiple values in one filed just delimit them with |(pipe). Ex. XL|M will result in two values XL and M') . '</li>';*/
    $form['type'] = [
      '#type' => 'vertical_tabs',
      '#title' => $this
        ->t('Type'),
    ];
    $form['import_type'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('CSV Import'),
      '#group' => 'type',
    ];
    $form['import_type']['csv_download'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Download CSV template'),
      '#submit' => [
        [
          $this,
          'csvDownload',
        ],
      ],
    ];
    $form['import_type']['csv_import'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('I have CSV file for import'),
    ];
    $form['import_type']['instructions'] = [
      '#markup' => $instructions,
    ];
    $form['update_type'] = [
      '#type' => 'details',
      '#title' => $this
        ->t('CSV Export/Update'),
      '#group' => 'type',
    ];
    $form['update_type']['csv_update_download'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Export products'),
    ];
    $form['update_type']['csv_update'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('I have CSV file for update'),
    ];
    $form['#attached']['library'] = [
      'commerce_smart_importer/commerce-smart-importer-introduction-library',
    ];
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t("In order to use this module you'll have to create at least one store"));
  }
  return $form;
}