Drupal - 用 div 包围特定的 Web 表单元素

发布于 2024-12-27 00:08:09 字数 136 浏览 5 评论 0原文

正如标题所描述的,我要设计一个网络表单,为此我需要包围 div 中的特定字段,以便为它们提供 CSS 属性,但我不知道应该如何去做。

我已经用 hook_form_alter 捕获了表单,但不知道之后要做什么。

有什么帮助吗?

as the title describes, I am to style a webform and for that i need to surround specific fields in divs in order to give them their CSS properties and I have no idea how I should go about doing that.

I've caught the form with hook_form_alter but no idea what to do after.

Any help?

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

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

发布评论

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

评论(3

岁月无声 2025-01-03 00:08:09

在您的主题 template.php 文件中,您需要获取表单 ID,然后您可以执行如下操作:

function phptemplate_webform_form_FORM_ID ($form) {
  $form['#prefix'] = '<div class="CLASS NAME">';
  $form['#suffix'] = '</div>';
  return _phptemplate_callback('webform_form_FORM_ID', array('form' => $form));
}

将 FORM_ID 替换为您的表单 ID。

编辑

要为特定字段添加它们,只需执行以下操作(类似):

$form['ELEMENT_NAME']['#prefix'] = '<div class="CLASS NAME">';
$form['ELEMENT_NAME']['#suffix'] = '</div>';

我没有时间测试它,但将 ELEMENT_NAME 替换为该元素,这应该可以解决您的问题。这里有一个很好的页面 - http://drupal.org/node/79086

In your themes template.php file you need to get the forms ID and then you can do something like the following:

function phptemplate_webform_form_FORM_ID ($form) {
  $form['#prefix'] = '<div class="CLASS NAME">';
  $form['#suffix'] = '</div>';
  return _phptemplate_callback('webform_form_FORM_ID', array('form' => $form));
}

Replace FORM_ID with your form's ID.

EDIT

To add them for specific fields just do (something like) this:

$form['ELEMENT_NAME']['#prefix'] = '<div class="CLASS NAME">';
$form['ELEMENT_NAME']['#suffix'] = '</div>';

I've not got time to test it but replace ELEMENT_NAME with the element and that should solve your problem. There's a good page on it here - http://drupal.org/node/79086

您可以使用 #field_prefix 和 #field_suffix 将字段与任何 HTML 元素一起包装。请参阅下面的示例:

    function auworks_form_alter(&$form, $form_state, $form_id) {
      switch($form_id) {
        case 'webform_client_form_1':
          $form['submitted']['title']['#field_suffix'] = '<span class="field-suffix">#</span>';
          break;
      }
    }

让我知道你进展如何......

You can use #field_prefix and #field_suffix to wrap field with any HTML element. See example below:

    function auworks_form_alter(&$form, $form_state, $form_id) {
      switch($form_id) {
        case 'webform_client_form_1':
          $form['submitted']['title']['#field_suffix'] = '<span class="field-suffix">#</span>';
          break;
      }
    }

Let me know how you go...

风铃鹿 2025-01-03 00:08:09

另外:如果您想将包含的 div 添加到网络表单的提交按钮,这里有一个钩子可以帮助您:

function YOURTHEMENAME_form_alter(&$form, &$form_state, $form_id) {

    if ($form['#form_id'] === 'webform_client_form_YOURWEBFORMID') {
        $form_structure = &$form['submitted'];

        $form['actions']['submit']['#prefix'] = '<div class="extra_div">';
        $form['actions']['submit']['#suffix'] = '</div>';
    }

}

如果您想将其应用于所有网络表单,您只需保留条件即可,如下所示:

function YOURTHEMENAME_form_alter(&$form, &$form_state, $form_id) {

    $form_structure = &$form['submitted'];

    $form['actions']['submit']['#prefix'] = '<div class="extra_div">';
    $form['actions']['submit']['#suffix'] = '</div>';    
}

Additionally: in case you want to add containing div's to the submit button of the webform, here's a hook which will help you out:

function YOURTHEMENAME_form_alter(&$form, &$form_state, $form_id) {

    if ($form['#form_id'] === 'webform_client_form_YOURWEBFORMID') {
        $form_structure = &$form['submitted'];

        $form['actions']['submit']['#prefix'] = '<div class="extra_div">';
        $form['actions']['submit']['#suffix'] = '</div>';
    }

}

If you want to apply this to all webforms, you just leave the conditional away, like this:

function YOURTHEMENAME_form_alter(&$form, &$form_state, $form_id) {

    $form_structure = &$form['submitted'];

    $form['actions']['submit']['#prefix'] = '<div class="extra_div">';
    $form['actions']['submit']['#suffix'] = '</div>';    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文