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