How to call a block controller custom public method?

Permalink 1 user found helpful
My block controller is like this :
<?php
namespace Application\Block\MCQ;
use Concrete\Core\Block\BlockController;
class Controller extends BlockController
{
    private $answerState = '';
.....
    public function getAnswerState(){
      return $this->answerState;
    }
}


From another block, I want to check the state of all MCQ blocks on the current page.
$blocks = Page::getCurrentPage()->getBlocks('MCQArea');
foreach ($blocks as $b) {
     echo $b->getController()->getAnswerState();
}

The problem is that the getController method gives me a generic BlockController and I obtain the following error
Call to undefined method Concrete\Block\Content\Controller::getAnswerState()

How to cast the generic BlockController into a MCQ BlockController in order to call the method getAnswerState? If this is not possible what is the concrete5.7 way of doing this?

Thank you, any ideas are welcome.

View Replies: View Best Answer
A3020 replied on at Permalink Best Answer Reply
A3020
Are you sure you have only one type of blocks in that area? Because it looks like it also retrieves a Content block. And obviously that block controller doesn't have the method you try to call.

You'd wrap it in an if statement with $b->getBlockTypeHandle()
VonUniGE replied on at Permalink Reply
You are right, there was a lost content block in my area. I should check the block type before to call this method.

Thank you!