You are here

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

Class

TermDelete
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('taxonomy_term');
  if ($ids = explode('|', $request->query
    ->get('ids'))) {
    $terms = $storage
      ->loadMultiple($ids);
    $form_state
      ->set('terms', $terms);
    $list = '<ol>';
    foreach ($terms as $term) {
      $list .= '<li><h5>' . $term
        ->getName() . '</h5></li>';
    }
    $list .= '</ol>';
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h1>You are about to delete the following terms:</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');
  }
  return $form;
}