You are here

public function NumberWithBytesFormatter::settingsForm in Formatter Suite 8

Returns a form to configure settings for the formatter.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the formatter. 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 elements for the formatter settings.

Overrides FormatterBase::settingsForm

File

src/Plugin/Field/FieldFormatter/NumberWithBytesFormatter.php, line 121

Class

NumberWithBytesFormatter
Formats numbers with a byte suffix, like "bytes", "KB", or "MB".

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $formState) {

  // Get the parent's form.
  $elements = parent::settingsForm($form, $formState);

  // Add branding.
  $elements = Branding::addFieldFormatterBranding($elements);
  $elements['#attached']['library'][] = 'formatter_suite/formatter_suite.fieldformatter';

  // Add description.
  //
  // Use a large negative weight to insure it comes first.
  $elements['description'] = [
    '#type' => 'html_tag',
    '#tag' => 'div',
    '#value' => $this
      ->getDescription(),
    '#weight' => -1000,
    '#attributes' => [
      'class' => [
        'formatter_suite-settings-description',
      ],
    ],
  ];
  $weight = 0;

  // Prompt for each setting.
  $elements['kunit'] = [
    '#title' => $this
      ->t('Bytes units'),
    '#type' => 'select',
    '#options' => $this
      ->getKiloUnits(),
    '#default_value' => $this
      ->getSetting('kunit'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-number-with-bytes-kunit',
      ],
    ],
  ];
  $elements['fullWord'] = [
    '#title' => $this
      ->t('Use full words, not abbreviations (e.g. "Kilobyte" vs. "KB")'),
    '#type' => 'checkbox',
    '#default_value' => $this
      ->getSetting('fullWord'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-number-with-bytes-full-word',
      ],
    ],
  ];
  $elements['decimalDigits'] = [
    '#title' => $this
      ->t('Decimal digits'),
    '#type' => 'number',
    '#min' => 0,
    '#max' => 3,
    '#default_value' => $this
      ->getSetting('decimalDigits'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-number-with-bytes-decimal-digits',
      ],
    ],
  ];
  return $elements;
}