3

I've created a custom module trying to implement new field types. I'm using How to create custom field type in Craft 3 and https://github.com/mmikkel/IncognitoField-Craft3 as reference on how to structure the module.

However, with a barebones version, I cannot seem to get the correct path to my templates. My module structure has the templates at:

SalesteamModule/src/templates/_components/fields/_input.twig

And the main function is:


public function getInputHtml($value, ElementInterface $element = null): string
    {
        // Get our id and namespace
        $id = Craft::$app->getView()->formatInputId($this->handle);
        $namespacedId = Craft::$app->getView()->namespaceInputId($id);

        // Render the input template
        return Craft::$app->getView()->renderTemplate(
            'salesteam-module/_components/fields/_input',
            [
                'name' => $this->handle,
                'value' => $value,
                'field' => $this,
                'id' => $id,
                'namespacedId' => $namespacedId,
            ]
        );
    }

App.php

return [

    // All environments
    '*'       => [
        'modules'   => [
            'site-module' => [
                'class' => \modules\sitemodule\SiteModule::class,
            ],
            'salesteam-module' => [
                'class' => \modules\salesteammodule\SalesteamModule::class,
            ],
        ],
        'bootstrap' => ['site-module', 'salesteam-module'],
    ],

    // Live (production) environment
    'live'    => [
    ],

    // Staging (pre-production) environment
    'staging' => [
    ],

    // Local (development) environment
    'local'   => [
    ],
];

The module is registered as "salesteam-module" in the app config, so I don't think it's a module id thing. And the path I copied the structure of the Incognito plugin I posted above. Although that repo is a plugin, not a module if that makes any difference.

Not sure where to go from here since I've tried a whole slew of different path combinations.

Thanks

1 Answer 1

4

If this is a module and not a plugin, you might need to declare your base template directory in the Module's __construct using:

Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (craft\events\RegisterTemplateRootsEvent $e) {
    if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
        $e->roots[$this->id] = $baseDir;
    }
});

See Andrews's article on the subject.

1
  • Thanks, this did the trick. Commented Apr 8, 2020 at 3:20

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.