You are here

public function BulkSkuWidget::settingsForm in Commerce Bulk 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 StringTextfieldWidget::settingsForm

File

src/Plugin/Field/FieldWidget/BulkSkuWidget.php, line 44

Class

BulkSkuWidget
Plugin implementation of the 'commerce_bulk_sku' widget.

Namespace

Drupal\commerce_bulk\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $none = $this
    ->t('None');
  $settings = $this
    ->getSettings();
  $element['custom_label'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom label'),
    '#description' => $this
      ->t('The label for the SKU field displayed on a variation edit form.'),
    '#default_value' => empty($settings['custom_label']) ? '' : $settings['custom_label'],
    '#placeholder' => $none,
  ];
  $element['uniqid_enabled'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Enable unique auto SKU values generation'),
    '#default_value' => $settings['uniqid_enabled'],
  ];
  $element['more_entropy'] = [
    '#type' => 'checkbox',
    '#title_display' => 'before',
    '#title' => $this
      ->t('More unique'),
    '#description' => $this
      ->t('If unchecked the SKU (without prefix and suffix) will look like this: <strong>@short</strong>. If checked, like this: <strong>@long</strong>. <a href=":uniqid_href" target="_blank">Read more</a>', [
      ':uniqid_href' => 'http://php.net/manual/en/function.uniqid.php',
      '@short' => uniqid(),
      '@long' => uniqid('', TRUE),
    ]),
    '#default_value' => $settings['more_entropy'],
    '#states' => [
      'visible' => [
        ':input[name*="uniqid_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['hide'] = [
    '#type' => 'checkbox',
    '#title_display' => 'before',
    '#title' => $this
      ->t('Hide SKU'),
    '#description' => $this
      ->t('Hide the SKU field on a product add/edit forms adding SKU values silently at the background.'),
    '#default_value' => $settings['hide'],
    '#states' => [
      'visible' => [
        ':input[name*="uniqid_enabled"]' => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];
  $element['prefix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('SKU prefix'),
    '#default_value' => $settings['prefix'],
    '#placeholder' => $none,
  ];
  $element['suffix'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('SKU suffix'),
    '#default_value' => $settings['suffix'],
    '#placeholder' => $none,
    '#description' => $this
      ->t('Note if you leave all the above settings empty some services will become unavailable. For example, bulk creation of variations will be disabled on a product add or edit form.'),
  ];
  $element['size'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Size of SKU field'),
    '#default_value' => $settings['size'],
    '#required' => TRUE,
    '#min' => 1,
  ];
  $element['placeholder'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Placeholder'),
    '#default_value' => $settings['placeholder'],
    '#description' => $this
      ->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
    '#placeholder' => $none,
  ];
  $element['maximum'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Maximum'),
    '#default_value' => $settings['maximum'],
    '#description' => $this
      ->t('The maximum of SKU values that might be generated in one go. Use it if you have troubles with bulk creation of variations on a product add or edit form. Helps to create a great number of variations by pressing <strong>Create N variations</strong> button several times. Note that <strong>the actual maximum of created values may differ</strong> as it depends on the number of attributes. Start from the minimum 3 SKU values to calculate the desired maximum.'),
    '#required' => TRUE,
    '#step' => 1,
    '#min' => 3,
  ];
  return $element;
}