Community

Template or custom module - what is the best practice?

I have a requirement for a standard HTML form to collect a postcode or address, fire this off to our GIS (Geographical Information System), which returns a number of parameters incuding a slippy map etc. The form itself will not change over time, and as part of the process SQL queries will need to be passed to a seperate Postgres database. The flow will go something like this:

  • Visitor enters address
  • Form validates / sanitises input which is passed to Postgres which returns multiple options (e.g 28 main St, 29 Main St etc.)
  • Visitor selects his address
  • Form sends address ID to Postsgres and GIS
  • Postgres returns some rows and GIS returns map

The question I have is it worth writing a custom module just for 1 page? I like the idea of using the Drupal API for sanitising the data and using D7's database abstraction layer for accessing Postgres, but I could also embed the code as a template override. I would prefer to keep the code well away from anyone who might accidently change it, so I'd rather not to add this as a PHP code block to the page.

I am also wary of changing the default database - could the following code snippet impact the rest of the site if the page failed to completely load?

<?php
db_set_active
('mydb');

db_query('SELECT * FROM table_in_anotherdb');

//Switch back to the default connection when finished.
db_set_active('default');
?>

From http://drupal.org/node/18429 - How to connect multiple databases within Drupal

As this is a new system, I want to make sure that it follows Drupal best practice - Any suggestions or recomendations would be really appreciated.

Comments

Templates are only for the

Templates are only for the displaying of data that has already been generated. This is the principle behind MVC (which Drupal uses) that keeps logic and display separate. Consider this, if you put your logic in a template, and you change your theme, your site loses this functionality. But it sounds like your functionality is part of your site functionality that should remain even if the theme is changed, which means you want it in a module.

Templates should never contain any more programming logic than conditionals and loops. If you really need to do some processing that is theme based, then you can do that in template.php, and set variables that can be used in your template files.

Interested in hiring us? jaypan.com

Many thanks Jay, that clears

Many thanks Jay, that clears that up. Didn't want to fall into the trap that while there are many ways of doing things in Drupal, some are better than others.

nobody click here