function TimestampFormatter::settingsForm

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm()
  2. 10 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm()
  3. 11.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm()

Overrides FormatterBase::settingsForm

File

core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php, line 102

Class

TimestampFormatter
Plugin implementation of the 'timestamp' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $date_formats = [];
    foreach ($this->dateFormatStorage
        ->loadMultiple() as $machine_name => $value) {
        $date_formats[$machine_name] = $this->t('@name format: @date', [
            '@name' => $value->label(),
            '@date' => $this->dateFormatter
                ->format(REQUEST_TIME, $machine_name),
        ]);
    }
    $date_formats['custom'] = $this->t('Custom');
    $elements['date_format'] = [
        '#type' => 'select',
        '#title' => $this->t('Date format'),
        '#options' => $date_formats,
        '#default_value' => $this->getSetting('date_format') ?: 'medium',
    ];
    $elements['custom_date_format'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Custom date format'),
        '#description' => $this->t('See <a href="https://www.php.net/manual/datetime.format.php#refsect1-datetime.format-parameters" target="_blank">the documentation for PHP date formats</a>.'),
        '#default_value' => $this->getSetting('custom_date_format') ?: '',
    ];
    $elements['custom_date_format']['#states']['visible'][] = [
        ':input[name="fields[' . $this->fieldDefinition
            ->getName() . '][settings_edit_form][settings][date_format]"]' => [
            'value' => 'custom',
        ],
    ];
    $elements['timezone'] = [
        '#type' => 'select',
        '#title' => $this->t('Time zone'),
        '#options' => [
            '' => $this->t('- Default site/user time zone -'),
        ] + system_time_zones(FALSE, TRUE),
        '#default_value' => $this->getSetting('timezone'),
    ];
    return $elements;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.