You are here

class SmartImporterConfigurationForm in Commerce Smart Importer 8

Smart importer config form.

Hierarchy

Expanded class hierarchy of SmartImporterConfigurationForm

1 string reference to 'SmartImporterConfigurationForm'
commerce_smart_importer.routing.yml in ./commerce_smart_importer.routing.yml
commerce_smart_importer.routing.yml

File

src/Form/SmartImporterConfigurationForm.php, line 17

Namespace

Drupal\commerce_smart_importer\Form
View source
class SmartImporterConfigurationForm extends ConfigFormBase {

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * Database service.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $smartImporterService;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface
   */
  protected $moduleHandler;

  /**
   * File system.
   *
   * @var FileSystem
   */
  protected $fileSystem;

  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'commerce_smart_importer_settings';
  }

  /**
   * {@inheritdoc}
   */
  protected function getEditableConfigNames() {
    return [
      'commerce_smart_importer.settings',
    ];
  }

  /**
   * SmartImporterConfigurationForm constructor.
   */
  public function __construct(Connection $connection, CommerceSmartImporerService $service, ModuleHandler $moduleHandler, FileSystem $fileSystem) {
    $this->database = $connection;
    $this->smartImporterService = $service;
    $this->moduleHandler = $moduleHandler;
    $this->fileSystem = $fileSystem;
  }

  /**
   * Create.
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('database'), $container
      ->get('commerce_smart_importer.service'), $container
      ->get('module_handler'), $container
      ->get('file_system'));
  }

  /**
   * {@inheritdoc}
   */
  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);
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
    $values = $form_state
      ->getValues();
    $config = $this
      ->config('commerce_smart_importer.settings');
    if ($values['store'] != '') {
      $config
        ->set('store', $values['store']);
    }
    elseif ($config
      ->get('store') == NULL) {
      $config
        ->set('store', 1);
    }
    if ($values['sku_prefix'] != '') {
      $config
        ->set('sku_prefix', $values['sku_prefix']);
    }
    elseif ($config
      ->get('sku_prefix') == NULL) {
      $config
        ->set('sku_prefix', 'si_');
    }
    if ($values['sku_method'] != '') {
      $config
        ->set('sku_method', $values['sku_method']);
    }
    elseif ($config
      ->get('sku_method') == NULL) {
      $config
        ->set('sku_method', 1);
    }
    if ($values['sku_digits'] != '' && $values['sku_digits'] > 2 && $values['sku_digits'] < 20 && is_numeric($values['sku_digits'])) {
      $config
        ->set('sku_random_digits', floor($values['sku_digits']));
    }
    elseif ($values['sku_digits'] > 2 && $values['sku_digits'] <= 20 && is_numeric($values['sku_digits'])) {
      if ($config
        ->get('sku_random_digits') == NULL) {
        $config
          ->set('sku_random_digits', 6);
      }
    }
    elseif ($config
      ->get('sku_random_digits') == NULL) {
      $config
        ->set('sku_random_digits', 6);
    }
    if (!empty($values['batch'])) {
      $config
        ->set('batch_products', $values['batch']);
    }
    elseif ($config
      ->get('batch_products') == NULL) {
      $config
        ->set('batch_products', 50);
    }
    if (!empty($values['commerce_product_variation_bundle'])) {
      $config
        ->set('commerce_product_variation_bundle', $values['commerce_product_variation_bundle']);
    }
    elseif ($config
      ->get('commerce_product_variation_bundle') == NULL) {
      $config
        ->set('commerce_product_variation_bundle', 'default');
    }
    if (!empty($values['commerce_product_bundle'])) {
      $config
        ->set('commerce_product_bundle', $values['commerce_product_bundle']);
    }
    elseif ($config
      ->get('commerce_product_bundle') == NULL) {
      $config
        ->set('commerce_product_bundle', 'default');
    }
    if (!empty($values['external_folders'])) {
      $config
        ->set('external_folders', explode(',', $values['external_folders']));
    }
    elseif ($config
      ->get('external_folders') == NULL) {
      $config
        ->set('external_folders', []);
    }
    if (array_key_exists('flush_image_cache', $values)) {
      $config
        ->set('flush_image_cache', $values['flush_image_cache']);
    }
    $config
      ->set('expose_store', $values['expose_store']);
    $config
      ->save();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigFormBaseTrait::config protected function Retrieves a configuration object.
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
FormBase::validateForm public function Form validation handler. Overrides FormInterface::validateForm 62
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
SmartImporterConfigurationForm::$database protected property Database service.
SmartImporterConfigurationForm::$fileSystem protected property File system.
SmartImporterConfigurationForm::$moduleHandler protected property The module handler.
SmartImporterConfigurationForm::$smartImporterService protected property Database service.
SmartImporterConfigurationForm::buildForm public function Form constructor. Overrides ConfigFormBase::buildForm
SmartImporterConfigurationForm::create public static function Create. Overrides ConfigFormBase::create
SmartImporterConfigurationForm::getEditableConfigNames protected function Gets the configuration names that will be editable. Overrides ConfigFormBaseTrait::getEditableConfigNames
SmartImporterConfigurationForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
SmartImporterConfigurationForm::submitForm public function Form submission handler. Overrides ConfigFormBase::submitForm
SmartImporterConfigurationForm::__construct public function SmartImporterConfigurationForm constructor. Overrides ConfigFormBase::__construct
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.