function TimestampFormatter::settingsForm

Same name and namespace in other branches
  1. 9 core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm()
  2. 8.9.x core/lib/Drupal/Core/Field/Plugin/Field/FieldFormatter/TimestampFormatter.php \Drupal\Core\Field\Plugin\Field\FieldFormatter\TimestampFormatter::settingsForm()
  3. 10 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 122

Class

TimestampFormatter
Plugin implementation of the 'timestamp' formatter.

Namespace

Drupal\Core\Field\Plugin\Field\FieldFormatter

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
    $form = parent::settingsForm($form, $form_state);
    $date_formats = [];
    $requestTime = $this->time
        ->getRequestTime();
    foreach ($this->dateFormatStorage
        ->loadMultiple() as $machine_name => $value) {
        $date_formats[$machine_name] = $this->t('@name format: @date', [
            '@name' => $value->label(),
            '@date' => $this->dateFormatter
                ->format($requestTime, $machine_name),
        ]);
    }
    $date_formats[static::CUSTOM_DATE_FORMAT] = $this->t('Custom');
    $time_diff = $this->getSetting('time_diff');
    $form['time_diff']['#tree'] = TRUE;
    $form['time_diff']['enabled'] = [
        '#type' => 'checkbox',
        '#title' => $this->t("Display as a time difference (e.g. '6 months ago')"),
        '#default_value' => $time_diff['enabled'],
    ];
    $states = $this->buildStates([
        'time_diff',
        'enabled',
    ], [
        'checked' => TRUE,
    ]);
    $form['time_diff']['future_format'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Future format'),
        '#description' => $this->t("Use the <code>@interval</code> placeholder to represent the formatted time difference interval. E.g. <code>@interval hence</code> will be displayed as <em>2 hours 5 minutes hence</em>."),
        '#default_value' => $time_diff['future_format'],
        '#states' => $states,
    ];
    $form['time_diff']['past_format'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Past format'),
        '#description' => $this->t("Use the <code>@interval</code> placeholder to represent the formatted time difference interval. E.g. <code>@interval ago</code> will be displayed as <em>2 hours 5 minutes ago</em>."),
        '#default_value' => $time_diff['past_format'],
        '#states' => $states,
    ];
    $form['time_diff']['granularity'] = [
        '#type' => 'select',
        '#title' => $this->t('Time units'),
        '#description' => $this->t("How many time units will be used in formatting the time difference. For example, if '1' is selected then the displayed time difference will only contain a single time unit such as '2 years' or '5 minutes' never '2 years 3 months' or '5 minutes 8 seconds'."),
        '#default_value' => $time_diff['granularity'],
        '#options' => array_combine(range(1, 7), range(1, 7)),
        '#states' => $states,
    ];
    $form['time_diff']['refresh'] = [
        '#type' => 'select',
        '#title' => $this->t('Refresh interval'),
        '#description' => $this->t('How often to refresh the displayed time difference. The time difference is refreshed on client-side, by JavaScript, without reloading the page.'),
        '#default_value' => $time_diff['refresh'],
        '#options' => $this->getRefreshIntervals(),
        '#states' => $states,
    ];
    $form['time_diff']['description'] = [
        '#type' => 'item',
        '#title' => $this->t('Fallback configuration'),
        '#description' => $this->t('The configuration below is used as a fallback when JavaScript is not available on the page.'),
        '#states' => $states,
    ];
    $form['date_format'] = [
        '#type' => 'select',
        '#title' => $this->t('Date format'),
        '#options' => $date_formats,
        '#default_value' => $this->getSetting('date_format'),
    ];
    $form['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'),
        '#states' => $this->buildStates([
            'date_format',
        ], [
            'value' => static::CUSTOM_DATE_FORMAT,
        ]),
    ];
    $form['timezone'] = [
        '#type' => 'select',
        '#title' => $this->t('Time zone'),
        '#options' => [
            '' => $this->t('- Default site/user time zone -'),
        ] + TimeZoneFormHelper::getOptionsListByRegion(),
        '#default_value' => $this->getSetting('timezone'),
    ];
    $tooltip = $this->getSetting('tooltip');
    $form['tooltip']['#tree'] = TRUE;
    $form['tooltip']['date_format'] = [
        '#type' => 'select',
        '#title' => $this->t('Tooltip date format'),
        '#description' => $this->t('Select the date format to be used for the title and displayed on mouse hover.'),
        '#options' => $date_formats,
        '#default_value' => $tooltip['date_format'],
        '#empty_option' => $this->t('- No tooltip -'),
    ];
    $form['tooltip']['custom_date_format'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Tooltip custom date format'),
        '#description' => $this->t('See <a href="http://php.net/manual/function.date.php" target="_blank">the documentation for PHP date formats</a>.'),
        '#default_value' => $tooltip['custom_date_format'],
        '#states' => $this->buildStates([
            'tooltip',
            'date_format',
        ], [
            'value' => static::CUSTOM_DATE_FORMAT,
        ]),
    ];
    return $form;
}

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