将 HTMl 添加到 Drupal 中的 CCK

发布于 2024-08-27 09:55:18 字数 282 浏览 5 评论 0原文

你好,我是 Drupal 的新手,在我的进步中遇到了障碍。我一直在使用 CCK 向我的表单添加新的内容类型。我想知道是否有任何方法可以添加到生成的表单中,以便我可以包含元素并插入可视化 html 代码,如头规则等。我已经涉足了 hook_form_alter() ,它似乎对我没有帮助在我的努力下。我一直在调整 tpl.php 文件等,但没有取得任何进展。如果互联网上有任何人对此事有了解,请提供您的建议,我们将不胜感激。

这只是我想要在表单中执行的操作的示例: 1. 在 DIV 中包含字段元素 2. 将 HTML 内容添加到表单中

Hello im a newbie at using Drupal, and have come across a block in my progress. I have been using CCK to add new content types to my forms. I was wondering if there was any way to add to the form that is generated so that i may contain the elements and also insert visual html code like head rules etc. I have dabbled with the hook_form_alter() and it does not seem to help me in my efforts. Ive been through adjusting tpl.php files and such and have made no progress. Please if there is any one there in the inter-webs who is knowledgeable on this matter your advice would be greatly appreciated.

Here is just an example of what I would want to do within the form:
1. Contain field elements within DIV's
2. Add HTML Content into the form

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

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

发布评论

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

评论(3

兔小萌 2024-09-03 09:55:18

如果您想自定义 for 的标记,您可以为其创建一个主题函数,并使表单与 hook_form_alter 一起使用它。这与 CCK 无关,CCK 用于自定义内容类型,因此您可以向内容类型添加其他内容。

If you want to customize the markup for a for you can create a theme function for it and make the form use it with hook_form_alter. This has nothing to do with CCK which is used to customize a content type so you can add additional content to a content type.

云归处 2024-09-03 09:55:18

hook_form_alter 无法帮助您更改包含 cck 字段的编辑表单,因为 cck 字段是在 hook_form_alter 调用之后添加的。

您需要像这样对表单进行主题设置:

创建一个模块并将此钩子实现放入其中:

function mymodule_theme($existing, $type, $theme, $path) {
    'your_form_id' => array(
        'arguments' => array('form' => NULL),
        'template' => 'my_template',
     )
}

然后在模块目录中创建一个名为 my_template.tpl.php 的文件。
现在清空你的缓存。

在该文件中,您可以单独呈现表单元素,如下所示:

<?php
print drupal_render($form['a_form_element']);
print '<h2>blah blah blah...</h2>';
print drupal_render($form['another_form_element']);
// this one is for rendering all the remaining items, like hiddens:
print drupal_render($form);

在开发过程中使用 devel 模块。它包含许多有用的工具,使开发变得更加容易。

hook_form_alter does not help you in altering an edit form containing cck fields because cck fields are added after hook_form_alter call.

You need to theme your form like this:

Create a module and put this hook implementation in it:

function mymodule_theme($existing, $type, $theme, $path) {
    'your_form_id' => array(
        'arguments' => array('form' => NULL),
        'template' => 'my_template',
     )
}

Then create a file named my_template.tpl.php in your module directory.
Now empty your cache.

In that file, you can render form elements separately, like this:

<?php
print drupal_render($form['a_form_element']);
print '<h2>blah blah blah...</h2>';
print drupal_render($form['another_form_element']);
// this one is for rendering all the remaining items, like hiddens:
print drupal_render($form);

Use devel module during development. It contains many useful tools that makes development much easier.

鯉魚旗 2024-09-03 09:55:18

原始 HTML CCK 字段 (Drupal 6) - 无过滤器、格式或编辑器

简单修复!只需对未过滤的 HTML 使用纯文本格式。然后在构建节点时将其转换回字段 .tpl 中的 html。

CCK 字段上的纯文本格式会将 HTML 标记转换为实体特殊字符(这将使其读起来像页面上的代码,而不是实际的 html 标记)。它将使用 php 的 htmlspecialchars($text, ENT_QUOTES, 'UTF-8') 编码的字符串存储在 drupal 的 check_plain() 函数中。

最干净、最简单的解码方法是在field tpl 文件中。这可以避免钩子、钩子顺序问题、循环错误和性能问题。这是通过将 tpl 文件添加到基本主题来完成的:hq_base、odyssey_base 和 odyssey_admin。以下是 drupal 如何解码 cck 节点编辑表单上的纯文本: print html_entity_decode(strip_tags($text), ENT_QUOTES);注意 - html_entity_decode 在解码回 html 时将 php 标签转换为 html 注释。以下是具有正确命名约定的示例文件,可让 php 控制字段:

content-field.tpl.php

content-field-[your_field_name].tpl.php

content-field.tpl.php 是从 cck contrib 到主题文件夹的副本,这是一个 contrib 覆盖,以使其在主题中可用,并且不应修改(除非您想更改主题中的所有字段)。字段特定文件也是 tpl 的副本,一旦覆盖文件存在,它将起作用。然后在字段 tpl 文件中解码为 html

• // print $item['view'];

print html_entity_decode(strip_tags($item['view']), ENT_QUOTES);

Drupal 版本说明:
Drupal 7 和 Drupal 8 中的 tpl 文件略有不同。但是 html_entity_decode() 是一个 php 函数,不会根据 Drupal 版本进行更改。

安全注意事项:
这种获取原始 HTML 的解码技巧违背了 Drupal 为文本格式化安全而构建的方式。这意味着任何有权编辑页面的人现在都有权编辑 html 结构,并在页面上添加脚本标签。这可能会破坏布局,并且可能存在危险。您在这里依赖于安全性的编辑权限,而不是 Drupal 的正常“每个角色格式”安全性。

Raw HTML CCK Field (Drupal 6) - no filters, formats or editor

Simple Fix! Just use plain text format for unfiltered HTML. Then convert it back to html in a field .tpl when the node is built.

Plain Text format on a CCK field will convert the HTML tags to entity special characters (this would make is so it reads like code on the page instead of being actual html tags). It stores the string encoded using php's htmlspecialchars($text, ENT_QUOTES, 'UTF-8') inside of drupal's check_plain() function.

The cleanest simplest way to decode it, is in a field tpl file. This avoids hooks, hook order problems, looping bugs, and performance issues. This is done by adding tpl files to the base themes: hq_base, odyssey_base and odyssey_admin. Here is how drupal decodes plain text on a cck node edit form: print html_entity_decode(strip_tags($text), ENT_QUOTES); Note - html_entity_decode turns php tags into html comments when it decodes back to html. Here are sample files with the correct naming convention to give php control over the fields:

content-field.tpl.php

content-field-[your_field_name].tpl.php

content-field.tpl.php is a copy from the cck contrib into the theme folders, this is a contrib override to make it available in the theme, and should not be modified (unless you wanted to change all the fields in the theme). The field specific file is also a copy of the tpl, it will work once the override file is there. Then decode to html in the field tpl file:

• // print $item['view'];

print html_entity_decode(strip_tags($item['view']), ENT_QUOTES);

Drupal Version Note:
The tpl files are slightly different in Drupal 7 and Drupal 8. But the html_entity_decode() is a php function that won't change per Drupal version.

Security Note:
This decode trick for getting raw HTML goes against the way Drupal was built for text formatting security. It means that anyone with permissions to edit the page now has permissions to edit html structure, and add script tags on the page. This can break layouts, and potentially be dangerous. You are relying on editing permissions for security here, instead of Drupal's normal Formats-per-Role security.

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