本文整理汇总了PHP中drupal_get_user_timezone函数的典型用法代码示例。如果您正苦于以下问题:PHP drupal_get_user_timezone函数的具体用法?PHP drupal_get_user_timezone怎么用?PHP drupal_get_user_timezone使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了drupal_get_user_timezone函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: serveCachedPage
public function serveCachedPage()
{
$cache_enabled = $this->getCacheMode();
// If there is no session cookie and cache is enabled (or forced), try
// to serve a cached page.
if (!isset($_COOKIE[session_name()]) && $cache_enabled) {
global $user;
// Make sure there is a user object because its timestamp will be
// checked, hook_boot might check for anonymous user etc.
$user = drupal_anonymous_user();
// Get the page from the cache.
$cache = drupal_page_get_cache();
// If there is a cached page, display it.
if (is_object($cache)) {
header('X-Drupal-Cache: HIT');
// Restore the metadata cached with the page.
$_GET['q'] = $cache->data['path'];
drupal_set_title($cache->data['title'], PASS_THROUGH);
date_default_timezone_set(drupal_get_user_timezone());
// If the skipping of the bootstrap hooks is not enforced, call
// hook_boot.
if (variable_get('page_cache_invoke_hooks', TRUE)) {
bootstrap_invoke_all('boot');
}
drupal_serve_page_from_cache($cache);
// If the skipping of the bootstrap hooks is not enforced, call
// hook_exit.
if (variable_get('page_cache_invoke_hooks', TRUE)) {
bootstrap_invoke_all('exit');
}
// We are done.
exit;
} else {
header('X-Drupal-Cache: MISS');
}
}
}
示例2: formElement
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
$element['start_date'] = array('#type' => 'date', '#title' => t('Start date'), '#default_value' => $items[$delta]->start_date, '#date_increment' => 1, '#date_timezone' => drupal_get_user_timezone());
$element['end_date'] = array('#type' => 'date', '#title' => t('End date'), '#default_value' => $items[$delta]->end_date, '#date_increment' => 1, '#date_timezone' => drupal_get_user_timezone());
$element['state'] = array('#type' => 'select', '#title' => t('State'), '#options' => array('paid-leave' => 'paid-leave', 'unpaid-leave' => 'unpaid-leave'), '#default_value' => $items[$delta]->state);
return $element;
}
示例3: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
{
// We are nesting some sub-elements inside the parent, so we need a wrapper.
// We also need to add another #title attribute at the top level for ease in
// identifying this item in error messages. We do not want to display this
// title because the actual title display is handled at a higher level by
// the Field module.
$element['#theme_wrappers'][] = 'datetime_wrapper';
$element['#attributes']['class'][] = 'container-inline';
$element['value'] = array('#type' => 'datetime', '#default_value' => NULL, '#date_increment' => 1, '#date_timezone' => drupal_get_user_timezone(), '#required' => $element['#required']);
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date-only field should have no timezone conversion performed, so
// use the same timezone as for storage.
$element['value']['#date_timezone'] = DATETIME_STORAGE_TIMEZONE;
}
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
$element['value']['#default_value'] = $date;
}
return $element;
}
示例4: prepareTimezone
/**
* Overrides prepareTimezone().
*
* Override basic component timezone handling to use Drupal's
* knowledge of the preferred user timezone.
*/
protected function prepareTimezone($timezone)
{
if (empty($timezone)) {
// Fallback to user or system default timezone.
$timezone = drupal_get_user_timezone();
}
return parent::prepareTimezone($timezone);
}
示例5: refreshUser
/**
* {@inheritdoc}
*/
public function refreshUser(UserInterface $user)
{
if (!$user instanceof User) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
$GLOBALS['user'] = $user->getDrupalUser();
date_default_timezone_set(drupal_get_user_timezone());
return $this->loadUserByUsername($user->getUsername());
}
示例6: setAccount
/**
* {@inheritdoc}
*/
public function setAccount(AccountInterface $account)
{
// If the passed account is already proxied, use the actual account instead
// to prevent loops.
if ($account instanceof static) {
$account = $account->getAccount();
}
$this->account = $account;
date_default_timezone_set(drupal_get_user_timezone());
}
示例7: getStartTimeStamp
/**
* Getter for the start timestamp.
*
* @param int $index
* The index of the field value to be retrieved. Defaults to 0.
*
* @return int
* The start date as a UNIX timestamp.
*/
protected function getStartTimeStamp($index = 0)
{
$value = $this->fieldItemList->getValue()[$index]['value'];
$field_def = $this->fieldItemList->getFieldDefinition();
$field_type = $field_def->getFieldStorageDefinition()->getType();
if ($field_type == 'datetime') {
/** @var \Drupal/datetime\Plugin\FieldType\DateTimeItem $field */
$field = $this->fieldItemList->get($index);
// Set User's Timezone
$field->date->setTimezone(timezone_open(drupal_get_user_timezone()));
// Format to timestamp.
return $field->date->format('U');
}
return (int) $value;
}
示例8: onKernelRequestAuthenticate
/**
* Authenticates user on request.
*
* @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
* The request event.
*
* @see \Drupal\Core\Authentication\AuthenticationProviderInterface::authenticate()
*/
public function onKernelRequestAuthenticate(GetResponseEvent $event)
{
if ($event->getRequestType() === HttpKernelInterface::MASTER_REQUEST) {
$request = $event->getRequest();
if ($this->authenticationProvider->applies($request)) {
$account = $this->authenticationProvider->authenticate($request);
if ($account) {
$this->accountProxy->setAccount($account);
return;
}
}
// No account has been set explicitly, initialize the timezone here.
date_default_timezone_set(drupal_get_user_timezone());
}
}
示例9: formElement
/**
* {@inheritdoc}
*/
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, array &$form_state)
{
// We are nesting some sub-elements inside the parent, so we need a wrapper.
// We also need to add another #title attribute at the top level for ease in
// identifying this item in error messages. We do not want to display this
// title because the actual title display is handled at a higher level by
// the Field module.
$element['#theme_wrappers'][] = 'datetime_wrapper';
$element['#attributes']['class'][] = 'container-inline';
$element['#element_validate'][] = 'datetime_datetime_widget_validate';
// Identify the type of date and time elements to use.
switch ($this->getFieldSetting('datetime_type')) {
case DateTimeItem::DATETIME_TYPE_DATE:
$date_type = 'date';
$time_type = 'none';
$date_format = $this->dateStorage->load('html_date')->getPattern();
$time_format = '';
$element_format = $date_format;
$storage_format = DATETIME_DATE_STORAGE_FORMAT;
break;
default:
$date_type = 'date';
$time_type = 'time';
$date_format = $this->dateStorage->load('html_date')->getPattern();
$time_format = $this->dateStorage->load('html_time')->getPattern();
$element_format = $date_format . ' ' . $time_format;
$storage_format = DATETIME_DATETIME_STORAGE_FORMAT;
break;
}
$element['value'] = array('#type' => 'datetime', '#default_value' => NULL, '#date_increment' => 1, '#date_date_format' => $date_format, '#date_date_element' => $date_type, '#date_date_callbacks' => array(), '#date_time_format' => $time_format, '#date_time_element' => $time_type, '#date_time_callbacks' => array(), '#date_timezone' => drupal_get_user_timezone(), '#required' => $element['#required']);
// Set the storage and widget options so the validation can use them. The
// validator will not have access to the field definition.
$element['value']['#date_element_format'] = $element_format;
$element['value']['#date_storage_format'] = $storage_format;
if ($items[$delta]->date) {
$date = $items[$delta]->date;
// The date was created and verified during field_load(), so it is safe to
// use without further inspection.
$date->setTimezone(new \DateTimeZone($element['value']['#date_timezone']));
if ($this->getFieldSetting('datetime_type') == DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time will pick up the current time, use the default
// time.
datetime_date_default_time($date);
}
$element['value']['#default_value'] = $date;
}
return $element;
}
示例10: viewElements
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items)
{
$elements = array();
foreach ($items as $delta => $item) {
$output = '';
if (!empty($item->date)) {
// The date was created and verified during field_load(), so it is safe
// to use without further inspection.
$date = $item->date;
$date->setTimeZone(timezone_open(drupal_get_user_timezone()));
$format = DATETIME_DATETIME_STORAGE_FORMAT;
if ($this->getFieldSetting('datetime_type') == 'date') {
// A date without time will pick up the current time, use the default.
datetime_date_default_time($date);
$format = DATETIME_DATE_STORAGE_FORMAT;
}
$output = $date->format($format);
}
$elements[$delta] = array('#markup' => $output);
}
return $elements;
}
示例11: processDefaultValue
/**
* {@inheritdoc}
*/
public static function processDefaultValue($default_value, FieldableEntityInterface $entity, FieldDefinitionInterface $definition)
{
$default_value = parent::processDefaultValue($default_value, $entity, $definition);
if (isset($default_value[0]['default_date_type'])) {
if ($definition->getSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// A default date only value should be in the format used for date
// storage but in the user's local timezone.
$date = new DrupalDateTime($default_value[0]['default_date'], drupal_get_user_timezone());
$format = DATETIME_DATE_STORAGE_FORMAT;
} else {
// A default date+time value should be in the format and timezone used
// for date storage.
$date = new DrupalDateTime($default_value[0]['default_date'], DATETIME_STORAGE_TIMEZONE);
$format = DATETIME_DATETIME_STORAGE_FORMAT;
}
$value = $date->format($format);
// We only provide a default value for the first item, as do all fields.
// Otherwise, there is no way to clear out unwanted values on multiple value
// fields.
$default_value = array(array('value' => $value, 'date' => $date));
}
return $default_value;
}
示例12: processDatetime
//.........这里部分代码省略.........
* a list of the possible formats and HTML5 standards for the HTML5
* requirements. Defaults to the right HTML5 format for the chosen element
* if a HTML5 element is used, otherwise defaults to
* entity_load('date_format', 'html_time')->getPattern().
* - #date_time_callbacks: An array of optional callbacks for the time
* element. Can be used to add a jQuery timepicker or an 'All day' checkbox.
* - #date_year_range: A description of the range of years to allow, like
* '1900:2050', '-3:+3' or '2000:+3', where the first value describes the
* earliest year and the second the latest year in the range. A year
* in either position means that specific year. A +/- value describes a
* dynamic value that is that many years earlier or later than the current
* year at the time the form is displayed. Used in jQueryUI datepicker year
* range and HTML5 min/max date settings. Defaults to '1900:2050'.
* - #date_increment: The increment to use for minutes and seconds, i.e.
* '15' would show only :00, :15, :30 and :45. Used for HTML5 step values and
* jQueryUI datepicker settings. Defaults to 1 to show every minute.
* - #date_timezone: The local timezone to use when creating dates. Generally
* this should be left empty and it will be set correctly for the user using
* the form. Useful if the default value is empty to designate a desired
* timezone for dates created in form processing. If a default date is
* provided, this value will be ignored, the timezone in the default date
* takes precedence. Defaults to the value returned by
* drupal_get_user_timezone().
*
* Example usage:
* @code
* $form = array(
* '#type' => 'datetime',
* '#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
* '#date_date_element' => 'date',
* '#date_time_element' => 'none',
* '#date_year_range' => '2010:+3',
* );
* @endcode
*
* @param array $element
* The form element whose value is being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
* The form element whose value has been processed.
*/
public static function processDatetime(&$element, FormStateInterface $form_state, &$complete_form)
{
$format_settings = array();
// The value callback has populated the #value array.
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
// Set a fallback timezone.
if ($date instanceof DrupalDateTime) {
$element['#date_timezone'] = $date->getTimezone()->getName();
} elseif (empty($element['#timezone'])) {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
if ($element['#date_date_element'] != 'none') {
$date_format = $element['#date_date_element'] != 'none' ? static::getHtml5DateFormat($element) : '';
$date_value = !empty($date) ? $date->format($date_format, $format_settings) : $element['#value']['date'];
// Creating format examples on every individual date item is messy, and
// placeholders are invalid for HTML5 date and datetime, so an example
// format is appended to the title to appear in tooltips.
$extra_attributes = array('title' => t('Date (e.g. @format)', array('@format' => static::formatExample($date_format))), 'type' => $element['#date_date_element']);
// Adds the HTML5 date attributes.
if ($date instanceof DrupalDateTime && !$date->hasErrors()) {
$html5_min = clone $date;
$range = static::datetimeRangeYears($element['#date_year_range'], $date);
$html5_min->setDate($range[0], 1, 1)->setTime(0, 0, 0);
$html5_max = clone $date;
$html5_max->setDate($range[1], 12, 31)->setTime(23, 59, 59);
$extra_attributes += array('min' => $html5_min->format($date_format, $format_settings), 'max' => $html5_max->format($date_format, $format_settings));
}
$element['date'] = array('#type' => 'date', '#title' => t('Date'), '#title_display' => 'invisible', '#value' => $date_value, '#attributes' => $element['#attributes'] + $extra_attributes, '#required' => $element['#required'], '#size' => max(12, strlen($element['#value']['date'])), '#error_no_message' => TRUE, '#date_date_format' => $element['#date_date_format']);
// Allows custom callbacks to alter the element.
if (!empty($element['#date_date_callbacks'])) {
foreach ($element['#date_date_callbacks'] as $callback) {
if (function_exists($callback)) {
$callback($element, $form_state, $date);
}
}
}
}
if ($element['#date_time_element'] != 'none') {
$time_format = $element['#date_time_element'] != 'none' ? static::getHtml5TimeFormat($element) : '';
$time_value = !empty($date) ? $date->format($time_format, $format_settings) : $element['#value']['time'];
// Adds the HTML5 attributes.
$extra_attributes = array('title' => t('Time (e.g. @format)', array('@format' => static::formatExample($time_format))), 'type' => $element['#date_time_element'], 'step' => $element['#date_increment']);
$element['time'] = array('#type' => 'date', '#title' => t('Time'), '#title_display' => 'invisible', '#value' => $time_value, '#attributes' => $element['#attributes'] + $extra_attributes, '#required' => $element['#required'], '#size' => 12, '#error_no_message' => TRUE);
// Allows custom callbacks to alter the element.
if (!empty($element['#date_time_callbacks'])) {
foreach ($element['#date_time_callbacks'] as $callback) {
if (function_exists($callback)) {
$callback($element, $form_state, $date);
}
}
}
}
return $element;
}
示例13: setTimeZone
/**
* Sets the proper time zone on a DrupalDateTime object for the current user.
*
* A DrupalDateTime object loaded from the database will have the UTC time
* zone applied to it. This method will apply the time zone for the current
* user, based on system and user settings.
*
* @see drupal_get_user_timezone()
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A DrupalDateTime object.
*/
protected function setTimeZone(DrupalDateTime $date)
{
$date->setTimeZone(timezone_open(drupal_get_user_timezone()));
}
示例14: processDatelist
/**
* Expands a date element into an array of individual elements.
*
* Required settings:
* - #default_value: A DrupalDateTime object, adjusted to the proper local
* timezone. Converting a date stored in the database from UTC to the local
* zone and converting it back to UTC before storing it is not handled here.
* This element accepts a date as the default value, and then converts the
* user input strings back into a new date object on submission. No timezone
* adjustment is performed.
* Optional properties include:
* - #date_part_order: Array of date parts indicating the parts and order
* that should be used in the selector, optionally including 'ampm' for
* 12 hour time. Default is array('year', 'month', 'day', 'hour', 'minute').
* - #date_text_parts: Array of date parts that should be presented as
* text fields instead of drop-down selectors. Default is an empty array.
* - #date_date_callbacks: Array of optional callbacks for the date element.
* - #date_year_range: A description of the range of years to allow, like
* '1900:2050', '-3:+3' or '2000:+3', where the first value describes the
* earliest year and the second the latest year in the range. A year
* in either position means that specific year. A +/- value describes a
* dynamic value that is that many years earlier or later than the current
* year at the time the form is displayed. Defaults to '1900:2050'.
* - #date_increment: The increment to use for minutes and seconds, i.e.
* '15' would show only :00, :15, :30 and :45. Defaults to 1 to show every
* minute.
* - #date_timezone: The local timezone to use when creating dates. Generally
* this should be left empty and it will be set correctly for the user using
* the form. Useful if the default value is empty to designate a desired
* timezone for dates created in form processing. If a default date is
* provided, this value will be ignored, the timezone in the default date
* takes precedence. Defaults to the value returned by
* drupal_get_user_timezone().
*
* Example usage:
* @code
* $form = array(
* '#type' => 'datelist',
* '#default_value' => new DrupalDateTime('2000-01-01 00:00:00'),
* '#date_part_order' => array('month', 'day', 'year', 'hour', 'minute', 'ampm'),
* '#date_text_parts' => array('year'),
* '#date_year_range' => '2010:2020',
* '#date_increment' => 15,
* );
* @endcode
*
* @param array $element
* The form element whose value is being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $complete_form
* The complete form structure.
*
* @return array
*/
public static function processDatelist(&$element, FormStateInterface $form_state, &$complete_form)
{
// Load translated date part labels from the appropriate calendar plugin.
$date_helper = new DateHelper();
// The value callback has populated the #value array.
$date = !empty($element['#value']['object']) ? $element['#value']['object'] : NULL;
// Set a fallback timezone.
if ($date instanceof DrupalDateTime) {
$element['#date_timezone'] = $date->getTimezone()->getName();
} elseif (!empty($element['#timezone'])) {
$element['#date_timezone'] = $element['#date_timezone'];
} else {
$element['#date_timezone'] = drupal_get_user_timezone();
}
$element['#tree'] = TRUE;
// Determine the order of the date elements.
$order = !empty($element['#date_part_order']) ? $element['#date_part_order'] : array('year', 'month', 'day');
$text_parts = !empty($element['#date_text_parts']) ? $element['#date_text_parts'] : array();
// Output multi-selector for date.
foreach ($order as $part) {
switch ($part) {
case 'day':
$options = $date_helper->days($element['#required']);
$format = 'j';
$title = t('Day');
break;
case 'month':
$options = $date_helper->monthNamesAbbr($element['#required']);
$format = 'n';
$title = t('Month');
break;
case 'year':
$range = static::datetimeRangeYears($element['#date_year_range'], $date);
$options = $date_helper->years($range[0], $range[1], $element['#required']);
$format = 'Y';
$title = t('Year');
break;
case 'hour':
$format = in_array('ampm', $element['#date_part_order']) ? 'g' : 'G';
$options = $date_helper->hours($format, $element['#required']);
$title = t('Hour');
break;
case 'minute':
$format = 'i';
$options = $date_helper->minutes($format, $element['#required'], $element['#date_increment']);
//.........这里部分代码省略.........
示例15: setTimeZone
/**
* Sets the proper time zone on a DrupalDateTime object for the current user.
*
* A DrupalDateTime object loaded from the database will have the UTC time
* zone applied to it. This method will apply the time zone for the current
* user, based on system and user settings.
*
* @see drupal_get_user_timezone()
*
* @param \Drupal\Core\Datetime\DrupalDateTime $date
* A DrupalDateTime object.
*/
protected function setTimeZone(DrupalDateTime $date)
{
if ($this->getFieldSetting('datetime_type') === DateTimeItem::DATETIME_TYPE_DATE) {
// A date without time has no timezone conversion.
$timezone = DATETIME_STORAGE_TIMEZONE;
} else {
$timezone = drupal_get_user_timezone();
}
$date->setTimeZone(timezone_open($timezone));
}