Why should I use Drupal's Form API & Ajax fram

2019-08-19 19: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.

2条回答
甜甜的少女心
2楼-- · 2019-08-19 20:20

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
查看更多
老娘就宠你
3楼-- · 2019-08-19 20:37

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.

查看更多
登录 后发表回答