为什么我应该使用 Drupal 的 Form API 和 Form API? Ajax 框架而不是仅仅通过调用 node_save() 来实现我自己的解决方案?

发布于 2025-01-04 18:12:45 字数 344 浏览 5 评论 0原文

我希望用户能够通过 AJAX 提交节点和评论。我还想对节点和评论表单进行一些相当广泛的定制。

我花了很多时间查看 Drupal 7 的 Form API 和 Ajax 框架的文档和代码示例,但我发现它非常复杂。因此,我只想用 HTML 创建自己的表单,并使用自己的 JavaScript 代码通过 Ajax 提交它。我还将设置一个特定的 URL 来处理这些 Ajax 请求,最终将在适当的时候调用 node_save()comment_save()

与通过 Form API 和 Ajax 框架相比,这种方式有哪些缺点?我创建模块并不是为了对社区做出贡献。一切只是为了我自己的网站。

I want users to be able to submit nodes and comments via AJAX. I also want to do some fairly extensive customization of the node and comment forms.

I've spent time looking through documentation and code examples for Drupal 7's Form API and Ajax framework, but I find it very complex. Therefore, I simply want to create my own form in HTML and use my own JavaScript code to submit it via Ajax. I'll also set up a specific URL for processing these Ajax requests, which will ultimately call node_save() or comment_save() when appropriate.

What are the downsides to doing it this way as opposed to going through the Form API and Ajax framework? I'm not creating modules for contribution to the community. Everything is just for my own site.

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

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

发布评论

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

评论(2

怪异←思 2025-01-11 18:12:45

从技术上讲,是的,您可以做到这一点而不会产生任何不良影响。有时我不得不从提要中导入数据并手动使用node_save。

您错过的是 drupal 提供的灵活性。例如,想要在表单中添加一个新的复选框来指示特色项目?现在,您必须手动更新表单以添加字段并更新提交处理程序以保存数据。如果您使用 drupal 的系统,它会自动为您填充。

进一步讲灵活性,例如,您决定要在表单中添加验证码字段。您所要做的就是启用验证码模块并指定您想要的表单,它就会为您完成。有很多第三方模块可以让你做这样的事情。

Drupals 表单系统还允许您添加更复杂的项目,例如日期选择器,甚至托管文件上传,一旦您熟悉 API,这可以为您节省大量时间。

如果您只是想完成项目而不是花时间学习新东西,那么您当然可以手动完成这一切。我可以向您保证,将来任何查看您的代码的 Drupal 开发人员都会对您的工作评价很低。根据您的情况,这可能很重要,也可能不重要。但实际上,您错过的最大的事情是易于维护和灵活性。

简单回顾一下:

优点:

  • 工作起来
  • 快速、简单
  • 、舒适

缺点:

  • 失去灵活性
  • 难以维护 无法
  • 利用 drupals 表单小部件/助手
  • 无法利用 3rd 方模块
  • 来自其他开发人员的耻辱
  • 因肮脏的感觉而缺乏睡眠
  • 永恒的诅咒

Technically, yes you can do this with no ill effects. There have been times where i have had to import data from feeds and have used node_save manually.

What you are missing out on is the flexibility that drupal offers. For example, want to add a new checkbox to the form to indicate a featured item? Now you have to manually update the form to add the field and update the submit handler to save the data. Had you used drupal's system it would of been auto-populated for you.

Further on flexibility, say for example, you decide you want to add a CAPTCHA field to your form. All you would have to do is enable the CAPTCHA module and specify the form you want it on and it would be done for you. There are a bunch of 3rd party modules that let you do things like this.

Drupals form system also lets you add more complicated items such as date selectors, or even managed file uploads, which can save you a lot of time once you are familiar with the API.

If your looking just to get the project done and not spend time learning something new, sure you can do it all manually. I can promise you any drupal developer that looks at your code in the future will have a very low opinion of your work. Depending on your situation this may or may not be important. But really the biggest thing you are missing out on is ease of maintenance and flexibility.

So just to recap:

pros:

  • it will work
  • quick and easy
  • comfortable

cons:

  • loss of flexibility
  • harder to maintain
  • inability to take advantage of drupals form widgets / helpers
  • inability to take advantage of 3rd party modules
  • shame from other developers
  • Lack of sleep from dirty feeling
  • eternal damnation
木森分化 2025-01-11 18:12:45

我想,通常的争论是关于可移植性的,但如果您不打算将这些模块移植到另一个 Drupal 站点,那么我想这个观点就会失败。

为其他模块提供基于某些全局/继承设置更改表单的机会也是如此,但如果您真的不想要/需要此功能,那么它不能真正用作反对的论据。

您将失去的一件事是内置的跨站点请求伪造保护。不过,只要您正在推出自己的版本,就应该没问题。

如果您打算经常使用 Drupal,我建议您习惯 FAPI……一段时间后,使用 FAPI 实际上会比编写自定义 HTML 容易得多。

The usual argument would be about portability I guess, but if you're not going to be porting these modules to another Drupal site then I guess that falls down.

The same can be said for offering other modules the chance to alter your form based on some global/inherited setting, but again if you really don't want/need this functionality then it can't really be used as an argument against.

The one thing you will lose out on is the built in Cross-Site Request Forgery protection. As long as you're rolling your own version of that, though, you should be ok.

If you plan to use Drupal a lot I'd recommend getting used to the FAPI though...after a while it actually becomes a lot easier to use the FAPI than write out custom HTML.

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