public function BulkVariationsCreator::createAllIefFormVariations in Commerce Bulk 8
An AJAX callback to create all possible variations.
Parameters
array $form: An array form for commerce_product with ief widget.
\Drupal\Core\Form\FormStateInterface $form_state: The form state of the commerce_product form with at least one variation created.
Overrides BulkVariationsCreatorInterface::createAllIefFormVariations
See also
self->getIefFormAllAttributesCombinations()
File
- src/
BulkVariationsCreator.php, line 250
Class
- BulkVariationsCreator
- Default implementation of the BulkVariationsCreatorInterface.
Namespace
Drupal\commerce_bulkCode
public function createAllIefFormVariations(array $form, FormStateInterface $form_state) {
// Rid of entity type manager here as that prevents to use instance of
// BulkVariationsCreator as an AJAX callback therefore forcing to use
// just the class name instead of object and define all functions as static.
$this->entityTypeManager = NULL;
$ief_id = $form['variations']['widget']['#ief_id'];
$ief_entities = $form_state
->get([
'inline_entity_form',
$ief_id,
'entities',
]) ?: [];
if (!($all = $this
->getAttributesCombinations(array_column($ief_entities, 'entity')))) {
return;
}
// The attributes (ids and options) may be quite heavy, so unset them.
unset($all['attributes']);
$timestamp = time();
$ief_entity = end($ief_entities);
$settings = static::getSkuSettings($all['last_variation']);
extract($settings);
$prefix = isset($prefix) ? $prefix : '';
$suffix = isset($suffix) ? $suffix : '';
$more_entropy = isset($more_entropy) ? $more_entropy : FALSE;
$module_handler = \Drupal::moduleHandler();
$clone = clone $all['last_variation'];
foreach ($all['not_used_combinations'] as $combination) {
$variation = $all['last_variation']
->createDuplicate()
->setChangedTime($timestamp)
->setCreatedTime($timestamp);
$sku = \uniqid($prefix, $more_entropy) . $suffix;
unset($settings['combination']);
$settings['combination'] = $combination;
$module_handler
->alter("bulk_creator_sku", $sku, $settings, $clone);
$variation
->setSku($sku);
foreach ($combination as $field_name => $id) {
$variation
->get($field_name)
->setValue([
'target_id' => $id == '_none' ? NULL : $id,
]);
}
$variation
->updateOriginalValues();
$ief_entity['entity'] = $variation;
$ief_entity['weight'] += 1;
$ief_entity['needs_save'] = TRUE;
array_push($ief_entities, $ief_entity);
$timestamp--;
}
// Before continuing unset $all['*combinations'] which might be a huge data.
unset($all);
$form_state
->set([
'inline_entity_form',
$ief_id,
'entities',
], $ief_entities);
$form_state
->setRebuild();
}