class TimeZoneResolver

Same name in other branches
  1. 9 core/modules/system/src/TimeZoneResolver.php \Drupal\system\TimeZoneResolver
  2. 8.9.x core/modules/system/src/TimeZoneResolver.php \Drupal\system\TimeZoneResolver
  3. 10 core/modules/system/src/TimeZoneResolver.php \Drupal\system\TimeZoneResolver

Event handler that resolves time zone based on site and user configuration.

Sets the time zone using date_default_timezone_set().

Hierarchy

  • class \Drupal\system\TimeZoneResolver implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of TimeZoneResolver

See also

date_default_timezone_set()

1 string reference to 'TimeZoneResolver'
system.services.yml in core/modules/system/system.services.yml
core/modules/system/system.services.yml
1 service uses TimeZoneResolver
system.timezone_resolver in core/modules/system/system.services.yml
Drupal\system\TimeZoneResolver

File

core/modules/system/src/TimeZoneResolver.php, line 20

Namespace

Drupal\system
View source
class TimeZoneResolver implements EventSubscriberInterface {
    
    /**
     * The config.
     *
     * @var \Drupal\Core\Config\ConfigFactoryInterface
     */
    protected $configFactory;
    
    /**
     * The current user.
     *
     * @var \Drupal\Core\Session\AccountInterface
     */
    private $currentUser;
    
    /**
     * TimeZoneResolver constructor.
     *
     * @param \Drupal\Core\Session\AccountInterface $current_user
     *   The current user.
     * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
     *   The config factory.
     */
    public function __construct(AccountInterface $current_user, ConfigFactoryInterface $config_factory) {
        $this->configFactory = $config_factory;
        $this->currentUser = $current_user;
    }
    
    /**
     * Sets the default time zone.
     */
    public function setDefaultTimeZone() {
        if ($time_zone = $this->getTimeZone()) {
            date_default_timezone_set($time_zone);
        }
    }
    
    /**
     * Updates the default time zone when time zone config changes.
     *
     * @param \Drupal\Core\Config\ConfigCrudEvent $event
     *   The config crud event.
     */
    public function onConfigSave(ConfigCrudEvent $event) {
        $saved_config = $event->getConfig();
        if ($saved_config->getName() === 'system.date' && ($event->isChanged('timezone.default') || $event->isChanged('timezone.user.configurable'))) {
            $this->setDefaultTimeZone();
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public static function getSubscribedEvents() : array {
        $events[ConfigEvents::SAVE][] = [
            'onConfigSave',
            0,
        ];
        // The priority for this must run directly after the authentication
        // subscriber.
        $events[KernelEvents::REQUEST][] = [
            'setDefaultTimeZone',
            299,
        ];
        $events[AccountEvents::SET_USER][] = [
            'setDefaultTimeZone',
        ];
        return $events;
    }
    
    /**
     * Gets the time zone based on site and user configuration.
     *
     * @return string|null
     *   The time zone, or NULL if nothing is set.
     */
    protected function getTimeZone() {
        $config = $this->configFactory
            ->get('system.date');
        if ($config->get('timezone.user.configurable') && $this->currentUser
            ->isAuthenticated() && $this->currentUser
            ->getTimezone()) {
            return $this->currentUser
                ->getTimeZone();
        }
        elseif ($default_timezone = $config->get('timezone.default')) {
            return $default_timezone;
        }
        return NULL;
    }

}

Members

Title Sort descending Modifiers Object type Summary
TimeZoneResolver::$configFactory protected property The config.
TimeZoneResolver::$currentUser private property The current user.
TimeZoneResolver::getSubscribedEvents public static function
TimeZoneResolver::getTimeZone protected function Gets the time zone based on site and user configuration.
TimeZoneResolver::onConfigSave public function Updates the default time zone when time zone config changes.
TimeZoneResolver::setDefaultTimeZone public function Sets the default time zone.
TimeZoneResolver::__construct public function TimeZoneResolver constructor.

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