如何更改位置模块中的国家/地区下拉列表?

发布于 2024-11-13 03:08:53 字数 437 浏览 3 评论 0原文

我一直在研究一种方法来限制贡献位置模块附带的下拉列表中的可用国家/地区。我认为 hook_form_alter 是处理仅显示某些国家/地区的方法,但是从手动开始 hook_form_alter 片段并不是我有能力实现的。经过多次谷歌搜索后,我无法找到让我开始的代码片段。

我现在正在进行的一个项目只允许来自美国和加拿大的注册,所以我想将该下拉列表限制为仅这两个国家。调用国家列表的函数是location_get_iso3166_list,数组是$countries。位置模块用于填充内容配置文件模块中的片段。

我在网上发现了一些帖子,建议仅注释掉 .inc 文件中不需要的国家/地区...这不是该项目的选项,因为我们正在进行多站点设置,因此请更改它该模块会影响其他站点。我想我需要在 template.php 中添加一个 hook_form_alter 片段

非常感谢任何帮助。

谢谢你! -杰夫

I have been researching a way to limit the available countries in the drop-down that comes with the contrib locations module. I think hook_form_alter is the way to handle just showing certain countries, but starting a hook_form_alter snippet from hand is not something that I have the ability to achieve. After much googling I have not been able to find a code snippet to get me started.

A project I am working on now only allows registrations from the US and Canada, so I want to limit that drop-down to just those 2 countries. The function that calls the country list is location_get_iso3166_list and the array is $countries. The location module is being used to populate pieces in the Content Profile module.

I have found a couple posts online that suggest just commenting out the countries that are not needed in the .inc file...this is not an option for this project as we are on a multi-site set-up, so changing it in the module will affect other sites. I think I need to add a hook_form_alter snippet to the template.php

Any help is greatly appreciated.

Thank You!
-Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时间你老了 2024-11-20 03:08:53

你是对的,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 设置,则可能只是“profile”)。另外,将“FIELD_NAME”更改为位置字段的计算机名称。

最后,在 admin/build/modules 启用该模块。

现在,当您创建/编辑指定的内容类型时,您只会在国家/地区列表中看到 2 个选项。使用此方法,您现在也可以轻松地更改其他表单。

这个想法来自使位置表单字段可用于 hook_form_alter()。如果将来您决定添加其他国家/地区,可以在 http: //api.lullabot.com/location_get_iso3166_list/5

You are correct, hook_form_alter() is a good start. If you are looking to alter a content type form, one method I have used is to create a really small and simple custom module implementing hook_form_alter(). Details/instructions on creating this module can be found below.

As an example, I am calling this module 'custom_countries', if you want to change the name, you can always rename the files and do a search and replace in them later.

First you need to create a new folder in your modules folder (sites/all/modules, etc). (All files created from now on should be placed in this folder). Next, create a new file called custom_countries.info and put the following inside and save:

name = "Custom Countries"
description = "Changes the list of countries available from Location module for certain content types"
core = 6.x

Next, create another file called custom_countries.module, place the following code inside and save the file:

<?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;
}

Important: be sure to read the comments and change 'YOUR_CONTENT_TYPE' to the machine name of the content type your location field is in (possibly just 'profile' if using default content_profile settings). Also, change 'FIELD_NAME' to the machine name of the location field.

Finally, enable the module at admin/build/modules.

Now when you are creating/editing the content type you specified, you will only see 2 options in the countries list. Using this method, you can now easily make changes to other forms as well.

The idea for this came from Make Location form fields available to hook_form_alter(). If in the future you decide to add other countries, the complete list of key/value pairs can be found at http://api.lullabot.com/location_get_iso3166_list/5

べ映画 2024-11-20 03:08:53

如果您使用的是 Drupal 7,请编辑相关字段设置并从后端限制国家/地区选项。

If you are using Drupal 7, then edit the relevant field settings and restrict the country options from the back-end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文