You are here

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

2 calls to GeneralNumberFormatter::settingsForm()
GeneralNumberWithBarIndicatorFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php
Returns a form to configure settings for the formatter.
GeneralNumberWithMinMaxFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralNumberWithMinMaxFormatter.php
Returns a form to configure settings for the formatter.
2 methods override GeneralNumberFormatter::settingsForm()
GeneralNumberWithBarIndicatorFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralNumberWithBarIndicatorFormatter.php
Returns a form to configure settings for the formatter.
GeneralNumberWithMinMaxFormatter::settingsForm in src/Plugin/Field/FieldFormatter/GeneralNumberWithMinMaxFormatter.php
Returns a form to configure settings for the formatter.

File

src/Plugin/Field/FieldFormatter/GeneralNumberFormatter.php, line 273

Class

GeneralNumberFormatter
Format a number field with a variety of notation styles and parameters.

Namespace

Drupal\formatter_suite\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $formState) {
  $this
    ->sanitizeSettings();
  $isMultiple = $this->fieldDefinition
    ->getFieldStorageDefinition()
    ->isMultiple();

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

  // Add branding.
  $elements = [];
  $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 notation and exponent styles.
  $elements['notationStyle'] = [
    '#title' => $this
      ->t('Notation style'),
    '#type' => 'select',
    '#options' => $this
      ->getNotationStyles(),
    '#default_value' => $this
      ->getSetting('notationStyle'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-notation-style',
      ],
    ],
    '#attributes' => [
      'class' => [
        'notationStyle-' . $marker,
      ],
    ],
  ];
  $elements['exponentStyle'] = [
    '#title' => $this
      ->t('Exponent style'),
    '#type' => 'select',
    '#options' => $this
      ->getExponentStyles(),
    '#default_value' => $this
      ->getSetting('exponentStyle'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-exponent-style',
      ],
    ],
    '#states' => [
      // Visible only if using "scientific" notation.
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'scientific',
        ],
      ],
    ],
  ];
  $elements['numberBase'] = [
    '#title' => $this
      ->t('Number base'),
    '#type' => 'number',
    '#min' => 2,
    '#max' => 36,
    '#default_value' => $this
      ->getSetting('numberBase'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-number-base',
      ],
    ],
    '#states' => [
      // Visible only if using "numeralsystem" notation.
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'numeralsystem',
        ],
      ],
    ],
  ];

  // Basics.
  $elements['decimalDigits'] = [
    '#title' => $this
      ->t('Decimal digits'),
    '#type' => 'number',
    '#min' => 0,
    '#max' => 10,
    '#default_value' => $this
      ->getSetting('decimalDigits'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-decimal-digits',
      ],
    ],
    '#states' => [
      // Invisible if using "numeralsystem" notation.
      'invisible' => [
        '.notationStyle-' . $marker => [
          'value' => 'numeralsystem',
        ],
      ],
    ],
  ];
  $elements['decimalSeparator'] = [
    '#title' => $this
      ->t('Separator'),
    '#type' => 'select',
    '#options' => $this
      ->getDecimalSeparators(),
    '#default_value' => $this
      ->getSetting('decimalSeparator'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-decimal-separator',
      ],
    ],
    '#states' => [
      // Visible if using "generalnumber" notation.
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'generalnumber',
        ],
      ],
    ],
  ];
  $elements['useThousands'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Show thousands separator'),
    '#default_value' => $this
      ->getSetting('useThousands'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-use-thousands-separator',
      ],
    ],
    '#attributes' => [
      'class' => [
        'useThousands-' . $marker,
      ],
    ],
    '#states' => [
      // Invisible if using "scientific" or "numeralsystem" notation.
      'invisible' => [
        [
          '.notationStyle-' . $marker => [
            'value' => 'scientific',
          ],
        ],
        [
          '.notationStyle-' . $marker => [
            'value' => 'numeralsystem',
          ],
        ],
      ],
    ],
  ];
  $elements['thousandsSeparator'] = [
    '#title' => $this
      ->t('Separator'),
    '#type' => 'select',
    '#options' => $this
      ->getThousandsSeparators(),
    '#default_value' => $this
      ->getSetting('thousandsSeparator'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-thousands-separator',
      ],
    ],
    '#states' => [
      // Visible if using "generalnumber" notation AND thousands enabled.
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'generalnumber',
        ],
        '.useThousands-' . $marker => [
          'checked' => TRUE,
        ],
      ],
    ],
  ];

  // Positive and negative styles.
  $elements['positiveStyle'] = [
    '#title' => $this
      ->t('Positive style'),
    '#type' => 'select',
    '#options' => $this
      ->getPositiveStyles(),
    '#default_value' => $this
      ->getSetting('positiveStyle'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-positive-style',
      ],
    ],
    '#states' => [
      // Visible if using "generalnumber".
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'generalnumber',
        ],
      ],
    ],
  ];
  $elements['negativeStyle'] = [
    '#title' => $this
      ->t('Negative style'),
    '#type' => 'select',
    '#options' => $this
      ->getNegativeStyles(),
    '#default_value' => $this
      ->getSetting('negativeStyle'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-negative-style',
      ],
    ],
    '#states' => [
      // Visible if using "generalnumber".
      'visible' => [
        '.notationStyle-' . $marker => [
          'value' => 'generalnumber',
        ],
      ],
    ],
  ];

  // Padding.
  $elements['useZeroPadding'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t('Pad with zeroes'),
    '#default_value' => $this
      ->getSetting('useZeroPadding'),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-use-zero-padding',
      ],
    ],
    '#attributes' => [
      'class' => [
        'useZeroPadding-' . $marker,
      ],
    ],
    '#states' => [
      // Visible if using 'generalnumber' notation style and thousands
      // enabled, OR if using 'numeralsystem' notation style.
      'visible' => [
        [
          '.notationStyle-' . $marker => [
            'value' => 'generalnumber',
          ],
          '.useThousands-' . $marker => [
            'unchecked' => TRUE,
          ],
        ],
        [
          '.notationStyle-' . $marker => [
            'value' => 'numeralsystem',
          ],
        ],
      ],
    ],
  ];
  $elements['paddingWidth'] = [
    '#type' => 'number',
    '#title' => $this
      ->t('Padding width'),
    '#default_value' => $this
      ->getSetting('paddingWidth'),
    '#min' => 0,
    '#max' => 40,
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-padding-width',
      ],
    ],
    '#attributes' => [
      'class' => [
        'formatter_suite-settings-indent',
      ],
    ],
    '#states' => [
      // Visible if using 'generalnumber' notation style and thousands
      // enabled and zero padding enabled, OR if using 'numeralsystem'
      // notation style and zero padding enabled.
      'visible' => [
        [
          '.notationStyle-' . $marker => [
            'value' => 'generalnumber',
          ],
          '.useThousands-' . $marker => [
            'unchecked' => TRUE,
          ],
          '.useZeroPadding-' . $marker => [
            'checked' => TRUE,
          ],
        ],
        [
          '.notationStyle-' . $marker => [
            'value' => 'numeralsystem',
          ],
          '.useZeroPadding-' . $marker => [
            'checked' => TRUE,
          ],
        ],
      ],
    ],
  ];

  // Prefix & suffix.
  $elements['usePrefixAndSuffix'] = [
    '#type' => 'checkbox',
    '#title' => $this
      ->t("Show field definition's prefix & suffix"),
    '#default_value' => $this
      ->getSetting('usePrefixAndSuffix'),
    '#description' => $this
      ->t("These may be currency symbols or units of measure. Set these on the field's definition."),
    '#weight' => $weight++,
    '#wrapper_attributes' => [
      'class' => [
        'formatter_suite-general-number-use-prefix-and-suffix',
      ],
    ],
  ];
  if ($isMultiple === TRUE) {
    $weight = 1000;
    $elements['sectionBreak3'] = [
      '#markup' => '<div class="formatter_suite-section-break"></div>',
      '#weight' => $weight++,
    ];
    $elements['listStyle'] = [
      '#title' => $this
        ->t('List style'),
      '#type' => 'select',
      '#options' => $this
        ->getListStyles(),
      '#default_value' => $this
        ->getSetting('listStyle'),
      '#weight' => $weight++,
      '#wrapper_attributes' => [
        'class' => [
          'formatter_suite-general-number-list-style',
        ],
      ],
      '#attributes' => [
        'class' => [
          'listStyle-' . $marker,
        ],
      ],
    ];
    $elements['listSeparator'] = [
      '#title' => $this
        ->t('Separator'),
      '#type' => 'textfield',
      '#size' => 10,
      '#default_value' => $this
        ->getSetting('listSeparator'),
      '#weight' => $weight++,
      '#attributes' => [
        'autocomplete' => 'off',
        'autocapitalize' => 'none',
        'spellcheck' => 'false',
        'autocorrect' => 'off',
      ],
      '#wrapper_attributes' => [
        'class' => [
          'formatter_suite-general-number-list-separator',
        ],
      ],
      '#states' => [
        'visible' => [
          '.listStyle-' . $marker => [
            'value' => 'span',
          ],
        ],
      ],
    ];
  }
  return $elements;
}