带有自定义 ID 的 Drupal 表单

发布于 2024-09-01 08:15:25 字数 222 浏览 12 评论 0原文

如果我错了,请纠正我,在阅读了 drupal fapi 相关文章后,我的印象是 fapi 自己生成“id”属性。它允许开发人员仅分配“名称”属性。如果是这种情况,有没有办法为元素设置所需的“id”值?因为,我希望我的元素具有有意义的“id”,以便 html/jquery 代码更易于阅读,并节省我的时间,无需通过已经编写的 jquery 代码来更改我在内部使用的所有“id”。

PS:drupal版本 - 6.x

Correct me if I'm wrong, after reading drupal fapi related articles, I got the impression that fapi generates 'id' attributes by itself. It allows developers to assign 'name' attribute only. If that's the case, is there a way I can set desire 'id' value for elements? Because, I want my elements to have meaningful 'id' so that html/jquery code would be easier to read as well as save my time from going through already written jquery code to change those all 'id's that I've used inside.

P.S:drupal version - 6.x

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

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

发布评论

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

评论(4

要走干脆点 2024-09-08 08:15:25

好的找到了解决方案。我可以使用 $form 元素的 #attributes 键来设置任何其他属性(例如 class、id 等)。感谢您迄今为止的帮助。

Ok found the solution. I can use the #attributes key of the $form element to set any additional attributes (such as class, id, etc.). Thanks for your help so far.

绝對不後悔。 2024-09-08 08:15:25

我有一个类似的问题需要处理。我需要在同一页面上有多个表单,因此我必须更改表单及其元素的 id,以防止重复的 id。我做了类似以下的事情:

function voci_comment_form($form, &$form_state, $cid) {
  $form['#attributes']['id'] = 'voci-comment-form-' . $cid;
  $form['#attributes']['class'][] = 'voci-comment-form';
  $form['body'] = array(
    '#title' => 'Post a comment',
    '#type' => 'textarea',
    '#resizable' => FALSE,
    '#rows' => 1,
  );
  $form['comment'] = array(
    '#type' => 'submit',
    '#value' => 'Comment',
  );

  foreach ($form as $k => &$element) {
    $k = str_replace('_', '-', $k);
    $element['#attributes']['id'] = "edit-$k-$cid";
    $element['#attributes']['class'][] = "edit-$k";
  }

  return $form;
}

这基本上根据传入的 $cid 设置了唯一的 id。该代码还向表单中的每个元素添加了类,以便您可以轻松地对其进行样式设置。我确信可以有更强大的解决方案,但这是基本思想。在 Drupal 7 中测试。

I had a similar issue to deal with. I needed to have multiple forms on the same page so I had to change the ids of the form and its elements to prevent duplicate ids. I did something like the following:

function voci_comment_form($form, &$form_state, $cid) {
  $form['#attributes']['id'] = 'voci-comment-form-' . $cid;
  $form['#attributes']['class'][] = 'voci-comment-form';
  $form['body'] = array(
    '#title' => 'Post a comment',
    '#type' => 'textarea',
    '#resizable' => FALSE,
    '#rows' => 1,
  );
  $form['comment'] = array(
    '#type' => 'submit',
    '#value' => 'Comment',
  );

  foreach ($form as $k => &$element) {
    $k = str_replace('_', '-', $k);
    $element['#attributes']['id'] = "edit-$k-$cid";
    $element['#attributes']['class'][] = "edit-$k";
  }

  return $form;
}

This basically sets unique ids based on the $cid that is passed in. The code also adds classes to each element in the form so you can style it easily. I'm sure a more robust solution is possible but this is the basic idea. Tested in Drupal 7.

陌伤浅笑 2024-09-08 08:15:25

确实,您可以设置 $element['#attributes']['id'] 并将其应用于表单字段。然而,它会破坏 Drupal 7 中的标签和#states,因为渲染管道的其余部分从其他地方读取 ID。因此,为了让您的标签和 #states 继续工作,请使用将 ID 设置为 $element['#id'] (这是一个未记录的属性,但表单 API 的方式是这样的)在内部监视 ID)。

请务必通过 drupal_html_id 传递您的 ID,以确保不会发生冲突。

It's true that you can set $element['#attributes']['id'] and that will apply to the form field. However, it will break labels and #states in Drupal 7 because the rest of the rendering pipeline reads the ID from somewhere else. So for your labels and #states to keep working, use set the ID to $element['#id'] instead (an undocumented property that nonetheless is how the form API watches ID internally).

Make sure to pass your ID through drupal_html_id as well to ensure no conflicts.

浅听莫相离 2024-09-08 08:15:25

这个问题实际上与 Drupal-FAPI 本身没有太大关系,而更多地与 Drupal 主题的形成方式(创建标记)有关。

  • 如果您想要更改网站上的所有表单,您可以覆盖用于表单和不同类型表单字段的主题功能。

    如果

  • 如果您只想覆盖某些表单或表单字段,您可以在表单或元素上设置#theme 属性,以更改应使用哪个函数来创建标记。

This problem doesn't really have much to do with the Drupal-FAPI itself, but more with how Drupal theme forms (create the markup).

  • If you want to alter all forms on your site, you can overwrite the theming functions that is used for forms and the different type of form fields.

  • If you just want to overwrite some forms or form fields, you can set the #theme attribute on the form or an element, to change which function should be used for creating the markup.

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