You are here

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

Class

OrderAnonymize
Delete terms.

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_order');
  if ($ids = explode('|', $request->query
    ->get('ids'))) {
    $default_fields = [
      'ip_address',
      'billing_profile',
      'shipping_profile',
      'data',
      'mail',
      'field_pakiautomaadid',
    ];
    $bundles = $fields = [];
    $orders = $storage
      ->loadMultiple($ids);
    $form_state
      ->set('orders', $orders);
    $list = '<ol>';
    foreach ($orders as $order) {
      $list .= '<li><h5>' . $order
        ->id() . '</h5></li>';
      $bundle = $order
        ->bundle();
      if (!isset($bundles[$bundle])) {
        $bundles[$bundle] = $bundle;
        foreach ($order
          ->getFieldDefinitions() as $id => $definition) {
          $fields[$id] = $definition
            ->getLabel();
        }
      }
    }
    $list .= '</ol>';
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h1>You are about to anonymize the following orders:</h1>' . $list),
    ];
    $form['strong_warning'] = [
      '#markup' => new TranslatableMarkup('<h2 style="color:red">After this operation your life will never be the same.</h2>'),
    ];
    $form['fields'] = [
      '#type' => 'select',
      '#title' => $this
        ->t('Fields'),
      '#description' => $this
        ->t('Select fields to anonymize. Use <mark>Ctrl</mark> or <mark>Shift</mark> keys to select multiple options.'),
      '#multiple' => TRUE,
      '#options' => $fields,
      '#size' => count($fields),
      '#default_value' => $default_fields,
      '#required' => TRUE,
    ];
    $form['order_age'] = [
      '#type' => 'number',
      '#title' => $this
        ->t('Order Age'),
      '#description' => $this
        ->t('Anonymize only orders older than a number of days specified. Leave empty to anonymize all selected orders.'),
      '#min' => '1',
      '#step' => '1',
    ];
    $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;
}