2011-06-02 94 views
1

我一直在研究一種方法來限制contrib locations模塊附帶的下拉菜單中的可用國家。我認爲hook_form_alter是處理某些國家的方式,但從手頭開始hook_form_alter片段並不是我能夠實現的。經過大量的搜索,我一直無法找到一個代碼片段讓我開始。如何更改位置模塊中的國家/地區下拉菜單?

我現在正在從事的一個項目只允許來自美國和加拿大的註冊,所以我想將這個下拉限制在這兩個國家。調用國家/地區列表的函數是location_get_iso3166_list,數組是$ countries。位置模塊正用於填充內容配置文件模塊中的片段。

我在網上發現了一些帖子,建議只是評論.inc文件中不需要的國家......這不是該項目的一個選項,因爲我們正在進行多站點設置,所以在模塊中更改它會影響其他站點。我想我需要添加一個hook_form_alter片段到template.php

任何幫助,非常感謝。

謝謝! -Jeff

回答

0

你是對的,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

+0

完美。非常感謝你! – 2011-06-03 05:21:53

0

找到完整的鍵/值對列表。如果您使用的是Drupal 7,請編輯相關字段設置並限制後端設置中的國家/地區選項,結束。

相關問題