You are here

public function Base::settingsForm in Double Field 4.x

Same name and namespace in other branches
  1. 8.3 src/Plugin/Field/FieldFormatter/Base.php \Drupal\double_field\Plugin\Field\FieldFormatter\Base::settingsForm()

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

3 calls to Base::settingsForm()
Details::settingsForm in src/Plugin/Field/FieldFormatter/Details.php
Returns a form to configure settings for the formatter.
ListBase::settingsForm in src/Plugin/Field/FieldFormatter/ListBase.php
Returns a form to configure settings for the formatter.
Table::settingsForm in src/Plugin/Field/FieldFormatter/Table.php
Returns a form to configure settings for the formatter.
3 methods override Base::settingsForm()
Details::settingsForm in src/Plugin/Field/FieldFormatter/Details.php
Returns a form to configure settings for the formatter.
ListBase::settingsForm in src/Plugin/Field/FieldFormatter/ListBase.php
Returns a form to configure settings for the formatter.
Table::settingsForm in src/Plugin/Field/FieldFormatter/Table.php
Returns a form to configure settings for the formatter.

File

src/Plugin/Field/FieldFormatter/Base.php, line 51

Class

Base
Base class for Double field formatters.

Namespace

Drupal\double_field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) : array {
  $settings = $this
    ->getSettings();
  $field_settings = $this
    ->getFieldSettings();
  $types = DoubleFieldItem::subfieldTypes();
  $element = [];

  // General settings.
  foreach ([
    'first',
    'second',
  ] as $subfield) {
    $type = $field_settings['storage'][$subfield]['type'];
    $title = $subfield == 'first' ? $this
      ->t('First subfield') : $this
      ->t('Second subfield');
    $title .= ' - ' . $types[$type];
    if ($field_settings[$subfield]['list']) {
      $title .= ' (' . $this
        ->t('list') . ')';
    }
    $element[$subfield] = [
      '#title' => $title,
      '#type' => 'details',
    ];
    $element[$subfield]['link'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Display as link'),
      '#default_value' => $settings[$subfield]['link'],
      '#weight' => -10,
      '#access' => in_array($type, static::$linkTypes),
    ];
    if ($type == 'datetime_iso8601') {
      $format_types = DateFormat::loadMultiple();
      $date_formatter = static::getDateFormatter();
      $time = new DrupalDateTime();
      $options = [];
      foreach ($format_types as $type => $type_info) {
        $format = $date_formatter
          ->format($time
          ->getTimestamp(), $type);
        $options[$type] = $type_info
          ->label() . ' (' . $format . ')';
      }
      $element[$subfield]['format_type'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Date format'),
        '#description' => $this
          ->t('Choose a format for displaying the date.'),
        '#options' => $options,
        '#default_value' => $settings[$subfield]['format_type'],
      ];
    }
    else {
      $element[$subfield]['format_type'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['format_type'],
      ];
    }
    $element[$subfield]['hidden'] = [
      '#type' => 'checkbox',
      '#title' => $this
        ->t('Hidden'),
      '#default_value' => $settings[$subfield]['hidden'],
    ];
    if ($field_settings[$subfield]['list']) {
      $element[$subfield]['key'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Display key instead of label'),
        // @todo Remove the fallback on 5.x.
        '#default_value' => $settings[$subfield]['key'] ?? FALSE,
      ];
    }
    if ($type == 'numeric' || $type == 'float' || $type == 'integer') {
      $options = [
        '' => $this
          ->t('- None -'),
        '.' => $this
          ->t('Decimal point'),
        ',' => $this
          ->t('Comma'),
        ' ' => $this
          ->t('Space'),
        chr(8201) => $this
          ->t('Thin space'),
        "'" => $this
          ->t('Apostrophe'),
      ];
      $element[$subfield]['thousand_separator'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Thousand marker'),
        '#options' => $options,
        '#default_value' => $settings[$subfield]['thousand_separator'],
      ];
    }
    else {
      $element[$subfield]['thousand_separator'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['thousand_separator'],
      ];
    }
    if ($type == 'numeric' || $type == 'float') {
      $element[$subfield]['decimal_separator'] = [
        '#type' => 'select',
        '#title' => $this
          ->t('Decimal marker'),
        '#options' => [
          '.' => $this
            ->t('Decimal point'),
          ',' => $this
            ->t('Comma'),
        ],
        '#default_value' => $settings[$subfield]['decimal_separator'],
      ];
      $element[$subfield]['scale'] = [
        '#type' => 'number',
        '#title' => $this
          ->t('Scale', [], [
          'context' => 'decimal places',
        ]),
        '#min' => 0,
        '#max' => 10,
        '#default_value' => $settings[$subfield]['scale'],
        '#description' => $this
          ->t('The number of digits to the right of the decimal.'),
      ];
    }
    else {
      $element[$subfield]['decimal_separator'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['decimal_separator'],
      ];
      $element[$subfield]['scale'] = [
        '#type' => 'value',
        '#default_value' => $settings[$subfield]['scale'],
      ];
    }
  }
  return $element;
}