You are here

public function ObjectOptionsFormBase::buildBaseForm in Ubercart 8.4

Constructs Options 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 ObjectOptionsFormBase::buildBaseForm()
ProductClassOptionsForm::buildForm in uc_attribute/src/Form/ProductClassOptionsForm.php
Form constructor.
ProductOptionsForm::buildForm in uc_attribute/src/Form/ProductOptionsForm.php
Form constructor.

File

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

Class

ObjectOptionsFormBase
Defines the class/product attributes options form.

Namespace

Drupal\uc_attribute\Form

Code

public function buildBaseForm(array $form, FormStateInterface $form_state) {
  $form['attributes']['#tree'] = TRUE;
  foreach ($this->attributes as $aid => $attribute) {
    $base_attr = uc_attribute_load($aid);
    if ($base_attr->options) {
      $form['attributes'][$aid]['options'] = [
        '#type' => 'table',
        '#header' => [
          $this
            ->t('Options'),
          $this
            ->t('Default'),
          $this
            ->t('Cost'),
          $this
            ->t('Price'),
          $this
            ->t('Weight'),
          $this
            ->t('List position'),
        ],
        '#caption' => [
          '#markup' => '<h2>' . $attribute->name . '</h2>',
        ],
        '#empty' => $this
          ->t('This attribute does not have any options.'),
        '#tabledrag' => [
          [
            'action' => 'order',
            'relationship' => 'sibling',
            'group' => 'uc-attribute-option-table-ordering',
          ],
        ],
      ];
      $query = \Drupal::database()
        ->select('uc_attribute_options', 'ao')
        ->fields('ao', [
        'aid',
        'oid',
        'name',
      ]);
      $query
        ->leftJoin($this->optionTable, 'po', "ao.oid = po.oid AND po." . $this->idField . " = :id", [
        ':id' => $this->idValue,
      ]);
      $query
        ->addField('ao', 'cost', 'default_cost');
      $query
        ->addField('ao', 'price', 'default_price');
      $query
        ->addField('ao', 'weight', 'default_weight');
      $query
        ->addField('ao', 'ordering', 'default_ordering');
      $query
        ->fields('po', [
        'cost',
        'price',
        'weight',
        'ordering',
      ])
        ->addExpression('CASE WHEN po.ordering IS NULL THEN 1 ELSE 0 END', 'null_order');
      $query
        ->condition('aid', $aid)
        ->orderBy('null_order')
        ->orderBy('po.ordering')
        ->orderBy('default_ordering')
        ->orderBy('ao.name');
      $result = $query
        ->execute();
      foreach ($result as $option) {
        $oid = $option->oid;
        $form['attributes'][$aid]['options'][$oid]['#attributes']['class'][] = 'draggable';
        $form['attributes'][$aid]['options'][$oid]['select'] = [
          '#type' => 'checkbox',
          '#title' => $option->name,
          '#default_value' => isset($attribute->options[$oid]),
        ];
        $form['attributes'][$aid]['options'][$oid]['default'] = [
          '#type' => 'radio',
          '#title' => $this
            ->t('Default'),
          '#title_display' => 'invisible',
          '#parents' => [
            'attributes',
            $aid,
            'default',
          ],
          '#return_value' => $oid,
          '#default_value' => $attribute->default_option,
        ];
        $form['attributes'][$aid]['options'][$oid]['cost'] = [
          '#type' => 'uc_price',
          '#title' => $this
            ->t('Cost'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->cost) ? $option->default_cost : $option->cost,
          '#size' => 6,
          '#allow_negative' => TRUE,
        ];
        $form['attributes'][$aid]['options'][$oid]['price'] = [
          '#type' => 'uc_price',
          '#title' => $this
            ->t('Price'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->price) ? $option->default_price : $option->price,
          '#size' => 6,
          '#allow_negative' => TRUE,
        ];
        $form['attributes'][$aid]['options'][$oid]['weight'] = [
          '#type' => 'textfield',
          '#title' => $this
            ->t('Weight'),
          '#title_display' => 'invisible',
          '#default_value' => is_null($option->weight) ? $option->default_weight : $option->weight,
          '#size' => 5,
        ];
        $form['attributes'][$aid]['options'][$oid]['ordering'] = [
          '#type' => 'weight',
          '#title' => $this
            ->t('List position'),
          '#title_display' => 'invisible',
          '#delta' => 50,
          '#default_value' => is_null($option->ordering) ? $option->default_ordering : $option->ordering,
          '#attributes' => [
            'class' => [
              'uc-attribute-option-table-ordering',
            ],
          ],
        ];
      }
    }
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save changes'),
  ];
  return $form;
}