You are here

public function AttributeValueDelete::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/AttributeValueDelete.php, line 33

Class

AttributeValueDelete
Delete variations.

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_attribute_value');
  if ($ids = explode('|', $request->query
    ->get('ids'))) {
    $attributes = $storage
      ->loadMultiple($ids);
    $form_state
      ->set('attributes', $attributes);
    $list = '<ol>';
    foreach ($attributes as $attribute) {
      $list .= '<li><h5>' . $attribute
        ->getName() . '</h5></li>';
    }
    $list .= '</ol>';
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h2>You are about to delete the following attributes:</h2>' . $list),
    ];
    $form['strong_warning'] = [
      '#markup' => new TranslatableMarkup('<h3>Ensure the attributes above are <span style="color:red">NOT</span> in use on some existing product variation.</h3><h3 style="color:red">After this operation your life will never be the same.</h3>'),
    ];
    $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;
}