Altering Layout If Module Is Activated

Hey guys,

More newbie questions. As I migrate my applications over to ColdBox, I’ve been thinking about breaking some of them into their own modules. I was wondering if there was a way though to detect if a module is activated from a view or layout. Basically I’m wanting to display certain links in my layout, but only if the module is installed and activated.

1.) Is this dumb?

2.) Would I somehow inject the moduleService into the layout so I could call something like getLoadedModules()?

Best,

Grant

  1. No it is not dumb.

  2. You could very easily on a preRequest or some of the earlier entry points for your module, load up or get the loaded modules and see if it is in the struct.

Off the top of my head this will return all the loaded modules.

modules = getSetting(“modules”);

which returns a struct, so you could then do

if(!structKeyExists(modules, “myModule”) {

}

Or whatever you need to do.

Thanks Andrew! I’ll give this a try. I’m running into another issue also when I tried moving some of my application’s ORM models and services into the module. I’m getting hibernate errors like “Unknown Entity” because basically hibernate doesn’t know to look at the module’s model folder.

In Application.cfc, I have:

this.datasource = "db"; this.ormSettings = { cfclocation = ["model"], dbcreate = "none", logSQL = true, flushAtRequestEnd = false, autoManageSession = false, eventHandling = true, eventHandler = "model.ORMEventHandler", dialect = "MicrosoftSQLServer" };

Is the only way around this add the “modules” folder to my cfclocations in Application.cfc? This feels like there probably is a better way. :slight_smile:
I’m also now wondering if it’s okay to use ORM models in modules.

Best,

Grant

Grant, that is why you need to read up on CF-ORM.

The reason you are getting Unknown Entity is because ColdFusion uses the cfclocation to now where to look for entities, which means you need to also add the modules folder to the CFClocation. Which you have acknowledged.

And no there is no better way, that is how ColdFusion works. You could just use the root of your application, but remember that ColdFusion will scan every single cfc to determine if it is an Entity or not.

Thanks again Andrew for all your help. I really appreciate it.

Best,

Grant

No problem Grant, like I said feel free to ask that’s what we are here for.

Grant,

Try this in your application.cfc

this.ormSettings = {
cfclocation = [“model”],
dialect = “MySQLwithInnoDB”,
autogenmap = true,
dbcreate = “none”,
secondarycacheenabled = false,
cacheprovider = “ehCache”,
logSQL = false,
flushAtRequestEnd = false,
autoManageSession = false,
eventHandling = true,
eventHandler = “”,
skipCFCWithError = true
};

modulesDirectory = directoryList(path=getDirectoryFromPath(expandPath(’./modules/’)),recurse=false);
for(i=1;i<=arrayLen(modulesDirectory);i++){
arrayAppend(this.ormSettings.cfclocation,‘modules/’ & listLast(modulesDirectory[i],’/’) & ‘/model’);
}

It will load the module’s model folder at startup.

Thanks Jeremy! That worked perfectly.

Best,

Grant