You are here

public function GeneralNumberWithBarIndicatorFormatter::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 GeneralNumberFormatter::settingsForm

File

src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php, line 156

Class

GeneralNumberWithBarIndicatorFormatter
Presents an integer as a labeled horizontal bar of varying length.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

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

  // Get the parent's form, which includes a lot of settings for
  // formatting numbers.
  $elements = parent::settingsForm($form, $formState);

  // Add warning if min/max are not set.
  $fieldSettings = $this
    ->getFieldSettings();
  $min = $fieldSettings['min'];
  $max = $fieldSettings['max'];
  $disabled = FALSE;
  if (isset($min) === FALSE || isset($max) === FALSE) {
    $disabled = TRUE;
    $elements['warning'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t("To enable horizontal bar display, first set the minimum and maximum in the field's definition."),
      '#weight' => -999,
      '#attributes' => [
        'class' => [
          'formatter_suite-settings-warning',
        ],
      ],
    ];
  }
  $weight = 100;

  // Prompt for each setting.
  $elements['sectionBreak'] = [
    '#markup' => '<div class="formatter_suite-section-break"></div>',
    '#weight' => $weight++,
  ];
  $elements['barLength'] = [
    '#title' => $this
      ->t('Max bar length'),
    '#type' => 'number',
    '#min' => 1,
    '#max' => 5000,
    '#size' => 5,
    '#default_value' => $this
      ->getSetting('barLength'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-bar-indicator-bar-length',
      ],
    ],
  ];
  $elements['barWidth'] = [
    '#title' => $this
      ->t('Bar width'),
    '#type' => 'number',
    '#min' => 1,
    '#max' => 5000,
    '#size' => 5,
    '#default_value' => $this
      ->getSetting('barWidth'),
    '#description' => $this
      ->t('Bar length and width in pixels.'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-bar-indicator-bar-width',
      ],
    ],
  ];
  $elements['barColor'] = [
    '#title' => $this
      ->t('Bar color'),
    '#type' => 'textfield',
    '#size' => 7,
    '#default_value' => $this
      ->getSetting('barColor'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#attributes' => [
      'autocomplete' => 'off',
      'autocapitalize' => 'none',
      'spellcheck' => 'false',
      'autocorrect' => 'off',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-bar-indicator-bar-color',
      ],
    ],
  ];
  $elements['backgroundColor'] = [
    '#title' => $this
      ->t('Background color'),
    '#type' => 'textfield',
    '#size' => 7,
    '#default_value' => $this
      ->getSetting('backgroundColor'),
    '#description' => $this
      ->t("Colors use CSS syntax (e.g. '#ff0000'). Empty background uses page's background."),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#attributes' => [
      'autocomplete' => 'off',
      'autocapitalize' => 'none',
      'spellcheck' => 'false',
      'autocorrect' => 'off',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-bar-indicator-background-color',
      ],
    ],
  ];
  $elements['valueLocation'] = [
    '#title' => $this
      ->t('Value location'),
    '#type' => 'select',
    '#options' => $this
      ->getValueLocations(),
    '#default_value' => $this
      ->getSetting('valueLocation'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-bar-indicator-value-location',
      ],
    ],
  ];
  return $elements;
}