You are here

protected function DateRecurModularUtilityTrait::getDefaultTimeZone in Recurring Date Field Modular Widgets 8

Same name and namespace in other branches
  1. 3.x src/DateRecurModularUtilityTrait.php \Drupal\date_recur_modular\DateRecurModularUtilityTrait::getDefaultTimeZone()
  2. 2.x src/DateRecurModularUtilityTrait.php \Drupal\date_recur_modular\DateRecurModularUtilityTrait::getDefaultTimeZone()

Determines a default time zone for a field item.

If the provided field item does not have time zone data, then a sensible time zone will be determined based on the current user and site configuration.

Parameters

\Drupal\date_recur\Plugin\Field\FieldType\DateRecurItem $item: A date recur field item.

Return value

string A time zone.

3 calls to DateRecurModularUtilityTrait::getDefaultTimeZone()
DateRecurModularAlphaWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularAlphaWidget.php
Returns the form for a single field widget.
DateRecurModularOscarWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularOscarWidget.php
Returns the form for a single field widget.
DateRecurModularSierraWidget::formElement in src/Plugin/Field/FieldWidget/DateRecurModularSierraWidget.php
Returns the form for a single field widget.

File

src/DateRecurModularUtilityTrait.php, line 110

Class

DateRecurModularUtilityTrait
Trait containing convenience methods for dealing with date recur widgets.

Namespace

Drupal\date_recur_modular

Code

protected function getDefaultTimeZone(DateRecurItem $item) : string {
  assert($this->fieldDefinition instanceof FieldDefinitionInterface);
  $defaultTimeZone = $item->timezone ?? NULL;
  if (!$defaultTimeZone && empty($item
    ->getValue())) {

    // Handily set default value to the field settings default time zone if
    // value is empty. This typically happens if entity is new or this is a
    // blank extra field value.
    $defaultTimeZone = $this->fieldDefinition
      ->getDefaultValueLiteral()[0]['default_time_zone'] ?? NULL;
  }

  // If still blank then use current user time zone.
  if (empty($defaultTimeZone)) {
    $defaultTimeZone = $this
      ->getCurrentUserTimeZone();
  }
  return $defaultTimeZone;
}