You are here

protected function ObjectAttributesAddFormBase::buildBaseForm in Ubercart 8.4

Constructs Attributes Add Form array on behalf of subclasses.

Parameters

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

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

Return value

array The form structure.

2 calls to ObjectAttributesAddFormBase::buildBaseForm()
ProductAttributesAddForm::buildForm in uc_attribute/src/Form/ProductAttributesAddForm.php
Form constructor.
ProductClassAttributesAddForm::buildForm in uc_attribute/src/Form/ProductClassAttributesAddForm.php
Form constructor.

File

uc_attribute/src/Form/ObjectAttributesAddFormBase.php, line 66

Class

ObjectAttributesAddFormBase
Defines the class/product attributes add form.

Namespace

Drupal\uc_attribute\Form

Code

protected function buildBaseForm(array $form, FormStateInterface $form_state) {
  $used_aids = [];
  foreach ($this->attributes as $attribute) {
    $used_aids[] = $attribute->aid;
  }
  $unused_attributes = [];
  $result = \Drupal::database()
    ->query("SELECT a.aid, a.name, a.label FROM {uc_attributes} a LEFT JOIN {uc_attribute_options} ao ON a.aid = ao.aid GROUP BY a.aid, a.name, a.label ORDER BY a.name");
  foreach ($result as $attribute) {
    if (!in_array($attribute->aid, $used_aids)) {
      $unused_attributes[$attribute->aid] = $attribute->name;
    }
  }
  $form['add_attributes'] = [
    '#type' => 'checkboxes',
    '#title' => $this
      ->t('Attributes'),
    '#options' => $unused_attributes ?: [
      $this
        ->t('No attributes left to add.'),
    ],
    '#disabled' => empty($unused_attributes),
  ];
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['add'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Add attributes'),
  ];
  return $form;
}