I'm trying to define a block in an external file (tpl.php?) file but have it visible in the Administer>Site Building>Blocks page available for "Disabled" mode.
I realize that I can make a custom block through the "Block" page and add PHP code to it. However, I'd like to protect the PHP code by keeping it in a separate file.
Is there a standard practice for this? I'm new to Drupal (we have 6.2) and having difficulty.
Should I:
A. Live with editing PHP in Drupal (what page variables do I have access to and how do I access them?)
B. Have my custom block load an external tmp.php file via a file function?
C. Download a module that does this already (though I haven't been able to find one)
D. Write a module that creates a custom block type?
E. ???
Thanks!
Comments
You might start by saying
You might start by saying what you want the block to do, there may be an existing module but without knowing what functionality you want it would be hard to guess.
You can make a custom block from the block admin and use PHP (if you have the feature enabled). Other than the few globals Drupal core uses there are no other variables defined for the PHP code to use.
Or write your own module that creates a custom block. If you really need a custom block and do not want the PHP in the block this would be the way to go.
I don't want admins to have
I don't want admins to have access to PHP code, scary. The blocks are custom to the site, it's easier to create them than search for modules that might have some of the features I need but not all of them. However, I don't like the idea of making the blocks from a module simply for maintenance, portability and I think it's overkill for my blocks.
What I ended up doing was creating blocks via CCK and overriding them with custom .tpl files. After creating a new block in the admin section I noticed drupal calls a block "block-[some number]" I created a file called block-block-[some number].tpl.php in my theme folder. This tpl file overrides the content of the block allowing me to build it however I like. Being that the .tpl.php is a PHP file this solution also allows for custom PHP code which is not accessible to admins. However, the block is still visible in the block maintenance page and admins keep the ability to move the block around within the site layout or disable it if need be.
I'm not sure it's the best solution but it seems to be working for now. Blocks have limited access to variables. I've been circumventing this by using the $GLOBALS array to determine which node I'm on and load the node as I need it ($mynode = node_load([some number])). That gives the PHP in my custom block access to variables about its surrounding environment. Seems horribly inefficient but functional until I know of a better way.