Simple way to check for an override file in the custom directory

bsoremsugar —  May 21, 2012 — 7 Comments

The custom/ directory is the place for developers to make upgrade-safe customizations to the Sugar application. And as such, both internally here at Sugar and for you as module developers, the need for checking the custom directory for things is crucial. And to do this, you probably have code that looks something like this ( borrowed from the JJW Design’s Google Map plugin project )

But there’s one problem here, and that is that file_exists() is not allowed on modules to be used in our On-Demand environment. But there’s a solution for this, and it makes doing this sort of thing less painful than before. In Sugar 6.4, we’ve added a new function named get_custom_file_if_exists(), which will do this check for you. This reduces the above code example above down to this one-liner…

Hopefully this reduces the need to do these sorts of calls.

Are you seeing other reasons to do file_exists() calls in your modules? If so, sound off in the comments or on Twitter with your use cases and we’ll see what we can do to help you out.

7 responses to Simple way to check for an override file in the custom directory

  1. 

    I would love to see Sugar use this function internally when requiring some of the internal classes .. I often find myself with core classes I’d love to extend in an upgrade safe way, but as Sugar isn’t checking the custom folder first, I don’t really have a good point to hook into it.

    • 

       We do for many circumstances, but for overriding PHP classes it’s a bit too tricky to do right. We’ve been looking at other ways to accomplish this, such as via a service locator, and would love your input on a good solution.

  2. 

    It would be nice if you added these new methods to older versions as well (like 6.3). Right now you have to create two plugins (one that is nice and module-loadable and uses all these new methods) and then one that works for 6.3. There might also be two versions for CE. Maintaining all of these different versions becomes quite difficult and tedious.

    • 

       We try not add new functionality to earlier releases, as it causes confusion for plugin developers like yourself ( still need people to upgrade to latest 6.3.x to take advantage of the function ). But seeing that 6.3 is only supported for another year, hopefully there won’t be too much longer to have the older version.

  3. 

    Hi John, thanks for posting this article. I just now (August) stumbled on it. Please let me know of any additional changes I should be making to the Maps Project in order to make it On-Demand compliant. I don’t always get direct feedback from users. I’ve been working towards a new version, which I will try to always make as compatible/compliant as possible. Unfortunately, some people will not be happy that they will need to upgrade to v6.4 or greater.

    • 

       You should try loading it on any Sugar instance with the ModuleScanner turned on; this will let you know if the package is compatible.

      • 

        Looking back at it, I just changed file_exists() to is_file(). There is a difference between the two functions. is_file() will return false on directories, but is fine to check for files.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s