你是對的,hook_form_alter()是一個好的開始。如果您正在修改內容類型表單,我曾經使用的一種方法是創建一個非常小且簡單的自定義模塊,實現hook_form_alter()。關於創建這個模塊的細節/說明可以在下面找到。
作爲一個例子,我打電話給這個模塊'custom_countries',如果你想改變名稱,你可以隨時重命名文件並做一個搜索並在以後替換它們。
首先,您需要在模塊文件夾(sites/all/modules
等)中創建一個新文件夾。 (從現在開始創建的所有文件都應放置在此文件夾中)。接下來,創建一個名爲custom_countries.info
新文件,並把下面的內部並保存:
name = "Custom Countries"
description = "Changes the list of countries available from Location module for certain content types"
core = 6.x
接下來,創建另一個文件名爲custom_countries.module
,將下面的代碼中並保存文件:
<?php
/**
* @file custom_countries.module
* Module to change the countries options of location module
* for certain content type(s)
*/
/**
* Implementation of hook_form_alter()
*/
function custom_countries_form_alter(&$form, $form_state, $form_id) {
// Replace "YOUR_CONTENT_TYPE with the name of the content type desired
if ($form_id == 'YOUR_CONTENT_TYPE_node_form') {
$form['#after_build'][] = 'custom_countries_after_build';
}
}
/**
* Make changes to countries field after all fields are rendered
*/
function custom_countries_after_build($form_element, &$form_state) {
// Replace FIELD_NAME with the machine name of the location field for your content type
$form_element[FIELD_NAME][0]['country']['#options'] = array(
'ca' => 'Canada',
'us' => 'United States',
);
return $form_element;
}
重要:務必閱讀註釋並將「YOUR_CONTENT_TYPE」更改爲您的位置字段所在內容類型的計算機名稱(如果使用默認content_profile設置,則可能只是「配置文件」)。另外,將「FIELD_NAME」更改爲位置字段的計算機名稱。
最後,啓用模塊admin/build/modules
。
現在,當您創建/編輯您指定的內容類型時,您只能在國家/地區列表中看到2個選項。使用這種方法,您現在可以輕鬆地對其他表單進行更改。
這個想法來自Make Location form fields available to hook_form_alter()。如果將來您決定添加其他國家/地區,則可以在http://api.lullabot.com/location_get_iso3166_list/5
完美。非常感謝你! – 2011-06-03 05:21:53