Environment override - Block type mapped class

Permalink
I'm trying to install a block with BlockType->install('block_handle'), but locally everything works fine. I do have this block under application/blocks. On my live server though, it's searching for \Concrete\Block\ instaed of \Application\Block\. After debugging, I came to the conclusion the following in the BlockType.php class (file) is different on local compared to live:

$env = Environment::get();
$txt = Loader::helper('text');
$r = $env->getRecord(DIRNAME_BLOCKS . '/' . $btHandle . '/' . FILENAME_CONTROLLER);
var_dump($r->override);


You will notice $r->override will be true locally and false on a live server. Because this is true, the getBlockTypeMappedClass will prefix with the core class.

$prefix = $r->override ? true : $pkgHandle;
$class = core_class('Block\\' . $txt->camelcase($btHandle) . '\\Controller', $prefix);


I know I can write my own code, but of course I want to use core code in my packages.

So I went looking further, and saw in concrete/controllers/single_page/dashboard/blocks/types.php in the view function this code

$env = Environment::get();
$env->clearOverrideCache();


Adding this in, helped me. Is this the way to go? Or should I be doing it differently?

ramonleenders
 
Juha replied on at Permalink Reply
Juha
If you have caching enabled on the live server, the problem may be that on your dev server override list is built on every request whereas on the live server cache is checked first. In that case clearing the cache (using the dashboard page) might help.

I don't recall ever needing to clear the cache programmatically in my package installer, but I can't say if there's any real harm doing it your way either, though.
ramonleenders replied on at Permalink Reply
ramonleenders
Yeah well, this is not in my package installer but in a single_page, so other things might be still happening before all this. I've found this code in a single_page of the core, where blocks get installed. So I figure this is the way to go, but wasn't sure as it seems kinda odd. But could be me eh. Clearing the (override) cache is fine with me, as long as it doesn't hurt anything :)
Juha replied on at Permalink Reply
Juha
But you are adding the block from a package, right?
ramonleenders replied on at Permalink Reply
ramonleenders
Yes, correct. I have a single page WITHIN a package. Within this single page of the package I have a function and almost immediately call the installblocktype function. No weird code or anything at all. So I assume I have to go with the same approach as the types.php file (concrete/controllers/single_page/dashboard/blocks/types.php). You can see they use the same code in the view function, direct first 2 lines.
Juha replied on at Permalink Reply
Juha
Ah, I see. In that case the refresh might be necessary.

It looks to me that the clearOverrideCache() method just deletes the cache file and forces Concrete5 to refresh its list of overrides. Doesn't seem that dangerous. :)