You are here

public function ObjectAttributesFormBase::submitForm in Ubercart 8.4

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

uc_attribute/src/Form/ObjectAttributesFormBase.php, line 144

Class

ObjectAttributesFormBase
Defines the class/product attributes overview form.

Namespace

Drupal\uc_attribute\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $changed = FALSE;
  foreach ($form_state
    ->getValue('attributes') as $aid => $attribute) {
    if ($attribute['remove']) {
      $remove_aids[] = $aid;
    }
    else {
      unset($attribute['remove']);
      \Drupal::database()
        ->merge($this->attributeTable)
        ->key('aid', $aid)
        ->fields($attribute)
        ->execute();
      $changed = TRUE;
    }
  }
  if (isset($remove_aids)) {
    $select = \Drupal::database()
      ->select('uc_attribute_options', 'ao')
      ->fields('ao', [
      'oid',
    ])
      ->condition('ao.aid', $remove_aids, 'IN');
    \Drupal::database()
      ->delete($this->optionTable)
      ->condition('oid', $select, 'IN')
      ->condition($this->idField, $this->idValue)
      ->execute();
    \Drupal::database()
      ->delete($this->attributeTable)
      ->condition($this->idField, $this->idValue)
      ->condition('aid', $remove_aids, 'IN')
      ->execute();
    $this
      ->attributesRemoved();
    $this
      ->messenger()
      ->addMessage($this
      ->formatPlural(count($remove_aids), '1 attribute has been removed.', '@count attributes have been removed.'));
  }
  if ($changed) {
    $this
      ->messenger()
      ->addMessage($this
      ->t('The changes have been saved.'));
  }
}