You are here

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

Class

TermDuplicate
Duplicate term.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
  $messenger = \Drupal::messenger();
  $request = \Drupal::request();
  $storage = \Drupal::service('entity_type.manager')
    ->getStorage('taxonomy_term');
  if ($ids = explode('|', $request->query
    ->get('ids'))) {
    $tree = $storage
      ->loadTree($storage
      ->load(end($ids))
      ->bundle(), 0, NULL, TRUE);
    $names = '';
    $terms = $name_ids = [];

    // $dashed = $this->dashTerms('ru-RU', '','',1); || $dashed[$id] != $name
    foreach ($tree as $term) {
      $id = $term
        ->id();
      $name = $term
        ->getName();
      if (!in_array($id, $ids)) {
        $messenger
          ->addError("The term {$id}={$name} is wrong!!!!");
      }
      $name = "{$id}=" . str_repeat('-', $term->depth) . $name . PHP_EOL;
      $names .= $name;
      $terms[$id] = $term;
    }
    $values = [];
    $form_state
      ->set('term', end($values));
    $form_state
      ->set('terms', $terms);
    $readmehelp = readmehelp_converter_service();
    $path = $readmehelp->moduleHandler
      ->getModuleDirectories()['commerce_bulk'] . '/commerce_bulk.module';
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h2>Change and / or add terms for %count <span style="color:red">Name</span>s. New <em>Names</em> should be inserted after the last existing: one <em>Name</em> on each line with prepended with one dash "-" symbol for a child term, two dashes for a grandchild and so on.', [
        '%count' => count($terms),
      ]),
    ];
    $form['names'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Names'),
      '#default_value' => $names,
      '#rows' => 20,
    ];
    $form['data_warning'] = [
      '#markup' => new TranslatableMarkup('<h3><mark>Tip:</mark> Optionally, you can pass <em>XML</em> or <em>JSON</em> data and alter each term value in the <mark>YOUR_MODULE_commerce_bulk_term_new_alter()</mark>. See example hook implementation in the commerce_bulk.module file:</h3><div style="border:1px solid grey">' . $readmehelp
        ->highlightPhp($path, 74, 8) . '</div>'),
    ];
    $form['data'] = [
      '#type' => 'textarea',
      '#title' => $this
        ->t('Optional JSON or XML data'),
      '#rows' => 1,
    ];
    $form['cancel'] = [
      '#type' => 'submit',
      '#value' => 'CANCEL AND BACK',
      '#weight' => 1000,
    ];

    // Remove the "Action was applied to N items" message.
    $messenger
      ->deleteByType('status');
  }
  return $form;
}