Why should I use Drupal's Form API & Ajax framework for this rather than just implementing my own solution with calling node_save()?

橙三吉。 提交于 2019-12-11 23:07:40

问题


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.


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/9264514/why-should-i-use-drupals-form-api-ajax-framework-for-this-rather-than-just-im

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!