You are here

public function DurationHumanDisplayFormatter::settingsForm in Duration Field 3.0.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php \Drupal\duration_field\Plugin\Field\FieldFormatter\DurationHumanDisplayFormatter::settingsForm()
  2. 8 src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php \Drupal\duration_field\Plugin\Field\FieldFormatter\DurationHumanDisplayFormatter::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

File

src/Plugin/Field/FieldFormatter/DurationHumanDisplayFormatter.php, line 139

Class

DurationHumanDisplayFormatter
Provides a human friendly formatter for the Duration field type.

Namespace

Drupal\duration_field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element['text_length'] = [
    '#title' => $this
      ->t('Text length'),
    '#type' => 'select',
    '#options' => [
      'full' => $this
        ->getHumanFriendlyLabel('full'),
      'short' => $this
        ->getHumanFriendlyLabel('short'),
    ],
    '#default_value' => $this
      ->getSetting('text_length'),
  ];
  $custom_separators = $this->moduleHandler
    ->invokeAll('duration_field_separators');
  $custom_separator_mappings = [];
  foreach (array_keys($custom_separators) as $custom_separator) {
    $custom_separator_mappings[$custom_separator] = $this
      ->getHumanFriendlyLabel($custom_separator);
  }
  $element['separator'] = [
    '#title' => $this
      ->t('Separator'),
    '#type' => 'select',
    '#options' => [
      'space' => $this
        ->getHumanFriendlyLabel('space'),
      'hyphen' => $this
        ->getHumanFriendlyLabel('hyphen'),
      'comma' => $this
        ->getHumanFriendlyLabel('comma'),
      'newline' => $this
        ->getHumanFriendlyLabel('newline'),
    ] + $custom_separator_mappings,
    '#default_value' => $this
      ->getSetting('separator'),
  ];
  return $element;
}