C::get('content.file.extension') does not return default txt

In a plugin, I write this:

echo c::get('content.file.extension');

In the docs it says that the default for this config is txt:

https://getkirby.com/docs/cheatsheet/options/content-file-extension

Should’nt it return txt then if I did not set anything?

<?= $kirby->option('content.file.extension') ?>

does

c::$data only returns what’s actually defined in your config files.
$kirby->options() returns the complete options settings, i.e. defaults + user defined settings

This is the line where the merging happens (/kirby/kirby.php, line 171)

$this->options = array_merge($this->options, c::$data);

Reading source code is so enlightening, isn’t it :wink:

1 Like

I tried to use this within the Settings class (by @jenstornell ), located in a plugin, but couldn’t get it to work: $kirby->option('thumbs.quality') always returns NULL … (same for kirby())

:confused:

I’m not quite sure I understand. Where exactly do you use this? Could you post the complete code?

Edit: thumbs.quality is not a Kirby default option, it is a default set in the thumbs class.

Edit2: Interestingly, if you “remind” the plugin that there is a thumbs component, you can then access the option:

$thComponent = kirby()->component('thumb');
echo kirby()->option('thumbs.quality');

No idea, why.

1 Like

The reason for this is that the thumbs component is lazy-loaded, i.e. an instance of the component is only created when the thumb method is called. Therefore, the options are not defined before that happens. Thanks to @lukasbestle for the explanation.

I’m talking about this file in specific:

class Settings {

  /**
   * Returns the default options for `kirby-webp`
   *
   * @return array
   */

  public static function __callStatic($name, $args) {

    // Set prefix
    $prefix = 'plugin.kirby-webp.';

    // Set config names and fallbacks as settings
    $settings = [
      // TODO: $kirby->option('thumb.quality') ?
      'actions'             => ['upload'],
      'quality'             => 90, // Desired WebP compression quality
      'stripMetadata'       => TRUE,
      'serveConverted'      => FALSE,
      'serveOriginalOnFail' => TRUE,
      'preferredConverters' => ['gd', 'webp', 'imagick'] // TODO: include 'thumbs.driver'
    ];

    // If config settings exist, return the config with fallback
    if(isset($settings) && array_key_exists($name, $settings)) {
      return c::get($prefix . $name, $settings[$name]);
    }
  }
}

… and for the quality setting, I want whatever’s value is set (Kirby’s default being 90, otherwise whatever manually with c::set('thumbs.quality', $value)) - also as preferredConverter, I’d like thumbs.driver first, then webp.

So, basically calling thumb() makes these available via kirby()->option()? (And there’s really no easier way?)

As Sonja wrote, you can call kirby()->component('thumb'). There is no need to actually generate a thumb, you just need to load the thumb component. This also won’t break anything else, so it’s fine to do this.

1 Like