I've gotten several different answers to this so I'm hoping that by explaining in more detail I can find the best approach.

We have a legacy MySQL database, and I've created a CCK page type that displays content from it. I'll have about 15 pages of that type, each with a different subject. I have some PHP functions that automate getting stuff from that database. That way, I can just use this for each of the 15 pages:

$term = "SomeSubject";
custom_function($term);

And it will print out a whole list of stuff from the DB.

The problem is that I can't figure out where to put the code that defines custom_function and its arguments. If I paste it into every one of the 15 pages, it works fine, but then I have 15 copies to maintain. If I try to put it in node-myccktype.tpl.php, it doesn't work. If I try to require or include it, it doesn't seem to do anything either.

Someone suggested that the safest way to do this would be to write a module, but the PHP interacts entirely with a non-Drupal database, and I'm not sure how to figure out what hook works with this CCK type anyway. Is there some safe and simple way to require or include these functions?

Comments

nevets’s picture

It sounds like writing a module that implements hook_nodeapi and the case where $op equals 'view' would be appropriate. You will also want to read How to connect to multiple databases within Drupal

egm’s picture

Thanks--that page is really helpful. I'll look at the hook_nodeapi documentation.

progga’s picture

Either I haven't understood the problem or CCK has no part here. Here's a potential solution:

0. Write your own module with an implementation of hook_nodeapi()
1. Inside the hook implementation, call your functions during the 'load' phase.
2. Add the return value (I am assuming it's a string) to the node object ($node->your_property = $you_value)

3. Print $node->your_property from node-myccktype.tpl.php in whichever way you like. Of course it doesn't allow you to edit these contents from the legacy db.

HTH

egm’s picture

If there's a role for CCK here, it's the fact that only a small proportion of pages will require this treatment--so I was thinking it would be inefficient to write something that ran at every page load, even for page types that will never need it.

Each thing returned from the database only has three parts, a title, a URL, and a description, so what you're describing in step 3 shouldn't be too complicated. No editing is required, just display. Thanks.

arh1’s picture

a quick and dirty solution would also be to use your theme's template.php file. you could add your custom functions to that file, and in your implementation of _phptemplate_variables, overwrite $content for that content type and run it through your custom functions. (this is a similar solution to using node-myccktype.tpl.php -- not sure what trouble you had there...)

this is probably not best practice, though, in that it's pushing logic that should be separated in a module into your theme layer. not sure...