Drupal 7 theming a node form

我慢慢發現 content type node form 是繼 exposed views form 之後最常要修改的 form
(我明明有寫過 alter exposed views form, 但找不到...)

特定 content type 本身是沒有 theme function 的
先定義一個,順便定義使用一個 tpl 檔輸出

<?php
/**
 * Implements hook_theme().
 */
function MYMODULE_theme($existing, $type, $theme, $path) {

  return array(
   
'article_node_form' => array(
     
'render element' => 'form',
     
'template' => 'article-node-form',
     
// this will set to module/theme path by default:
     
'path' => drupal_get_path('module', 'MYMODULE'),
    ),
  );
}
?>

接著便可以使用 preprocess 加入其他需要的 variables

<?php
/**
 * Preprocessor for theme('article_node_form').
 */
function template_preprocess_article_node_form(&$variables) {

 
// nodeformcols is an alternative for this solution.
 
if (!module_exists('nodeformcols')) {

   
$variables['sidebar'] = array();   // Put taxonomy fields in sidebar.

   
$variables['sidebar'][] = $variables['form']['field_tags'];
   
hide($variables['form']['field_tags']);

   
// Extract the form buttons, and put them in independent variable.
   
$variables['buttons'] = $variables['form']['actions'];
   
hide($variables['form']['actions']);
  }
}
?>

模組資料夾內的 article.tpl.php: (當然可以修改成你需要的樣子)

<?php echo drupal_render_children($form)?>

參考 http://drupal.org/node/1092122

Drupal7 loading gif

要重用 Drupal7 的 藍色㘣形 loading gif:

$('a').after('<div class="ajax-progress ajax-progress-throbber"><div class="throbber">&nbsp;</div></div>');

//... 移走
complete: function (jqXHR, textStatus) {
  $('.ajax-progress').remove();
}

JQuery toggle checkboxes

使用例子: 一個「全選」 checkbox 需要 toggle 它的子 checkbox,

jquery:

  var $checkbox = $(this).find(':checkbox');
  $checkbox.attr('checked', !$checkbox.attr('checked'));

Programmically add custom date filter to views

When programmically use views, two function is very important:

<?php
//get results only, i.e. return array of values
views_get_view_result($name,. $display_id);

//return themed html from views
views_embed_view($name,. $display_id);
?>

These two functions provide PHP developers more control over views.
By looking at the source code of them, you get the idea to $view object

So as views/date module do not yet provide a context filter to "larger than created date" (need review)
so I use some custom code to add custom filter to views:

<?php
// API: add_item($display_id, $type, $table, $field, $options = array(), $id = NULL)
// add custom filter programmically
$view->add_item($display_id, 'filter', 'node', 'created',
  array(
'value' => array('type' => 'date', 'value' => date('c', strtotime($strtotime_string))), 'operator' => '>='));
?>

在台灣換錢大法,隨想

這次去台灣有另一個發現
是有關在台灣換台幣的

台灣銀行是國定的外匯中心
星期一至五九時至四時半提供兌換台幣的服務
匯率也是正常的
但旅遊在外可不容易在這些時間到銀行換錢
畢竟觀光點的開放時間都是一至五,九至六

我星期五晚上到台灣桃園機場
身上只有一千多台幣
要付到巿中心的車有餘
但第二天是星期六銀行不開門啊
就在入境大樓外的兌換店換了錢
但擔心匯率差,只換了一點點
事後發現匯率也很貼近銀行的

第二天星期六我們發現了另一個星期六都可以換錢的地方
原本是想看看台北車站這個旅客中心有沒有找換店
但原來台灣的郵局是星期六都開放
而且是可以換錢的,又是半官方機構便換多了點錢

台灣的 7﹣11 或便利店絕對是值得台灣人驕傲的地方
有免費的 wifi 使用,有最多款的飲品,最多的即食面,最多的分店
我不在飯店時的 maps 都是 7-11 提供的
還有,7-11 是提供銀聯櫃員機的
我把著試試看的心態,使用恆生銀行的銀聯卡,兌換了一千港幣
也是我幾次在台換錢匯率最好的!
我事後看到匯率也嚇了一跳
那我以後都不用預先換台幣了
甚至不需要到銀行
只要到 7-11 就可以了,也不需要擔心匯率的問題
也不需要擔心身上有太多現金了

Pages

Google