简体   繁体   English

Drupal-Webform元素主题

[英]Drupal - Webform element theming

Another question about Drupal webforms -- The form itself is built in by /includes/form.inc 's 关于Drupal Webforms的另一个问题-表单本身由/includes/form.inc内置

function theme_form_element($element, $value)

and adds a <label> element to the $output. 并将<label>元素添加到$ output中。 I want to remove that label only for one webform, so I have to override the function. 我只想为一个网络表单删除该标签,所以我必须重写该功能。 How can I override it for only one webform, while leaving it the same in all others? 如何仅对一个Web表单覆盖它,而在所有其他Web表单中使它保持不变? Eg 例如

if ($block == 'contact'):
  // only output <input> form element stored in $value
  function mytheme_html_form_element($element, $value) {
    $t = get_t();
    $output .= " $value\n";
    return $output;
  }
endif;

Is this possible, and what goes in the if condition? 这可能吗,如果条件是什么呢?

If you're just looking to remove the label, you can also use hook_form_alter() , and check that $form_id is equal to the webform in question. 如果您只是想删除标签,则还可以使用hook_form_alter() ,并检查$ form_id是否等于所讨论的Webform。 The id will be of the form: webform_client_form_N where N is the node ID of the webform. 该ID的格式为:webform_client_form_N,其中N是该Webform的节点ID。

Once you're operating on the proper form, you can unset the label using, for example, code like this: 使用正确的格式进行操作后,您可以使用例如以下代码取消设置标签:

 unset($form['submitted']['first_name']['#title']);

Which would unset the label for a field called first_name. 这将取消设置名为first_name的字段的标签。

i did have to do a hook_form_alter, but the label itself was in the ['submitted'] element. 我确实必须执行hook_form_alter,但标签本身位于['submitted']元素中。 here is the code 这是代码

  if($form_id == 'webform_client_form_18') {
    $form['submitted']['#children'] = '
    <input
     type="text" 
     maxlength="128"
     name="submitted[email]"
     id="edit-submitted-email"
     value="' . $form['submitted']['email']['#default_value']. '"
     class="form-text required"
    />
  ';
  }

in a different form, removing the #title worked (+1 for you!), but this was a different case. 以另一种形式删除#title起作用(为您+1!),但这是不同的情况。

I wouldn't unset form element titles. 我不会取消设置表单元素标题。 You could get unexpected results when your form gets rendered by the theme engine. 当主题引擎渲染表单时,您可能会得到意想不到的结果。

You can do it several ways: 您可以通过以下几种方法进行操作:

Theme each element or the whole form with with '#theme' => 'my_callback' . 使用'#theme' => 'my_callback'每个元素或整个表单设置主题。

You can also create your own form element using hook_elements that uses a corresponding theme hook. 您还可以使用hook_elements创建您自己的表单元素,该元素使用相应的主题挂钩。

See: 看到:

http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html

http://api.drupal.org/api/function/hook_elements/6 http://api.drupal.org/api/function/hook_elements/6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM