欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

drupal將Date表單元素月日年的順序改造為年月日的方法

  發(fā)布時(shí)間:2014-11-04 17:29:56   作者:佚名   我要評(píng)論
這篇文章主要為大家介紹了drupal將Date表單元素月日年的順序改造為年月日的方法,是很多drupal用戶在進(jìn)行二次開發(fā)的時(shí)候都會(huì)遇到的問題,需要的朋友可以參考下

本文實(shí)例講述了drupal將Date表單元素月日年的順序改造為年月日的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

我們?cè)诒韱卧刂?很多時(shí)候都會(huì)使用date這樣的元素,但是,你會(huì)發(fā)現(xiàn),這個(gè)元素由3個(gè)select組成,他們的順序?yàn)樵拢?,年,這是不符合中國(guó)的習(xí)慣的。

我在幫人指導(dǎo)建站的時(shí)候,就遇到了這樣的問題,但是找不到答案。我判斷,可以通過theme層搞定這個(gè)問題,這樣就去找了對(duì)應(yīng)的theme函數(shù)。這個(gè)問題在網(wǎng)上好像是找不到答案的,我google了多次,但是都找不到,也有人遇到了同樣的問題,但是都是繞道而行。

我決定嘗試著解決這樣的問題,因?yàn)槲蚁嘈?,一定存在一個(gè)方法,將月日年的順序調(diào)整為年月日。先看系統(tǒng)生成的默認(rèn)元素里面的markup。然后就去找對(duì)應(yīng)的主題函數(shù),這樣就找到了theme_date。

具體代碼如下:

復(fù)制代碼
代碼如下:
function theme_date($element) {
return theme('form_element', $element, '<div class="container-inline">'. $element['#children'] .'</div>');
}

container-inline就是這里生成。但是3個(gè)子元素的順序不是這里決定的。我的第一個(gè)想法是,覆寫這個(gè)函數(shù),print_r($element['#children']),這樣就可以到定這個(gè)順序問題了。

不過我很想知道,核心代碼中,哪部分決定了3個(gè)子元素的順序,這樣,就找到了expand_date($element)。其具體代碼如下:

復(fù)制代碼
代碼如下:
function expand_date($element) {
// Default to current date
if (empty($element['#value'])) {
$element['#value'] = array('day' => format_date(time(), 'custom', 'j'),
'month' => format_date(time(), 'custom', 'n'),
'year' => format_date(time(), 'custom', 'Y'));
}

$element['#tree'] = TRUE;

// Determine the order of day, month, year in the site's chosen date format.此處決定日期格式:
$format = variable_get('date_format_short', 'm/d/Y - H:i');
$sort = array();
$sort['day'] = max(strpos($format, 'd'), strpos($format, 'j'));
$sort['month'] = max(strpos($format, 'm'), strpos($format, 'M'));
$sort['year'] = strpos($format, 'Y');
asort($sort);
$order = array_keys($sort);

// Output multi-selector for date.
foreach ($order as $type) {
switch ($type) {
case 'day':
$options = drupal_map_assoc(range(1, 31));
break;
case 'month':
$options = drupal_map_assoc(range(1, 12), 'map_month');
break;
case 'year':
$options = drupal_map_assoc(range(1900, 2050));
break;
}
$parents = $element['#parents'];
$parents[] = $type;
$element[$type] = array(
'#type' => 'select',
'#value' => $element['#value'][$type],
'#attributes' => $element['#attributes'],
'#options' => $options,
);
}

return $element;
}

注意代碼注釋說明的部分,3個(gè)子元素的順序,是由日期格式?jīng)Q定的,我猜測(cè),調(diào)整日期格式,就可以改變3個(gè)子元素的順序,我嘗試著將日期格式都改為了年月日:導(dǎo)航到admin/settings/date-time,將3中長(zhǎng),中,短的日期格式都調(diào)整為自定義格式,Y/m/d(D) H:i。

這樣,date元素中的順序,就從“月,日,年”調(diào)整為了“年,月,日”。

希望本文所述對(duì)大家的drupal二次開發(fā)有所幫助。

相關(guān)文章

最新評(píng)論