You are here

public function GenerateProducts::settingsForm in Commerce Bulk 8

File

modules/commerce_generate/src/Plugin/DevelGenerate/GenerateProducts.php, line 157

Class

GenerateProducts
Provides a GenerateProducts plugin.

Namespace

Drupal\commerce_generate\Plugin\DevelGenerate

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $stores = $this->storeStorage
    ->loadMultiple();
  if (empty($stores)) {
    $create_url = $this->urlGenerator
      ->generateFromRoute('entity.commerce_store.add_page');
    $this
      ->setMessage($this
      ->t('You do not have any stores to which generated products could be assigned. <a href=":create-type">Go create a new store</a>', [
      ':create-type' => $create_url,
    ]), 'error', FALSE);
    return;
  }
  $types = $this->productTypeStorage
    ->loadMultiple();
  if (empty($types)) {
    $create_url = $this->urlGenerator
      ->generateFromRoute('entity.commerce_product_type.add_form');
    $this
      ->setMessage($this
      ->t('You do not have any product types that can be generated. <a href=":create-type">Go create a new product type</a>', [
      ':create-type' => $create_url,
    ]), 'error', FALSE);
    return;
  }
  $options = [];
  foreach ($stores as $store) {
    $options[$store
      ->id()] = [
      'type' => [
        '#markup' => $store
          ->label(),
      ],
      'store_type' => [
        '#markup' => $store
          ->bundle(),
      ],
    ];
  }
  $header = [
    'type' => $this
      ->t('Assign products to stores'),
    'store_type' => [
      'data' => $this
        ->t('Machine name'),
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
  ];
  $form['stores'] = [
    '#prefix' => $this
      ->t('<h6>Select at least one of the stores below:</h6>'),
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
  ];
  $options = [];
  foreach ($types as $type) {
    $options[$type
      ->id()] = [
      'type' => [
        '#markup' => $type
          ->label(),
      ],
      'variation_type' => [
        '#markup' => $type
          ->getVariationTypeId(),
      ],
    ];
  }
  $header = [
    'type' => $this
      ->t('Product type'),
    'variation_type' => [
      'data' => $this
        ->t('Machine name'),
      'class' => [
        RESPONSIVE_PRIORITY_MEDIUM,
      ],
    ],
  ];
  $form['product_types'] = [
    '#prefix' => $this
      ->t('<h6>Select at least one of the product types below:</h6>'),
    '#type' => 'tableselect',
    '#header' => $header,
    '#options' => $options,
  ];
  $form['kill'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('<strong>Delete ALL (<mark>sic!</mark>) products</strong> in the selected stores or of the selected product types before generating new ones. As an example you may delete all products belonging to a particular store by selecting this store (without product types) and setting the number of products to generate to 0 and then pressing the <strong>Generate</strong> button. By the way, the internal name of this checkbox is <strong>"kill"</strong> so, be careful when considering which products to delete. <mark>You are warned.</mark>'),
    '#default_value' => $this
      ->getSetting('kill'),
  ];
  $form['num'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The total number of products to generate.'),
    '#default_value' => $this
      ->getSetting('num'),
    '#required' => TRUE,
    '#step' => 1,
    '#min' => 0,
  ];
  $form['max_nb_skus'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The maximum number of variations to generate per product.'),
    '#description' => $this
      ->t('Leave empty to generate all possible variations.'),
    '#default_value' => $this
      ->getSetting('max_nb_skus'),
    '#required' => FALSE,
    '#step' => 1,
    '#min' => 2,
  ];
  $form['shuffle_variations'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Shuffle variation combinations.'),
    '#default_value' => $this
      ->getSetting('shuffle_variations'),
    '#description' => $this
      ->t('Tick this checkbox if you want to randomize the order of the generated variations.'),
  ];
  $form['batch'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The treshold for batch.'),
    '#description' => $this
      ->t("The number of products at which to start a batch products' generating process instead of doing this in one go. Environments where the module works may differ, the structure of product types may differ even more. So, adjust the treshold for your needs, depending on capacity of the server and products you are going to generate."),
    '#default_value' => $this
      ->getSetting('batch'),
    '#required' => TRUE,
    '#step' => 1,
    '#min' => 2,
  ];
  $form['title_prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('The title prefix'),
    '#description' => $this
      ->t('The word to prepend to a randomly generated product title.'),
    '#default_value' => $this
      ->getSetting('title_prefix'),
    '#required' => TRUE,
  ];
  $form['title_length'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum number of words in titles'),
    '#description' => $this
      ->t('Leave empty for no randomly generated words in title. Useful if you want generate one particular product for further use in production. Note that each product variation title will be prefixed with a product title so, use the title prefix field above to assign desirable title for a product and therefore title prefix for all its variations.'),
    '#default_value' => $this
      ->getSetting('title_length'),
    '#step' => 1,
    '#min' => 1,
    '#max' => 25,
  ];
  $form['price_min'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The minimum of the randomly generated price.'),
    '#default_value' => $this
      ->getSetting('price_min'),
    '#step' => '0.01',
    '#min' => '0.01',
  ];
  $form['price_max'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The maximum of the randomly generated price.'),
    '#default_value' => $this
      ->getSetting('price_max'),
    '#step' => '0.01',
    '#min' => '0.01',
  ];
  $form['list_price_min'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The minimum of the randomly generated list price. Leave empty for no list price.'),
    '#default_value' => $this
      ->getSetting('price_min'),
    '#step' => '0.01',
    '#min' => '0',
  ];
  $form['list_price_max'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('The maximum of the randomly generated list price. Leave empty for no list price.'),
    '#default_value' => $this
      ->getSetting('price_max'),
    '#step' => '0.01',
    '#min' => '0',
  ];
  $form['price_per_variation'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Set random price per variation instead of per product basis.'),
    '#default_value' => $this
      ->getSetting('price_per_variation'),
  ];
  $form['price_number'] = [
    '#type' => 'value',
    '#value' => '1.00',
  ];
  $form['owner'] = [
    '#type' => 'commerce_entity_select',
    '#title' => t('Product owner'),
    '#target_type' => 'user',
    '#description' => $this
      ->t('The user to assign as owner for generated products. Leave empty for randomly selected users.'),
  ];
  $options = [
    1 => $this
      ->t('Now'),
  ];
  foreach ([
    3600,
    86400,
    604800,
    2592000,
    31536000,
  ] as $interval) {
    $options[$interval] = $this->dateFormatter
      ->formatInterval($interval, 1) . ' ' . $this
      ->t('ago');
  }
  $form['time_range'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('How far back in time should the products be dated?'),
    '#description' => $this
      ->t('Product creation dates will be distributed randomly from the current time, back to the selected time.'),
    '#options' => $options,
    '#default_value' => 3600,
  ];
  $options = [];

  // We always need a language.
  $languages = $this->languageManager
    ->getLanguages(LanguageInterface::STATE_ALL);
  foreach ($languages as $langcode => $language) {
    $options[$langcode] = $language
      ->getName();
  }
  $form['add_language'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Set language on products'),
    '#multiple' => TRUE,
    '#description' => $this
      ->t('Requires locale.module'),
    '#options' => $options,
    '#default_value' => [
      $this->languageManager
        ->getDefaultLanguage()
        ->getId(),
    ],
  ];
  $form['#redirect'] = FALSE;
  return $form;
}