1

We are using a custom variable initalized like this in our module:

    Event::on(CraftVariable::class, CraftVariable::EVENT_INIT,
        function (Event $event) {
            /** @var CraftVariable $variable */
            $variable = $event->sender;
            $variable->set('myvar', MyVar::class);
        }
    );

The variable can be access from TWIG via

    {{ craft.myvar.WHATEVER }}

But how can I access it from PHP, e.g. from an adjuster or another controller?

1 Answer 1

1

Because MyVar is a service, the easiest solution is to just access it the same way you'd access any other service, e.g.:

$whatever = MyModule::getInstance()->myVar->whatever
3
  • This does not work. It would require to keep a reference to myVar in MyModule, which works if I'm in control over my module but not for foreign craft.something variables. Commented Dec 16, 2022 at 13:00
  • Let's say I want to access craft.blitz from PHP... (Blitz Cache) Commented Dec 16, 2022 at 13:02
  • OK, at least for Blitz I found out, that you can simply use Blitz::$plugin-> to access the Blitz object. Commented Dec 17, 2022 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.