rasteiner / k3-awesome-picker

Kirby 4 field plugin to show and select a free Font Awesome icon.
MIT License
14 stars 0 forks source link

Dynamic config options possible? #20

Closed Werbschaft closed 9 months ago

Werbschaft commented 9 months ago

I am currently trying to change the styles globally using a dropdown in $site because I have installed the icon field at least 20 times and don't always want to change the YAML in the $page Blueprints. I have already tried the following:

My option in the $site

weight:
  type: select
  options:
    "light" : "Light"
    "bold" : "Bold"

That's the config.php

'rasteiner.awesome-picker' => [
  'css-url' => function() {
    return url('assets/css/icons.css');
  },
  'meta-source' => 'assets/css/icons.yml',
  'default-styles' => function($kirby) {
    if ( $kirby->site()->weight()->value() == 'light' ) {
      return ["brands", "light"];
    }
    if ( $kirby->site()->weight()->value() == 'bold' ) {
      return ["brands", "bold"];
    }
  },
  'loaded-styles' => function($kirby) {
    if ( $kirby->site()->weight()->value() == 'light' ) {
      return ["brands", "light"];
    }
    if ( $kirby->site()->weight()->value() == 'bold' ) {
      return ["brands", "bold"];
    }
  },
]

Did you see any chance how this could work? I always get a blank field back. Tried it also in the "ready" state of the config but with the same result.

rasteiner commented 9 months ago

Hello, sorry for the wait and thanks for the sponsorship. It's a busy week... 0.3.1 should work for you.

The code would be slightly different as I don't pass $kirby into the function, but you can get to it simply with kirby(), or in your case, actually directly site() would work. Also, don't forget to return a default value for when site()->weight() is empty.

'rasteiner.awesome-picker' => [
  'css-url' => function() {
    return url('assets/css/icons.css');
  },
  'meta-source' => 'assets/css/icons.yml',
  'default-styles' => function() {
    if ( site()->weight()->value() == 'light' ) {
      return ["brands", "light"];
    }
    if ( site()->weight()->value() == 'bold' ) {
      return ["brands", "bold"];
    }

    return ["brands"];
  },
  'loaded-styles' => function($kirby) {
    if ( $kirby->site()->weight()->value() == 'light' ) {
      return ["brands", "light"];
    }
    if ( $kirby->site()->weight()->value() == 'bold' ) {
      return ["brands", "bold"];
    }

    return ["brands"];
  },
]

Closures in arrays are now also unpacked, so you could actually only have a closure on the light / bold styles:


<?php

$styles = [
    'brands',
    fn() => match (site()->weight()->value()) {
        'light' => 'light',
        default => 'bold',
    }
];

return [
    // ...
    'rasteiner.awesome-picker' => [
        'css-url' => fn() => url('assets/css/icons.css'),
        'meta-source' => 'assets/css/icons.yml',
        'default-styles' => $styles,
        'loaded-styles' => $styles,
    ],
];
Werbschaft commented 9 months ago

Hi there,

thanks so much, it's working awesome!! Have also your whenQuery Plugin in usage, it's also really amazing!