Hi,
Here is what I am trying to achieve.
I have properties (modular pages) and suites (modules) added to each of them. Everything is working fine and display correctly.
Each suite (module) has a value of size which is a number.
In the loop that displays each module, I need to convert the size as a range.
Example:
{% set suite_range = [] %} {% for module in suites_collection %} {{ if (module.header.suite_size >= 100) and (module.header.suite_size <= 5000) }} {% set suite_range = 'size-under5k' %} {{ elseif (module.header.suite_size >= 5001) and (module.header.suite_size <= 10000) }} {% set suite_range = 'size-5k-10k' %} {{ elseif (module.header.suite_size >= 10001) and (module.header.suite_size <= 20000) }} {% set suite_range = 'size-10k-20k' %} {{ elseif (module.header.suite_size >= 20001) and (module.header.suite_size <= 40000) }} {% set suite_range = 'size-20k-40k' %} {{ elseif (module.header.suite_size >= 40000) and (module.header.suite_size <= 80000) }} {% set suite_range = 'size-40k-80k' %} {{ endif }} {{ module.header.suite_size }} = {{ suite_range }}<br /> {% endfor %} <hr /> {% endfor %}
But {{ suite_range }} is always showing size-40k-80k no matter what the actual suite_size is.
In fact, there is no suite with a size greater than 40000.
6984 = SIZE-40K-80K
12387 = SIZE-40K-80K
22728 = SIZE-40K-80K
Does anybody know why it’s always setting “suite_range” with the last condition ?
I also tried to create an admin plugin that would simply save in the header a variable called “suite_range”. So the plugin would check the value, go through a switch with cases and set the value in the header of module upon save.
Unfortunately, my php skills are limited and my plugin was doing nothing.
I use the DevTools to create a plugin and here is what I had put in it.
namespace Grav\Plugin;
use Grav\Common\Plugin;
use Grav\Common\Grav;
use Grav\Common\Page\Collection;
use Grav\Common\Page\Page;
use Grav\Common\Page\Pages;class SizeRangePlugin extends Plugin
{
public static function getSubscribedEvents()
{
return [
‘onPluginsInitialized’ => [‘onPluginsInitialized’, 0]
];
}public function onPluginsInitialized() { // Don't proceed if we are in the admin plugin if ($this->isAdmin()) { return; } // Enable the main event we are interested in $this->enable([ 'onAdminSave' => ['onAdminSave', 0] ]); } public function onAdminSave(Event $e) { // get the ojbect being saved $page = $e['object']; if ($page instanceof Page && $page->template() == 'suite' ) { $header = $page->header(); if ( isset( $header->suite_size ) ) { $suiteSize = $header->suite_size; switch ($suiteSize) { case ($suiteSize < 5000): $sizeRange = 'size-under5k'; break; case (($suiteSize > 5001) and ($suiteSize < 10000)): $sizeRange = 'size-5k-10k'; break; case (($suiteSize > 10001) and ($suiteSize < 20000)): $sizeRange = 'size-10k-20k'; break; case (($suiteSize > 20001) and ($suiteSize < 40000)): $sizeRange = 'size-20k-40k'; break; case (($suiteSize > 40001) and ($suiteSize < 80000)): $sizeRange = 'size-40k-80k'; break; } $header->size_range = $sizeRange; $page->header($header); }; } }
}
Thank you in advance for your help.