1

Getting error:

Calling unknown method: craft\web\twig\variables\CraftVariable::markAsReadModule()

Trying to use a template variable that I created in my Craft 3 module, I use this module for other things that work as expected but I am running into an issue with this in the template:

{{ craft.markAsReadModule.read(entry.id) }}

In the init method for craft/modules/markasreadmodule/MarkAsReadModule.php I register the variable:

I am using:

use craft\web\twig\variables\CraftVariable;
use modules\markasreadmodule\variables\MarkAsReadModuleVariable;

Then I have this in the init:

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

In craft/modules/markasreadmodule/variables/MarkAsReadModuleVariable.php

public function read($entryId)
{
    $service = new MarkAsRead(); // initialize the service class
    $read = $service->read($entryId);
}

I suspect I am setting the naming convention where I include it in the template and/or in the init method within craft/modules/markasreadmodule/MarkAsReadModule.php

3
  • Not seeing anything obviously wrong... are you sure your module is being bootstrapped/loaded?
    – Brad Bell
    Commented Jan 8, 2019 at 20:08
  • I goofed on the bootstrap array in the app.php config, thanks.
    – Brad
    Commented Jan 8, 2019 at 20:51
  • Glad you're sorted... would you mind adding your solution as an official answer? Might end up helping someone in the future.
    – Brad Bell
    Commented Jan 8, 2019 at 21:25

1 Answer 1

2

Was running into the same problem today. I was able to access the controller and the service, but none of the variables. Looks like Brad was able to fix the problem but didn't add a final solution.

I wasn't registering any variables on the init() function. Adding the last line of code registered the variable but did not load it onto every request.

    // Register our variables
    Event::on(
        CraftVariable::class,
        CraftVariable::EVENT_INIT,
        function (Event $event) {
            /** @var CraftVariable $variable */
            $variable = $event->sender;
            $variable->set('formSenderModule', FormSenderModuleVariable::class);
        }
    );

To add it onto every request, I had to add form-sender-module into my bootstrap array in app.php.

'bootstrap' => ['form-sender-module']

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.