Admin plugin or twig

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.

@julien

Twig:
In Twig, an if-statement must be enclosed in {% %} not {{ }}…

Because of these “typo’s” every ‘suite-range’ assignment is executed and the last assignment wins…

Plugin:
In the plugin, during the ‘onPluginInitialized()’ method the event ‘onAdminSave’ is never enabled because code execution is terminated by the following statement:

// Don't proceed if we are in the admin plugin
if ($this->isAdmin()) {
   return;
}

Code should be:

// Don't proceed if we are NOT in the admin plugin
if (!$this->isAdmin()) {               <- Note the '!' which means 'not'
   return;
}

I can imagine the comment “// Don’t proceed if we are in the admin plugin” is confusing, because we áre not in the admin plugin, but in our own plugin… Maybe the comment “// Don’t proceed if we are in the Admin panel” would make more sense.

Thank you very much @pamtbaau !

Twig:
Of course! {{ }} are to display data and {% %} is to set/manipulate data.
Thank you for opening my closed eyes !
it worked right away.

Plugin:
Thanks for clarifying that. I still don’t get the value saved to the frontmatter of the suite.
I will have to get back to this one. I always like to make things work. It’s a good way to learn.
At least for now, I can wrap up this part of the site.

Have a great one !

@julien With respect to the plugin, I suspect the condition ‘$page instanceof Page’ fails because ‘Page’ is unknown.

You could try one of the following:

  • Define the proper ‘use’ as: use Grav\Common\Page\Page as Page;
  • Or change the condition into: $page instanceof Grav\Common\Page\Page

One other thing, an exception will be thrown if the user enters a value >= 80000. In that case $sizeRange will not be defined. Adding a default case statement that catches values > 80000 can prevent the exception.

Hope this helps.