You are here

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

Class

AttributeValueName
Duplicate attribute value.

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);
    $names = '';
    foreach ($attributes as $attribute) {
      $names .= $attribute
        ->getName() . PHP_EOL;
    }
    $values = $attribute
      ->getAttribute()
      ->getValues();
    $form_state
      ->set('attribute', end($values));
    $form_state
      ->set('attributes', $attributes);
    $readmehelp = readmehelp_converter_service();
    $path = $readmehelp->moduleHandler
      ->getModuleDirectories()['commerce_bulk'] . '/commerce_bulk.module';
    $form['warning'] = [
      '#markup' => new TranslatableMarkup('<h2>Change and / or add attributes <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.'),
    ];
    $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 attribute value in the <mark>YOUR_MODULE_commerce_bulk_attribute_value_alter()</mark>. See example hook implementation in the commerce_bulk.module file:</h3><div style="border:1px solid grey">' . $readmehelp
        ->highlightPhp($path, 59, 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.
    \Drupal::messenger()
      ->deleteByType('status');
  }
  return $form;
}