如何让 drupal 知道提交自定义内容
我知道这不是一个 drupal 论坛,但是,由于我在那里没有得到任何回复,所以我决定在这里尝试一下。
我正在创建一个接受用户自定义内容的网站。因此,就此而言,该网站有一个表单和一个自定义模块。该表单不使用管理主题,而是放置在自定义模板内,该模板的创建目的是与其余页面具有统一的外观。因此,通过hook_form创建表单元素是没有问题的。这就是我的问题所在。由于此表单使用自定义主题,我不确定如何让 drupal 知道用户在提交表单时正在提交新的内容数据?
我是否需要使用与管理页面内容提交页面相同的查询字符串,例如 - ?q=node/add/page 作为 html 表单的操作属性? (或)
唯一的方法是将 url 映射到我的自定义函数并调用其中的某种钩子?
谢谢
I know this is not a drupal forum but, as I’m not getting any response there, I decided to give it a shot here.
I’m creating a web site that accepts custom content from users. So, for that matter, this site has a form and a custom module. Instead of using admin theme, this form is placed inside custom template which is created to have a uniform look with the rest of the pages. As a result, creating form elements through hook_form is out of question. Here’s where my problems lie. As this form uses custom theme, I’m not sure as to what can I do to make drupal know that user is submitting new content data when the form is submitted?
Would I need to use same query string that of content submission page of admin page like - ?q=node/add/page for action attribute of the html form?
(OR)the only way is to map the url to my custom function and invoke some sort of hook inside of it?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以为表单创建任何所需的标记,您所需要做的就是在定义表单时使用
#theme
属性。使用它,您可以为表单本身和任何元素设置主题功能。不使用 Drupal 的 FAPI 是一个非常坏主意。它为您解决了很多问题,如果您想在站点中打开安全漏洞,那么不使用它将是采取的第一步。如果您不使用 Drupal 这样的开发框架的 API,那么它就没有多大价值。
编辑:
首先要做的就是访问 Drupal 的 FAPI 参考。您可以在那里了解有关 FAPI 的几乎所有内容。
如果您愿意,您可以使用模板,这只是基本的 Drupal 主题,但我建议您不要这样做。如果您为所有元素创建主题功能并使用它,那么它的可维护性会更高,您可以像 Drupal 那样循环遍历所有元素并渲染它们,而不必每次都需要编辑模板文件来更改形式。现在这可能是一项更多的工作,但这项工作有一个回报:更干净、更易于维护的代码。
在代码中,它看起来像这样:
这样做,该元素将使用“theme_function”进行渲染。您可以看到这样一个文本字段主题函数的示例。
You can literally create any markup you want for your form, all you need to do is use the
#theme
attribute when you define the form. With it you can set theme functions for the form itself and any of the elements.It is a very bad idea, not to use Drupal's FAPI. It solves so many problems for you, and not using it would be the first step to take if you want to open up a security hole in your site. A development framework like Drupal is not of much worth, if you don't use it's APIs.
Edit:
First thing to do, is to go to Drupal's FAPI reference. You can learn almost everything about the FAPI there.
You could use a template if you want, is just basic Drupal theming, but I would advise against it. It would be a lot more maintainable if you created theming functions for all the elements and used that instead, you could just loop through all the elements and render them like Drupal does, instead of having to edit a template file each you need to change the form. It might be a but more work now, but there's a reward to that work: cleaner and more maintainable code.
In code it looks something like this:
Doing this, the element will be rendered using the "theme_function". You can see an example of such a theme function for textfields.