Override Image Block Controller

Permalink 2 users found helpful
Well I have checked the how-to for overrides.

I'm trying to create a package where I need to override image block. While looking into the view.php, it only calls a method getContentAndGenerate()

I'm in a need to override it. So I just copy it and put it in my packages block folder. But it doesn't work at all.

Here is the code:

<?php 
   defined('C5_EXECUTE') or die("Access Denied.");   
   class ImageBlockController extends BlockController {
      function getContentAndGenerateCustom($align = false, $style = false, $id = null) {
         echo 'test';
         $c = Page::getCurrentPage();
         $bID = $this->bID;
         $f = $this->getFileObject();
         $fullPath = $f->getPath();
         $relPath = $f->getRelativePath();         
         $size = @getimagesize($fullPath);
         if (empty($size)) {
            echo t( 'Image Not Found. ');
             return '';
         }


Am I doing anything wrong? Just check another add-on which overrides the content block & done the same process.

Rony

ronyDdeveloper
 
mhawke replied on at Permalink Reply
mhawke
The original controller extends 'Concrete5_Controller_Block_Image':

class ImageBlockController extends Concrete5_Controller_Block_Image {


but you are extending the 'BlockController':

class ImageBlockController extends BlockController {
landollweb replied on at Permalink Reply
landollweb
I believe the core block controllers used to extend the BlockController object prior to 5.6.0. I just noticed these new objects that the core block controllers are now extending, but the howto still reflects the older objects.

It would sure be nice to see some documentation about this change and what we're supposed to do with it. I've got block overrides and custom blocks I created earlier, and I'm not sure whether I have to update them to extend the new objects, or if our custom blocks are still supposed to use the old ones.
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
I've also tried to use
class ImageBlockController extends Concrete5_Controller_Block_Image {


but no luck

Rony
JohntheFish replied on at Permalink Reply
JohntheFish
As well as declaring the class you will need to actually make it override by either

- placing it in the relevant root folder

Or

- calling overrodeCoreByPackage() from an on_ start() handler.
JohntheFish replied on at Permalink Reply
JohntheFish
The root folder for the override should be /controllers
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
You mean I need to create a separate controller folder under packages directory?
JohntheFish replied on at Permalink Reply
JohntheFish
No. That root /controllers folder applies to root level block controller overrides, not overrides from packages.

As far as I know, the place for a block controller override in a package would be in an identical structure to if the block was part of the package.
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Yes the block that I'm using is Image block. And I need to override the blocks controller through package.

Rony
JohntheFish replied on at Permalink Reply
JohntheFish
In your package controller, set up an on_start handler that calls overrideCoreByPackage() to map the core to use your new controller.
ronyDdeveloper replied on at Permalink Reply
ronyDdeveloper
Anyone who can help me, please?

Rony
Charlie replied on at Permalink Reply
Just had the same scenario where I wanted to override the image block's controller to add some additional methods. Cache was disabled however when calling the new methods it was error as they didn't appear to exist.

For anyone else who stumbles across this, taking JohntheFish's advice I added the following to my packages on_start method which resolved the issue so many thanks John for pointing me in the right direction.

public function on_start() {
   $objPkg = Loader::package($this->pkgHandle);
   $objEnv = Environment::get();
   $objEnv->overrideCoreByPackage('blocks/image/controller.php', $objPkg);
}