You are here

public function GeneralNumberWithMinMaxFormatter::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/GeneralNumberWithMinMaxFormatter.php, line 147

Class

GeneralNumberWithMinMaxFormatter
Formats with a variety of notation styles and includes field min/max.

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);

  // Remove the parent's prefix/suffix support. This is handled explicitly
  // by the format here.
  unset($elements['usePrefixAndSuffix']);

  // Add warning if min/max are not set.
  $fieldSettings = $this
    ->getFieldSettings();
  $min = $fieldSettings['min'];
  $max = $fieldSettings['max'];

  // Below, some checkboxes and select choices show/hide other form
  // elements. We use Drupal's obscure 'states' feature, which adds
  // Javascript to elements to auto show/hide based upon a set of
  // simple conditions.
  //
  // Those conditions need to reference the form elements to check
  // (e.g. a checkbox), but the element ID and name are automatically
  // generated by the parent form. We cannot set them, or predict them,
  // so we cannot use them. We could use a class, but this form may be
  // shown multiple times on the same page, so a simple class would not be
  // unique. Instead, we create classes for this form only by adding a
  // random number marker to the end of the class name.
  $marker = rand();
  $disabled = FALSE;
  if (isset($min) === FALSE && isset($max) === FALSE) {
    $disabled = TRUE;
    $elements['warning'] = [
      '#type' => 'html_tag',
      '#tag' => 'div',
      '#value' => $this
        ->t("To enable the display of a field's minimum and maximum, first set these in the field's definition."),
      '#weight' => -999,
      '#attributes' => [
        'class' => [
          'formatter_suite-settings-warning',
        ],
      ],
    ];
  }
  $weight = -100;

  // Prompt for each setting.
  $elements['commonFormat'] = [
    '#title' => $this
      ->t('Min/max format:'),
    '#type' => 'select',
    '#options' => $this
      ->getCommonFormats(),
    '#default_value' => $this
      ->getSetting('commonFormat'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-minmax-common-format',
      ],
    ],
    '#attributes' => [
      'class' => [
        'commonFormat-' . $marker,
      ],
    ],
  ];
  $elements['customFormat'] = [
    '#type' => 'textfield',
    '#title' => $this
      ->t('Custom:'),
    '#size' => 30,
    '#default_value' => $this
      ->getSetting('customFormat'),
    '#description' => $this
      ->t('The symbols @value, @min, @max, @prefix, and @suffix are replaced with the field\'s value, and parameters from the field\'s definition. For example, "@value out of @max" formats as "5 out of 10".'),
    '#disabled' => $disabled,
    '#weight' => $weight++,
    '#attributes' => [
      'autocomplete' => 'off',
      'autocapitalize' => 'none',
      'spellcheck' => 'false',
      'autocorrect' => 'off',
    ],
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-with-minmax-custom-format',
      ],
    ],
    '#states' => [
      'visible' => [
        '.commonFormat-' . $marker => [
          'value' => 'custom',
        ],
      ],
    ],
  ];
  $elements['sectionBreak'] = [
    '#markup' => '<div class="formatter_suite-section-break"></div>',
    '#weight' => $weight++,
  ];
  return $elements;
}