How to override only the controllers function?

Permalink 2 users found helpful
Hi All

As i am new to c5 i need your help for c5 packages block and overridden block

-Here i have one package that contains one block in it as i want the template of that to be custom so i added the custom template in the block folder(on root) and that is working fine but for that i need one function to make in controller so i also copied the controller from the package block to the block but now i want only the newer function to be executed from my outer controller file and other function like view and other to be executed from inner file(the packages block file) so is it possible that we can access few function of the controller from the inner file of the packages block and other from outer?

 
jordanlev replied on at Permalink Reply
jordanlev
I think you will need to create another function in the controller and somehow call that from the custom template (so the custom template uses one function, but the normal template defaults to the normal function). However, depending on the block you're trying to do this to it might be complicated. Which block is it? And what exactly are you trying to do differently?
ScottC replied on at Permalink Best Answer Reply
ScottC
if you want to do that you have to use Loader::controller('block_type_name'); or maybe you pass in a blocktype object, I can't remember.

Then in your custom function you would want to grab a new instance of the block controller, set the properties you want using that, and then have the view call the function by doing $this->controller->functionName(); and it can act on its return value.

Alternatively, you can have your custom controller extend that other block controller(which is extending controller so its has all the controllery stuff) and just have that custom function in it, and call the parent:: functions if you need to.
savan replied on at Permalink Reply
Hi ScottC,

Thanks a lot for your reply, it helped me thanks

savan
aghouseh replied on at Permalink Reply
aghouseh
I think this is close to what I'm trying to do.

I used the Designer Content block (which is super smooth) and then did some customization to be able to have a main "news" page with a setting to be saved in the block to flag the article for display on another page. I'm then calling that area from the other page and trying to check that saved value with a custom function in the custom block controller, but it's not able to access it. I'm getting this error:

Fatal error: Call to a member function getArticleDistribution() on a non-object.

Here's the code I'm using.

$newsPage = Page::getByID(155);
$news = $newsPage->getBlocks("Main");
foreach ($news as $article) {
 if ($article->getArticleDistribution() == $c->getCollectionHandle()) {
  echo '<div class="article">';
  echo $article->display();
  echo '</div>';
 } 
}


I feel like I'm missing a step to call the controller of the custom block somewhere, but I'm fairly new at most of this. Thanks in advance for any advice.
jordanlev replied on at Permalink Reply
jordanlev
That's really funny -- I just had to do the *exact* same thing for a project of mine. What you're missing is a call to "getInstance()", like this:
echo $article->getInstance()->display();
aghouseh replied on at Permalink Reply
aghouseh
I'm actually getting the content to display properly, but the call I'm making here is throwing the error.

$article->getArticleDistribution()


I'm just trying to access a function within the controller of the custom block, but perhaps I'm going about it entirely wrong.
jordanlev replied on at Permalink Reply
jordanlev
Did you try $article->getInstance()->getArticleDistribution()
aghouseh replied on at Permalink Reply
aghouseh
Jordanlev for prez.

That did it! Thanks for your help.