Hello Kirby lovers,
This solution will be very important for me and hopefully to others because it helps to only use the CSS that is in use on the website.
I’m creating pages with the pagebuilder plugin and pages with sections.
I want to combine two list that will output one list that will be used to create a less file that contains the modules for creating css.
Both lists contain the same modules/building blocks and need to be reduced with array_reduce
The first list is for all the pages with sections
This code is adapted from k2 to k3 and it almost works.
It needs to function with page delete and update too and in this list the section- attribute needs to be removed in the name.
'hooks' => [
'page.create:after' => function () {
$activeModules = [];
foreach(site()->index()->filterBy('intendedTemplate', '*=', 'section-') as $m) {
$moduleName = $m->intendedTemplate();
if(!in_array($moduleName, $activeModules)) $activeModules[] = $moduleName;
}
// convert the list to a LESS string
$content = array_reduce($activeModules, function($carry, $template) {
return $carry . '@import' . ' "modules/' . $template . '.less";' . "\n";
}, '');
// write the LESS file
f::write(kirby()->root('assets') . DS . 'less' . DS . '_modules.less', $content, false);
}
],
The second list is for all the builder pages with modules/ building blocks.
Something like this
Step 2.1 get all the the builder templates
$site->index()->filterBy('template', 'builder')
Step 2.2 structure the builder pages
$builder ->pagebuilder()->toStructure()
Step 2.3 get the keys
print $section->_key()
Final steps
Now we need to combine the two arrays and use array reduce
We write less file with this code.
// convert the list to a LESS string
$content = array_reduce($activeModules, function($carry, $template) {
return $carry . '@import' . ' "modules/' . $template . '.less";' . "\n";
}, '');
// write the LESS file
f::write(kirby()->root('assets') . DS . 'less' . DS . '_modules.less', $content, false);