public function VariationDelete::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/ VariationDelete.php, line 32
Class
- VariationDelete
- Delete variations.
Namespace
Drupal\commerce_bulk\Plugin\ActionCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$request = \Drupal::request();
$storage = \Drupal::service('entity_type.manager')
->getStorage('commerce_product_variation');
if ($ids = explode('|', $request->query
->get('ids'))) {
$variations = $storage
->loadMultiple($ids);
$form_state
->set('variations', $variations);
$list = '<ol>';
foreach ($variations as $variation) {
$list .= '<li><h5>' . $variation
->getTitle() . '</h5></li>';
}
$list .= '</ol>';
$form_state
->set('product', $variation
->getProduct());
$form['warning'] = [
'#markup' => new TranslatableMarkup('<h1>You are about to delete the following variations:</h1>' . $list),
];
$form['strong_warning'] = [
'#markup' => new TranslatableMarkup('<h2 style="color:red">After this operation your life will never be the same.</h2>'),
];
$form['cancel'] = [
'#type' => 'submit',
'#value' => 'CANCEL AND BACK',
'#weight' => 1000,
];
// Remove the "Action was applied to N items" message.
\Drupal::messenger()
->deleteByType('status');
// TODO: delete the fix below after a while.
$storage = \Drupal::entityTypeManager()
->getStorage('commerce_product_variation');
$product = $variation
->getProduct();
$ids = $product
->getVariationIds();
$count = 0;
foreach ($storage
->loadByproperties([
'product_id' => $product
->id(),
]) as $variation) {
if (!in_array($variation
->id(), $ids)) {
$variation
->delete();
$count++;
}
}
}
$count && \Drupal::messenger()
->addWarning(new TranslatableMarkup('Deleted %count orphaned variations. See more: <a href=":href" target="_blank">https://www.drupal.org/project/commerce_bulk/issues/3027034</a>', [
'%count' => $count,
':href' => 'https://www.drupal.org/project/commerce_bulk/issues/3027034',
]));
return $form;
}