views:

36

answers:

1

Hello,

I have added PHP code to a block in my Drupal site,by setting the input format to PHP Code. I would like to include an external php file in my code. Where should this file be placed, so that it would be available to all blocks ?

Please help Thank You

+1  A: 

The best thing to do, is to create a small module for this, and create a function that you can call from your blocks. Every function in the .module file is callable, since all module files of activated modules is parsed.

In your function you can use require once to include your php file and call the function you need from the file. Alternatively you could just include the file, and let the code in your blocks call whatever function you need.

You should probably check if the function exists first though, so your blocks wont break if you disable the module.

Since you need, php code and a custom php file, it would probably make more sense to create the blocks with hook_block in your custom module. Then you know that you can include the file etc. and be able to version control the php code in your blocks. This is recommended, but requires a little extra work.

googletorp