You are here

public function AttributeValueName::submitConfigurationForm in Commerce Bulk 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the plugin form as built by static::buildConfigurationForm().

\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().

Overrides PluginFormInterface::submitConfigurationForm

File

src/Plugin/Action/AttributeValueName.php, line 79

Class

AttributeValueName
Duplicate attribute value.

Namespace

Drupal\commerce_bulk\Plugin\Action

Code

public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
  if ($form_state
    ->getTriggeringElement()['#id'] != 'edit-cancel') {
    if ($names = explode(PHP_EOL, trim($form_state
      ->getValue('names')))) {
      $module_handler = \Drupal::moduleHandler();
      $data = $form_state
        ->get('data');
      $attributes = array_values($form_state
        ->get('attributes'));
      $form_state
        ->set('attributes', NULL);
      $attribute = end($attributes);
      $weight = $form_state
        ->get('attribute')
        ->getWeight();
      foreach ($names as $index => $name) {
        if ($name = trim($name)) {
          if (isset($attributes[$index])) {
            $value = $attributes[$index];
          }
          else {
            $value = $attribute
              ->createDuplicate();
            $weight++;
            $value
              ->setWeight($weight);
          }
          $module_handler
            ->alter('commerce_bulk_attribute_value', $value, $name, $data);
          $value
            ->setName($name)
            ->save();
        }
      }
    }
  }
}