0

Mine is shared hosting account with limited CPU usage so I want to minimize CPU and mysql usage. I'm generating 100,000s of nodes which contain some information. I also want to add a form in those pages. Since these nodes will be stored in node table how can I generate form on the fly? I think using filters but that'll not use Drupal caching hence it'll affect the shared hosting CPU quota due to this performance issue.

I don't want to use _menu to generate those pages on the fly because 1) it will slow down my site 2) It'll be difficult to use Drupal Taxonomy to classify "these" generated pages on the fly. Generating pages from the node table will be faster and also cacheable.

Another way is to insert the Form code in the nodes at the time of generation with the option that it won't contain/verify dynamically generated tokens(eg without #token) but it'll be security risk for my site.

What is the best way to handle this case?

1 Answer 1

1

What do you mean "generate form on the fly"? As in: the form for page "page/form" doesn't exist in a canonical state but is generated based on variables such as date/url/taxonomy? Or is it just the default values for the form etc?

In anyway: the options to have a form on a page are:

  1. Hardcode HTML into the node content, e.g. <form><input> etc
  2. Use hook_menu to create the page and use the form API
  3. Use hook_menu to create the page and use the straight HTML
  4. Use hook_nodeapi to add a form to the content with the Form API
  5. Use hook_nodeapi to add a form to the content using HTML
  6. Use a module such as webforms to add the forms

I would suggest either 2 or 4. When you say that hook_menu generates these plages "on teh fly": that's not true. hook_menu is only run on cache clear, and assuming you're using caching then the generation of forms isn't going to make much difference to your server. This may be a case where you're sweating about optimisation unnecessarily.

Using Form API gives you a load of tools that you'd otherwise have to handle yourself - validation, submission, sanitising, standardising, etc.

4
  • On the fly is just like generating the page using hook_menu without caching turned on and instead of fetching pre-built page from the node table. I don't want to use hook_nodeapi as it'll need to be called for every page load and I'll have tons of pages as well as traffic. I'm looking for embedding Form code within the node. So simply fetching the code from node table and displaying will be the work to do.
    – AgA
    Commented Mar 27, 2012 at 13:10
  • Apemantus got it right AFAIK : standard Drupal caching will do a better job performance wise than the overhead caused by any custom code I can think of. Unless of course you code it from the bottom up, in which case using Drupal makes no sense. And you'll have to be really really smart in your development to outwit the skillful people who have coded what exists.
    – Countzero
    Commented Mar 27, 2012 at 15:06
  • hook_nodeapi is called by lots of different modules every time a page is viewed: you adding one more call per page isn't going to make any real difference. And as you presumably have caching turned on (with lots of pages and lots of traffic on a shared server), I'm not sure why you're worried about hook_menu either. Again, there's many calls to hook_menu throughout your site, even before you touch custom code. One of the stages of Drupal development (for me) was learning to accept that in the vast majority of cases I was better off working with Drupal, rather than against it.
    – Apemantus
    Commented Mar 27, 2012 at 15:41
  • On another note - why not just create the form using FAPI, use hook_menu to call it...and cache the forms yourself using cache_set. Then you get the benefits of caching, plus the benefits of hook_menu. Check this out for help with caching.
    – Chapabu
    Commented Mar 27, 2012 at 15:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.