For blocks using Stack field type - select2 requires in controller.php Q

Permalink Browser Info Environment
I've noticed when I created a block that uses the stack field type, the controller has this code

public function on_start()
{
$al = \Concrete\Core\Asset\AssetList::getInstance();
$this->requireAsset('css', 'select2');
$this->requireAsset('javascript', 'select2');
$al->register('javascript', 'select2sortable', 'blocks/testimonial_stack_output/js_form/select2.sortable.js');
}


The problem with this is that it's calling those libraries when the block is placed on a page and it's published/no longer in edit mode. You only need that select2 plugin for the select field in the edit form for choosing stacks. So it's creating 2 extra requests that don't really need to be there for just outputting the block, granted they aren't heavy but I am trying to be very lean and mean with the requests. I spent ages in 5.6 trying to keep requests down and it 5.7 seems to have some nicer tools to that end, so I am really trying to minimize the requests.

In concrete5 5.6 and under legacy, you could do something like this in the controller under a function
public function on_page_view() {
 $c = $this->getCollectionObject();
 if($c->isEditMode()) {
    // stuff to do when in edit mode
 }
}


I experimented a little bit and didn't have much luck getting that to work but I'm also not a core dev and 5.7 is all still pretty new to me. Digging through docs and forums I can't find a way folks are conditionally loading things based on edit mode in the controller in 5.7 so I figured you might know.

So my question really is:
Is it possible to only include those select2 requires when you're in edit mode?

Thanks

Type: Discussion
Status: Resolved
wildapple
View Replies:
ramonleenders replied on at Permalink Reply
ramonleenders
Hi there,

You could do this:

$c = Page::getCurrentPage();
 if($c->isEditMode()) {
    // stuff to do when in edit mode
 }


May require the "use Page;" statement at the top though, but you will notice quickly. Is this what you've been looking for perhaps?

Kind regards,

Ramon
wildapple replied on at Permalink Reply
wildapple
You know, I experimented with that also before and it never occurred to me that I needed the "use Page;" at the top.

That did do it though, I'm no longer seeing select2 in the published view source. Thanks for the assist on that!
ramonleenders replied on at Permalink Reply
ramonleenders
Glad that helped! :)

Were there css/js files in the source when not logged in you mean? Or what source do you mean exactly. If you can give me enough reason to push this into the core of block designer, I will of course!
wildapple replied on at Permalink Reply
wildapple
Yeah, you've got it right. After I placed a block on a page, published, logged out and looked at the page source the select2 assets were in the head(css) and footer(js).

I changed the on_start function in the controller to what you suggested

use Page; // up top with the other use statements...
public function on_start()
{      
$c = Page::getCurrentPage();
if($c->isEditMode()) {
 $this->requireAsset('css', 'select2');
 $this->requireAsset('javascript', 'select2');
}
$al = \Concrete\Core\Asset\AssetList::getInstance();
$al->register('javascript', 'select2sortable', 'blocks/testimonial_stack_output/js_form/select2.sortable.js');
}
ramonleenders replied on at Permalink Reply
ramonleenders
I've moved the "requireAsset" calls from on_start to add/edit functions! This will be in the new version of Block Designer. May take some more days till release, but at least you know it's taken care of!

Kind regards,

Ramon

concrete5 Environment Information

N/A - this doesn't really apply to any one site, just a general question.

Browser User-Agent String

Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.130 Safari/537.36

Hide Post Content

This will replace the post content with the message: "Content has been removed by an Administrator"

Hide Content

Request Refund

You may not request a refund that is not currently owned by you.