[SCMP] Interests - International property
中文版: http://drupaltaiwan.org/forum/20101026/4655
http://interests.scmp.com/international-property/
This is actually not the first project I build for SCMP
But it will be a good summary for my works on Drupal for first half of 2010
Site structure
- 3 level navigation – frontpage, taxonomy(countries), articles
- 5 content types:
- articles
- featured content
- images in gallery
- static page
- useful resources
Frontpage structure
Frontpage is build within one views, which contain 5 main parts (display)
- views::attachment Featured
- a maximum of 5 images rotation
- use jQuery Galleriffic plugin
- content type featured content
- sponsored articles
- recent uploaded articles
- All content “images”
- Upon click, larger image appears (by fancybox)
- list of all articles in the site
- order by regions of articles
- complex views templates (remark1)
Technical highlights
- Images in gallery as a standalone content type
- Every image have several size to display, extensive use of imagefield, imagefield_crop, imagecache
- Articles need to link the images to itself to display images in gallery section
- Single image can be reused over articles
- In image popup, “related articles” can be show if another article also use this image (remark2)
Remark1: complex views templates
Difficulties:
- Articles will be unpublished/published on scheduled date
- Country list is dynamic, only display a country if the country have a published article belongs to
- The display is 3 column, with dynamic number of rows, as the number of displayed country is dynamic
- The country list is ordered by country name
SQL through views (simplified sample):
- SELECT country_name, node_title FROM `nodes` JOIN taxonomy WHERE nodes.tid = taxonomy.tid GROUP BY country_name ORDER BY country_name;
Result set will be like:
- Canada, title1
- Canada, title2
- China, title3
- France, title4
- USA, title5
- …
Calculations:
- number of country in each column (2,1,1)
- count current country
- print country name when needed
- open/close a column when needed
Remarks2: related articles of images
- A views that take the image node id as argument
- And find articles that node reference is image node id
- If second argument is given, remove second argument from result set, so in article page, “related articles” do not show itself
[inmediahk] inmediahk.net/m 手機版
自從香港獨立媒體搬了 server 之後,
其中一個需要增加的功能就是手機版
之前一直沒有下手做的原因是因為要另寫一個theme
手機版用的 theme, 雖然很可能會用上 jqtouch 一類現成的庫
但需要再多一個模組分析 user-agent
很可能需要 themekey
便覺得牽一髮而動全身
但看到最近 jquery 自己推出了 jquery mobile
又經過更多 theming 的磨練之後覺得其實不需要使用一個新 theme
另做一個手機版入口就可以了
這樣便成就了 inmediahk.net/m
loading 頁面
入口很簡單, 用 views 另做一個 page
從 page-m.tpl.php 放 jquery mobile 就可以了
問題是內頁不可以方便的 detect 到 user-agent
便在 mobile page 的 link 都加上 ?v=m
在 template.php 的 node preprocess 加一組 template_files:
<?php
function phptemplate_preprocess_node(&$vars) {
if($_GET['v']=='m') {
$vars['template_files'][] = 'node-m';
}
}
?>
便可以令 mobile 的內頁使用 node-m.tpl.php, 可以使用 jquery mobile 的 html 了
但目前還沒有完成 taxonomy page 的 手機版
技術上類似, 只是需要不急的話就先將就用了...
內頁
Attachment | Size |
---|---|
inmediahk.net_.m.articles.png | 42.54 KB |
inmediahk.net_.m.frontpage.png | 65.83 KB |
inmediahk.net_.m.loading.png | 32.79 KB |
Drupal contact form use tpl.php 將聯絡表單的內容移到 tpl.php
其實我做的是一個contact module 的擴充, cck 的 email field
它可以建立一個送到特定 email 欄的聯絡表單, 是建基於內建的聯絡表單的擴展
而我需要將它的 html 完全改變, contact form 的修改應該很類似
步驟:
先在theme 注冊一個使用的 theme hook:
<?php
//template.php
mytheme_theme() {
$hooks['email_mail_page_form'] = array(
'template' => 'email-mail-page-form',
'arguments' => array('form' => NULL),
// other theme registration code...
);
return $hooks;
}
?>
然後在 theme 內新建一個 email-mail-page-form.tpl.php:
//email-mail-page-form.tpl.php
<?php
kprint_r($form);
?>
但和前文所記, 如果需要使用 hook_form_alter() 的話
便需要自建一個 module, 而不可以好像上例般直接在 theme 內修改
建立一個自定義的區塊模組, 管理複雜的內容和權限設定 custom block module for complex block content and visability
專為右欄複雜的內容和複雜的顯示設定而建立一個模組管理有幾個好處:
1. 返回NULL 便可以令系統跳過
很方便的就可以處理不顯示的狀態
先檢查顯示與否, 不顯示或權限不足便返回 NULL
2. 源碼管理軟件 (scm) 可以助管理
傳統的 add block 方法將block 內容和顯示的邏輯放到資料庫
不利於 scm 管理
而且使用文件的方式儲存代碼更加有更新容易, 除錯容易等等的優點
身為開發者, 而不是使用者, 有責任為網站提供一個更容易維護的環境
3. 避免將錯誤的PHP 放到資料庫
在顯示設定很多時候都會用到 PHP code
而錯誤的 syntax error 的 PHP code 放到資料庫,
會為頁面不能顯示, 也不可以使用 web 介面修改
要到資料庫中修改, 很麻煩
build a module only for complex blocks have several pros:
1. simple "do not display"
check the visability first,
just return nothing for empty blocks, or access denied cases
system will skip them.
2. Source control management (scm) can help
traditional "add block" method put block content and related visability settings in database
which do not fit with scm.
The use of a module file to store code promotes later updates, easier debugging
Also, as a developer, but not a user, there is a responsibility to provide a better maintainable environment.
3. avoid DB crash upon error php code in database
in visability, PHP is often used
and the syntax errored PHP code will also got stored, and crash the page.
You cannot correct it through web interface,
but have to open a database connection and edit them, very trouble
Move / convert drupal multi site to separate instances (從 multi sites 分成多個 Drupal)
ok, multi sites is just too difficult to manage
我承認 multi sites 是太難管理了, 它帶來的好處比不上它的壞處:
1. file paths are crazy
2. upgrade and development of one of the multi sites are potentially dangerous
1. 檔案路徑太混亂
2. 為其中一個site 的模組升級和開發有可能影響到其他 sites, 需要很小心
so steps to convert multi sites settings to separate Drupal instance:
要從 multi sites 分成多個 Drupal:
1. copy files into separate sub folders
2. edit each sub folder's settings file to point to correct db and use correct base path
3. maybe change the file paths of css and images in themes
4. turn off rewrite rules
5. delete old files, except folders named "files"
1. 複製原本的檔案到新建的子資料夾
2. 修改每個子資料夾的 settings.php 以使用正確的 DB 和 base path
3. 選擇性的修改 themes 內的 css 和圖片路徑
4. 拿掉 rewrite
5. 移除舊體制的資料夾, 除了以 "files" 來命名的資料夾
it is not very painful and difficult, but absolutely worth to test before do it in live sites
雖然這不是一個複雜的工作, 但都强烈建議先測試才在 live 環境工作