Including a "functions.php" file in a block

Permalink
I come from a WordPress background and want to include a general php file that has function callbacks that gets included on all pages. I'm currently building a theme package with some custom blocks and have that file being loaded through the theme to use along with the block, but ultimately it should be loaded with the block so I can keep the 2 things separate.

I'm not sure if I should be creating a "helper" or how to go about this. I don't want the file to load within each individual block (where it only gets loaded if a block is added to a page), I would like the functions to be accessible globally if the block is installed/activated on the site.

Any tips? Thanks!

joemc
 
Steevb replied on at Permalink Reply
Steevb
Have you looked in the developers index:http://www.concrete5.org/documentation/developers/...
joemc replied on at Permalink Reply
joemc
Yes, I use it regularly... but, I can't seem to find the answers I'm looking for.
Sadu replied on at Permalink Reply
Sadu
Possibly a Library is the way to go.

Add a PHP class called "MyFunctions" in my_functions.php to the /libraries/ folder.

Implement all your functions as static methods of the class.
class MyFunctions {
static function bla(){
return 'bla';
}
}


Then use the following in your block...
Loader::library('my_functions'); //just add this line once
echo MyFunctions::bla();


Should do the trick.

To be super-clean about it, bundle your libraries and blocks into a package.
joemc replied on at Permalink Reply
joemc
Cool. I will check that out.

I am currently building a package with the theme, blocks, etc... which seems to be working pretty well.

One question, though, is if I want to split the block out of my package to possibly sell or distribute separately, how would that work? Since the Library file is external to the block, would I have to create a whole new package with just the block and the library file?
jordanlev replied on at Permalink Best Answer Reply
jordanlev
The standard way to do this in C5 is with a helper. You can use a library (as one of the other answers suggests), but helpers are specifically made for this purpose so I think it's better to use them because the code will be easier to understand if someone else comes in to work on your system.

To make a helper, add a new file to your site's top-level "helpers" directory -- for example, let's call it "joe_mc.php". Inside the file you would have some code like this:
<?php  defined('C5_EXECUTE') or die("Access Denied.");
class JoeMcHelper {
    public function doSomething() {
        //do stuff here....
        return 'something';
    }
    public function somethingElse() {
        return 'something else';
    }
}


Then, throughout your site's codebase, you can do this:
echo Loader::helper('joe_mc')->doSomething();


Or, if you're using the helpers' functions more than once in one file, you can do this:
$jh = Loader::helper('joe_mc');
//...
$jh->doSomething();
//...
$jh->somethingElse();


Note that the usual C5 naming conventions apply here -- the file name should be lowercase_underscore, and the class name should be CamelCaseHelper (add the word "Helper" to the end of the CamelCase version of the file name).

All that being said, if you really want to duplicate the way Wordpress has functions in a file (and you want to bypass all the Loader::helper() calls), you could create a file called "site_events.php" in your site's top-level "config" directory -- that will be loaded by the system and any functions defined there would be available everywhere across the site. It's not a "best practice", but it will work.

-Jordan
jordanlev replied on at Permalink Reply
jordanlev
And to answer your other question... you should just have one package that contains the block and the helper / library. That is the purpose of packages (to bundle all this stuff together).

Note that in the sample code from my other answer, since you have the helper in a package, you'll need to pass in the package handle when calling Loader::helper()... like so:
Loader::helper('joe_mc', 'my_package_handle');


And the trick about the site_events.php file won't work from a package.
joemc replied on at Permalink Reply
joemc
Thanks, Jordan! I'll give the helpers a try.

I don't necessarily want to force C5 to act like WordPress, I just think like a WordPress developer and have limited knowledge of C5's best practices, which I'd like to learn!

One more question for you... I wanted to override the default layouts behavior (to fit with Bootstraps scaffolding), but could not figure out where to even start with that, so I created a custom block that allows users to set up a grid of a total of 12 spans. That block does not display anything on the front end, but the function (that I am dealing with in this thread) hijacks the default area display to create extra areas laid out in the grid. I think it works pretty slick, but I'm sure there's a probably a better way to actually use/override the layouts. Do you have any starting point tips for modifying the layout behavior? Or even just creating a new top level edit menu item for my own custom layouts?

Thanks for the help and your knowledge!
jordanlev replied on at Permalink Reply
jordanlev
I am not sure about how to customize the layouts system (I know they're working on improving that, and making it so you can integrate it with frameworks/grid systems -- but it will be a while before that's released probably).

I have some CSS from a project that made the area layouts responsive -- not sure if that's the problem you're trying to solve, but if so let me know and I'll dig it up for you.
joemc replied on at Permalink Reply
joemc
I've come up with a custom block system that works pretty well... I'd prefer to be able to modify the layouts, but it doesn't sound possible at the moment.

Would you be willing to check out my custom block and let me know if you see any possible issues that may arrise from it?
jordanlev replied on at Permalink Reply
jordanlev
Hey Joe,
I wish I had the time to take a look at your block, but unfortunately I'm super busy this week -- sorry, wish I could be of more help.
joemc replied on at Permalink Reply
joemc
Not a problem. Thanks for all the help!