Drupal 6:如何隐藏字段集以使其不显示在表单中?

发布于 2024-10-16 06:20:58 字数 115 浏览 6 评论 0原文

我已启用“位置用户”模块,并且在用户注册期间我正在收集 2 个位置字段“国家/地区”和“邮政编码”。当我为用户注册表单设置主题时,我想删除围绕国家/地区和邮政编码元素设置的位置字段。

你能指导一下吗?

I have enabled Location User module and during user registration I am collecting 2 location fields Country and Postal Code. As I am theming user registration form, I want to remove the Location field-set around Country and Postal Code elements.

Could you please guide me?

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

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

发布评论

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

评论(4

计㈡愣 2024-10-23 06:20:58

要更改内容类型表单上的位置字段集,您必须添加后构建,如下所示

function hook_form_alter(&$form, &$form_state, $form_id) {
  ...
  $form['#after_build'][] = 'alter_location_elements';
}

,回调函数将如下所示:

function alter_location_elements($form_element, &$form_state) {
  $form_element['locations'][0]['street']['#type'] = 'hidden';
  $form_element['locations'][0]['city']['#type'] = 'hidden';
  $form_element['locations'][0]['postal_code']['#type'] = 'hidden'; 

  return $form_element;
}

To alter the location fieldset on content type forms you have to add an after build, like so

function hook_form_alter(&$form, &$form_state, $form_id) {
  ...
  $form['#after_build'][] = 'alter_location_elements';
}

and the callback function would look something like this:

function alter_location_elements($form_element, &$form_state) {
  $form_element['locations'][0]['street']['#type'] = 'hidden';
  $form_element['locations'][0]['city']['#type'] = 'hidden';
  $form_element['locations'][0]['postal_code']['#type'] = 'hidden'; 

  return $form_element;
}
嘿咻 2024-10-23 06:20:58

到目前为止,还没有直接的答案,因为位置表单结构与其他表单数组不同。基本上使用 drupal_render 函数,我在默认位置字段集之外填充了邮政编码和国家/地区表单元素。因此模板显示空位置字段集。

我使用肮脏的技术 - CSS hack 暂时隐藏字段集来纠正它,如下所示:

#user-register fieldset {display:none;}

So far no straight answer for this as Location form structure is different from other form array. Basically using drupal_render function, I have populated postal code and country form element outside of the default Location field-set. So template was displaying empty Location field-set.

I rectified it using dirty technique - CSS hack for time being hiding field-set as below:

#user-register fieldset {display:none;}
爱,才寂寞 2024-10-23 06:20:58

对于 D6,最好的方法(不要使用 ['#type'] = 'hidden'; 或 unset(),这些方法会产生错误):

$form_element['locations'][0]['street']['#access'] = false;

在 form_alter 中:

$form['field']['#access'] = false;

By Css:

display:none;

By Js:

$('#IDELEMENT').css('display') == "none"); or $('#IDELEMENT').hide;

不需要 id 元素。

For D6, the best way (don't use ['#type'] = 'hidden'; or unset(), these methods can produce bugs):

$form_element['locations'][0]['street']['#access'] = false;

In form_alter:

$form['field']['#access'] = false;

By Css:

display:none;

By Js:

$('#IDELEMENT').css('display') == "none"); or $('#IDELEMENT').hide;

It isn't necessary a id element.

美羊羊 2024-10-23 06:20:58

如果您想要做的只是隐藏视图中的字段,那么将它们设置为 display:none; 的简单 CSS 类就可以解决问题。

如果您想实际更改表单中显示的字段(而不是简单地隐藏它们),您可以使用 form_alter 挂钩覆盖表单中的字段。

form_alter 挂钩允许您覆盖任何表单的内容。例如,如果您想向“user_login”表单添加一个字段,您可以编写如下函数:

function mymodule_form_user_login_alter(&$form, &$form_state, $form_id) {
  $form['myfield'] = array(
          '#type' => 'textfield',
          '#title' => t('My new field')
  );
}

然后,当显示用户登录表单时,其中将包含您的新字段。您可以在此 $form 数组中设置任意数量的字段。

同样,您可以通过取消设置来从表单中删除字段,如下所示:

unset($form['fieldname']);

但是,您将面临的问题是,在 Drupal 6 中,form_alter 挂钩仅适用于模块,不适用于主题。因此,您可能需要创建一个小模块来完成您需要的操作。我相信 Drupal 7 中已删除此限制。

请参阅 Drupal Hook_Form_Alter 文档 了解更多详细信息。

您可能还想查看 此链接,这是 Drupal 错误单,他们在其中讨论了添加功能以允许 < code>form_alter 主题。您会注意到,它被标记为已修复,Drupal 版本号为 7。

您对该表单的另一个选择是在您的主题中为其编写自己的模板。使用此功能,您可以使用 drupal_render 单独输出表单中的每个字段/字段集。如果您需要额外的 HTML 代码等,这还允许您进行更复杂的主题。但是,这种方法充满了危险,因为如果稍后将任何其他字段添加到表单中,您的主题将无法应对它。通常不建议以这种方式手动呈现表单。

希望有帮助。

If all you want to do is hide the fields from view, then a simple CSS class setting them to display:none; will do the trick.

If you want to actually change the fields that are presented in the form (as opposed to simply hiding them), you can override the fields in a form using the form_alter hook.

The form_alter hook allows you to override the contents of any form. For instance, if you want to add a field to the 'user_login' form, you could write a function like this:

function mymodule_form_user_login_alter(&$form, &$form_state, $form_id) {
  $form['myfield'] = array(
          '#type' => 'textfield',
          '#title' => t('My new field')
  );
}

Then when the user login form is displayed, it will now have your new field in it. You can set as many fields as you like in this $form array.

Likewise, you can remove a field from the form by unsetting it as follows:

unset($form['fieldname']);

The problem you will face, however, is that in Drupal 6, the form_alter hook is only available for modules, not themes. So you may need to create a small module to do what you need. I believe this limitation has been removed in Drupal 7.

See the Drupal Hook_Form_Alter documentation for more detail.

You may also want to see this link which is the Drupal bug ticket where they discussed adding the feature to allow form_alter on themes. You'll note that it is marked as fixed with the Drupal version number of 7.

Another option you have for the form is to write your own template for it in your theme. Using this, you can use drupal_render to output each field/fieldset in the form individually. This also allows you to do more complex theming if you want additional HTML code, etc. However, this method is fraught with danger, as if any other fields are added to the form later on, your theme will not be able to cope with it. Its generally not advised to render your form manually in this way.

Hope that helps.

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