You are here

public function AddressFieldsForm::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 ConfigFormBase::buildForm

File

uc_store/src/Form/AddressFieldsForm.php, line 32

Class

AddressFieldsForm
Configure address field settings for this store.

Namespace

Drupal\uc_store\Form

Code

public function buildForm(array $form, FormStateInterface $form_state) {
  $config = $this
    ->config('uc_store.settings')
    ->get('address_fields');
  $form['fields'] = [
    '#type' => 'table',
    '#header' => [
      $this
        ->t('Field'),
      $this
        ->t('Required'),
      $this
        ->t('List position'),
    ],
    '#tabledrag' => [
      [
        'action' => 'order',
        'relationship' => 'sibling',
        'group' => 'uc-store-address-fields-weight',
      ],
    ],
  ];
  $fields = [
    'first_name' => $this
      ->t('First name'),
    'last_name' => $this
      ->t('Last name'),
    'company' => $this
      ->t('Company'),
    'street1' => $this
      ->t('Street address 1'),
    'street2' => $this
      ->t('Street address 2'),
    'city' => $this
      ->t('City'),
    'zone' => $this
      ->t('State/Province'),
    'country' => $this
      ->t('Country'),
    'postal_code' => $this
      ->t('Postal code'),
    'phone' => $this
      ->t('Phone number'),
  ];
  foreach ($fields as $field => $label) {
    $form['fields'][$field]['#attributes']['class'][] = 'draggable';
    $form['fields'][$field]['#weight'] = $config[$field]['weight'];
    $form['fields'][$field]['status'] = [
      '#type' => 'checkbox',
      '#title' => $label,
      '#default_value' => $config[$field]['status'],
    ];
    $form['fields'][$field]['required'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('@title is required', [
        '@title' => $label,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $config[$field]['required'],
    ];
    $form['fields'][$field]['weight'] = [
      '#type' => 'weight',
      '#title' => $this
        ->t('Weight for @title', [
        '@title' => $label,
      ]),
      '#title_display' => 'invisible',
      '#default_value' => $config[$field]['weight'],
      '#attributes' => [
        'class' => [
          'uc-store-address-fields-weight',
        ],
      ],
    ];
  }
  uasort($form['fields'], 'Drupal\\Component\\Utility\\SortArray::sortByWeightProperty');
  return parent::buildForm($form, $form_state);
}