Overriding core class via a package

Permalink 2 users found helpful
I have a package and I'm trying to override the core TextHelper.

Filesystem:
/packages/my_package/helpers/text.php

<?php
class TextxHelper extends Concrete5_Helper_Text{
   public function foo(){
      echo 'bar';
   }
}
?>


In my package's controller.php:
public function on_start(){
   Environment::overrideCoreByPackage('helpers/text.php', $this);
}


Then, I try to call the new method:
$objTh = Loader::helper('text');
$objTh->foo();


But no dice ("Call to undefined method TextHelper::foo").

I'm unclear how Environment::overrideCoreByPackage should be used. What am I doing wrong?

beebs93
View Replies: View Best Answer
JohntheFish replied on at Permalink Reply
JohntheFish
I have yet to experiment with this, but the common issue that seems to appear for those that have posted about it is to clear the override cache.

If you have already tried that, sorry, I don't know any more.
beebs93 replied on at Permalink Reply
beebs93
Thanks for the suggestion, but clearing the cache had no effect :(
mhawke replied on at Permalink Reply
mhawke
Just to clarify... clearing the cache is not the same as turning off the 'Overrides Cache'. As far as I understand, in order for C5 to even bother looking for your override files and functions, you need to turn off the overrides cache. Once you have stopped adding override files, you can turn that part of the cache system back on.
beebs93 replied on at Permalink Reply
beebs93
Thanks for the info. I noticed I had kept the cache overrides off the entire time so I can rule that out as a potential cause for this.
beebs93 replied on at Permalink Best Answer Reply
beebs93
Finally figured this out:

E.g. If you want to override the c5 Area model class...
<?php
// In the custom package's controller.php file
public function on_start(){
   $objEnv = Environment::get();
   $objEnv->overrideCoreByPackage('models/area.php', $this);
}


Instantiating the Environment class the traditional way or calling "overrideCoreByPackage" statically was the problem. The "get" method is where the magic happens.

Thanks for the suggestions here (and to the c5 forum folks) for trying to help me out :)
thronic replied on at Permalink Reply
thronic
I'm trying to do this but with libraries/view.php. I've used this as my example but for some reason it's still not working.

Is it possible to override the core libraries/view.php?

Maybe i'm missing something?
hissy replied on at Permalink Reply
hissy
I'm also trying to override helpers/file.php, but it doesn't works. To override models (eg. beebs93's code. thanks!) are works fine. Isn't there any solution to override helpers/libraries?

class MyPackage extends Package {
...
   public function on_start() {
      $env = Environment::get();
      $env->overrideCoreByPackage('helpers/file.php', $this);
   }
}
JohntheFish replied on at Permalink Reply
JohntheFish
For helpers, in the root overrides folder, the helper class name needs to begin class SiteXxxxxYyyyyyHelper {
hissy replied on at Permalink Reply
hissy
Thanks John the Fish, I want to override via my package.
But, my package works today! It was maybe a cache problem.
Thanks to folks!

class MyPackage extends Package {
...
   public function on_start() {
      $env = Environment::get();
      $env->overrideCoreByPackage('helpers/file.php', $this);
   }
}


class FileHelper extends Concrete5_Helper_File {
   public function mymethod() {
   }
}