"If default language in panel" do hook

Hi everyone, I have one hook that would like to work only if the default language is selected in the panel.

// Write custom CSS to file
    'hooks' => [
      'site.update:after' => function($newSite) {
    
        $css = kirby()->languages()->default() ? $newSite->customCss()->value() : null;
        F::write(kirby()->root('assets') . '/css/site.css', $css);

      }
    ],

It is a Custom CSS field that writes to a CSS file. I made that field untranslatable, but if anything is saved on the secondary language site.yml it erases everything in the css/site.css file.

I tried adding “if/else” kirby()->languages()->default but that doesn’t influence anything.

Any idea how could that work?

This condition is always true (because the code return the default language and doesn’t check if the current language is the default language), you need to check the current language

kirby()->language()

returns the current language. And with

kirby()->language()->isDefault()

you can check if the current language is the default language.

And then only write conditionally, because otherwise you write null to the file.

Thank you, Sonja :pray: As always another good solution for you :muscle:

I just changed the logic a bit, and it works just the way I wanted

// Write custom CSS to file
    'hooks' => [
      'site.update:after' => function($newSite) {
    
        $css = $newSite->customCss()->value();
          if (kirby()->language()->isDefault()) {
            F::write(kirby()->root('assets') . '/css/site.css', $css);
        }
      }
    ],

So here is a solution if someone else needs it.

Thank you!