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
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
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
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 |
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!
That did do it though, I'm no longer seeing select2 in the published view source. Thanks for the assist on that!
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!
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!
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
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'); }
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
Kind regards,
Ramon
You could do this:
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