You are here

public function VariationDuplicate::buildConfigurationForm in Commerce Bulk 8

Form constructor.

Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see \Drupal\Core\Render\Element\FormElement.

Parameters

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

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().

Return value

array The form structure.

Overrides PluginFormInterface::buildConfigurationForm

File

src/Plugin/Action/VariationDuplicate.php, line 32

Class

VariationDuplicate
Duplicate variation.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $request = \Drupal::request();
  $storage = \Drupal::service('entity_type.manager')
    ->getStorage('commerce_product_variation');
  if ($id = $request->query
    ->get('id')) {
    $creator = \Drupal::service('commerce_bulk.variations_creator');
    $variation = $storage
      ->load($id);
    $product = $variation
      ->getProduct();
    $variations = $product
      ->getVariations();
    $all = $creator
      ->getAttributesCombinations($variations);
    $all['last_variation'] = $variation;
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h1>You are about to create <span style="color:red">@not_used</span> variations:</h1><h3>The number of created variations can be narrowed down by unselecting some attribute options below. Use <mark>Ctrl</mark> or <mark>Shift</mark> keys to select multiple options. Note that default maximum possible number of variations to create in one go is <span style="color:red">500</span>. If you experience problems when creating large amount of variations try to change this number on <a href=":href" target="_blank">the variation type SKU widget settings</a>. Also, the server <span style="color:red">php.ini</span> configuration file settings can be increased in order to be able to perform this operation.</h3>', [
        '@not_used' => $all['not_used'],
        ':href' => '/admin/commerce/config/product-variation-types/' . $variation
          ->bundle() . '/edit/form-display',
      ]),
    ];
    $form['max_execution_time'] = [
      '#type' => 'number',
      '#title' => new TranslatableMarkup('Temporarily increase php.ini <span style="color:red">max_execution_time</span> setting. Leave empty to apply default <span style="color:red">%max_execution_time</span> seconds.', [
        '%max_execution_time' => ini_get('max_execution_time'),
      ]),
      '#min' => '0',
      '#step' => '1',
    ];
    $options = $creator
      ->getAttributeFieldOptionIds($variation);

    // First, move selected variation to the bottom.
    foreach ($variations as $index => $variation) {
      if ($variation == $all['last_variation']) {
        unset($variations[$index]);
        array_push($variations, $all['last_variation']);
        break;
      }
    }
    $form_state
      ->set('kit', [
      'creator' => $creator,
      'product' => $product,
      'variations' => $variations,
      'all' => $all,
      'options' => $options,
    ]);
    $form['attributes'] = [
      '#type' => 'container',
      '#attributes' => [
        'class' => [
          'container-inline',
        ],
      ],
    ];
    $values = [];
    foreach ($all['not_used_combinations'] as $index => $combination) {
      foreach ($combination as $key => $value) {
        $values[$key][$value] = $value;
      }
    }
    foreach ($options['options'] as $field_name => $value) {
      $definition = $variation
        ->get($field_name)
        ->getFieldDefinition();
      if (!($required = $definition
        ->isRequired() && !isset($value['_none']))) {
        $value = [
          '_none' => '',
        ] + $value;
      }
      foreach ($value as $key => $name) {
        if (!isset($values[$field_name][$key])) {

          // All combinations for this option are already used.
          unset($value[$key]);
        }
      }
      if ($size = count($value)) {
        $form['attributes'][$field_name] = [
          '#type' => 'select',
          '#title' => $definition
            ->getLabel(),
          '#field_prefix' => "<mark>{$size}</mark>",
          '#multiple' => TRUE,
          '#options' => $value,
          '#size' => $size,
          '#default_value' => array_keys($value),
          '#required' => $required,
        ];
      }
    }
    $form['cancel'] = [
      '#type' => 'submit',
      '#value' => 'CANCEL AND BACK',
      '#weight' => 1000,
    ];

    // Remove the "Action was applied to N items" message.
    \Drupal::messenger()
      ->deleteByType('status');
  }
  return $form;
}