You are here

public function StockLevelWidgetBase::settingsForm in Commerce Stock 8

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. The field_ui module takes care of handling submitted form values.

Parameters

array $form: The form where the settings form is being included in.

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

Return value

array The form definition for the widget settings.

Overrides WidgetBase::settingsForm

File

modules/field/src/Plugin/Field/FieldWidget/StockLevelWidgetBase.php, line 94

Class

StockLevelWidgetBase
Provides the base structure for commerce stock level widgets.

Namespace

Drupal\commerce_stock_field\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $field_name = $this->fieldDefinition
    ->getName();
  $element = [];
  if ($this
    ->hasHelpText()) {
    $element = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->getHelpText(),
    ];
  }
  $element['default_transaction_note'] = [
    '#title' => $this
      ->t('Default transaction note'),
    '#description' => $this
      ->t('Use this as default transaction note.'),
    '#type' => 'textfield',
    '#default_value' => $this
      ->getSetting('default_transaction_note'),
    '#size' => 50,
    '#maxlength' => 255,
  ];
  $element['custom_transaction_note'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow custom note per transaction.'),
    '#default_value' => $this
      ->getSetting('custom_transaction_note'),
  ];

  // Shameless borrowed from commerce quantity field.
  $step = $this
    ->getSetting('step');
  $element['#element_validate'][] = [
    get_class($this),
    'validateSettingsForm',
  ];
  $element['allow_decimal'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Allow decimal quantities'),
    '#default_value' => $step != '1',
  ];
  $element['step'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Step'),
    '#description' => $this
      ->t('Only stock levels that are multiples of the selected step will be allowed. Maximum precision is 2 (0.01).'),
    '#default_value' => $step != '1' ? $step : '0.1',
    '#options' => [
      '0.1' => '0.1',
      '0.01' => '0.01',
      '0.25' => '0.25',
      '0.5' => '0.5',
      '0.05' => '0.05',
    ],
    '#states' => [
      'visible' => [
        ':input[name="fields[' . $field_name . '][settings_edit_form][settings][allow_decimal]"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
    '#required' => TRUE,
  ];
  return $element;
}