You are here

public function AttributeOptionsForm::buildForm in Ubercart 8.4

Form constructor.

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.

Overrides FormInterface::buildForm

File

uc_attribute/src/Form/AttributeOptionsForm.php, line 24

Class

AttributeOptionsForm
Displays options and the modifications to products they represent.

Namespace

Drupal\uc_attribute\Form

Code

public function buildForm(array $form, FormStateInterface $form_state, $aid = NULL) {
  $attribute = uc_attribute_load($aid);
  $form['#title'] = $this
    ->t('Options for %name', [
    '%name' => $attribute->name,
  ]);
  $form['options'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Name'),
      $this
        ->t('Default cost'),
      $this
        ->t('Default price'),
      $this
        ->t('Default weight'),
      $this
        ->t('List position'),
      $this
        ->t('Operations'),
    ],
    '#empty' => $this
      ->t('No options for this attribute have been added yet.'),
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'uc-attribute-option-table-ordering',
      ],
    ],
  ];
  foreach ($attribute->options as $oid => $option) {
    $form['options'][$oid]['#attributes']['class'][] = 'draggable';
    $form['options'][$oid]['name'] = [
      '#markup' => $option->name,
    ];
    $form['options'][$oid]['cost'] = [
      '#theme' => 'uc_price',
      '#price' => $option->cost,
    ];
    $form['options'][$oid]['price'] = [
      '#theme' => 'uc_price',
      '#price' => $option->price,
    ];
    $form['options'][$oid]['weight'] = [
      '#markup' => (string) $option->weight,
    ];
    $form['options'][$oid]['ordering'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('List position'),
      '#title_display' => 'invisible',
      '#default_value' => $option->ordering,
      '#attributes' => [
        'class' => [
          'uc-attribute-option-table-ordering',
        ],
      ],
    ];
    $form['options'][$oid]['operations'] = [
      '#type' => 'operations',
      '#links' => [
        'edit' => [
          'title' => $this
            ->t('Edit'),
          'url' => Url::fromRoute('uc_attribute.option_edit', [
            'aid' => $attribute->aid,
            'oid' => $oid,
          ]),
        ],
        'delete' => [
          'title' => $this
            ->t('Delete'),
          'url' => Url::fromRoute('uc_attribute.option_delete', [
            'aid' => $attribute->aid,
            'oid' => $oid,
          ]),
        ],
      ],
    ];
  }
  $form['actions'] = [
    '#type' => 'actions',
  ];
  $form['actions']['submit'] = [
    '#type' => 'submit',
    '#value' => $this
      ->t('Save changes'),
  ];
  return $form;
}