You are here

public function SmartDateTimezoneWidget::settingsForm in Smart Date 3.3.x

Same name and namespace in other branches
  1. 8.2 src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()
  2. 3.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()
  3. 3.0.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()
  4. 3.1.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()
  5. 3.2.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()
  6. 3.4.x src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php \Drupal\smart_date\Plugin\Field\FieldWidget\SmartDateTimezoneWidget::settingsForm()

Returns a form to configure settings for the widget.

Invoked from \Drupal\field_ui\Form\EntityDisplayFormBase to allow administrators to configure the widget. 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 definition for the widget settings.

Overrides SmartDateDefaultWidget::settingsForm

File

src/Plugin/Field/FieldWidget/SmartDateTimezoneWidget.php, line 71

Class

SmartDateTimezoneWidget
Plugin implementation of the 'smartdate_timezone' widget.

Namespace

Drupal\smart_date\Plugin\Field\FieldWidget

Code

public function settingsForm(array $form, FormStateInterface $form_state) {
  $element = parent::settingsForm($form, $form_state);
  $element['default_tz'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Default timezone'),
    '#default_value' => $this
      ->getSetting('default_tz'),
    '#options' => [
      '' => $this
        ->t('Site default (ignores any user override)'),
      'user' => $this
        ->t("User's timezone, defaulting to site (always saved)"),
      'custom' => $this
        ->t('A custom timezone (always saved)'),
    ],
  ];
  $custom_tz = $this
    ->getSetting('custom_tz') ? $this
    ->getSetting('custom_tz') : $this
    ->getSiteTimezone();
  $element['custom_tz'] = [
    '#type' => 'select',
    '#title' => $this
      ->t('Custom timezone'),
    '#default_value' => $custom_tz,
    '#options' => $this
      ->getTimezones(),
    '#states' => [
      // Show this select only if the 'default_tz' select is set to custom.
      'visible' => [
        'select[name$="[settings][default_tz]"]' => [
          'value' => 'custom',
        ],
      ],
    ],
  ];

  // Set default, based on field config.
  $default_label = t('- default: @tz_label -', [
    '@tz_label' => $this
      ->getSiteTimezone(),
  ]);
  switch ($this
    ->getSetting('default_tz')) {
    case '':
      $default_timezone = '';
      break;
    case 'user':
      $default_timezone = date_default_timezone_get();
      break;
    case 'custom':
      $default_timezone = $this
        ->getSetting('custom_tz');
      break;
  }
  $element['allowed_timezones']['#type'] = 'select';
  $element['allowed_timezones']['#multiple'] = TRUE;
  $element['allowed_timezones']['#options'] = $this
    ->getTimezones();
  $element['allowed_timezones']['#default_value'] = $this
    ->getSetting('allowed_timezones');
  $element['allowed_timezones']['#weight'] = 100;
  return $element;
}