Getting Controller variable from View file

Permalink
Quick question and hopefully an easy one...
I am creating a custom block template for the 'calendar event' block. I want to have the event category color displayed.

$bkcolor = $linkFormatter->getEventOccurrenceBackgroundColor($occurrence);

The core controller for this block seems to have the values already but they are not passed to the view file. I was able to make it work by modifying the core controller but I know that is a no-no. Here's how I modified it...

public function view()
    {
        $this->set('event', $this->getEvent());
        $displayEventAttributes = array();
        if (isset($this->displayEventAttributes)) {
            $displayEventAttributes = json_decode($this->displayEventAttributes);
        }
        $this->set('displayEventAttributes', $displayEventAttributes);
        $occurrence = $this->getOccurrence();
        $this->set('occurrence', $occurrence);
        $provider = $this->app->make(CalendarServiceProvider::class);
        if ($occurrence) {
            $this->set('event', $occurrence->getEvent());
            $linkFormatter = $provider->getLinkFormatter();
            $this->set('linkFormatter', $linkFormatter);// <-- This allows it to work properly but requires modifying the core controller


My question is... is there any way to grab the linkFormatter from the view file without having to modifying the core with $this->set('linkFormatter', $linkFormatter); ?

Thanks in advance,
C

View Replies: View Best Answer
mnakalay replied on at Permalink Best Answer Reply
mnakalay
Actually, in you case, you don't even need to do that. To get the link frmatter you did this
$provider = $this->app->make(CalendarServiceProvider::class);
$linkFormatter = $provider->getLinkFormatter();

There is nothing there that relies on anything from the controller, it's just generic code that would work anywhere. So you can use it directly in your view.

So in your view, all you need to do is
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
$provider = $app->make(\Concrete\Core\Calendar\CalendarServiceProvider::class);
$linkFormatter = $provider->getLinkFormatter();

And that's it you're good to go without overriding the controller
Chrouglas replied on at Permalink Reply
Awesome thanks.
Works like a charm.
Thanks for the quick response!
C