0

I'm trying to build a simple dropdown field with a pre-populated set of options, so I figured I'd just import most of the methods from Craft's native Dropdown field. The plugin name is siteutilities and the field name is Regionselect:

namespace kgrote\siteutilities\fields;

use kgrote\siteutilities\SiteUtilities;

use Craft;
use craft\base\ElementInterface;
use craft\fields\BaseOptionsField; 

This pulls in the code from craft/vendor/craftcms/cms/src/fields/BaseOptionsField.php.

So now I want to render a custom settings template for my plugin:

public function getSettingsHtml()
{
    return Craft::$app->getView()->renderTemplate(
        '_components/fields/Regionselect_settings',
        [
            'field' => $this,
        ]
    );
}

But I get a Twig error:

Unable to find the template “_components/fields/Regionselect_settings”.

My plugin file structure looks like this:

kgrote
|-siteutilities
  |-src
    |-fields
    |  |-Regionselect.php
    |-templates
    |  |-_components
    |    |-fields
    |      |-Regionselect_settings.html
    |-SiteUtilities.php
  • 1
    It searches for your template as well. Are you sure it's the correct path? Do you use a custom module instead of a plugin eventually? – Robin Schambach Sep 19 '18 at 22:07
  • @RobinSchambach I think so, I've updated the question with my plugin's file structure. And no I don't use a custom module, just a plugin. – kmgdev Sep 20 '18 at 19:57
  • 1
    Why a php file? You can only render twig and html files. All others won't be parsed/proceed by Twig – Robin Schambach Sep 20 '18 at 19:58
  • 1
    You need to include your plugin handle at the first position plugin-handle/_components/fields/Regionselect_settings – Robin Schambach Sep 20 '18 at 20:48
  • 1
    Sorry for being so slow. I somehow overview that mistake at the first sight. – Robin Schambach Sep 20 '18 at 21:09
1

In order to render a template within your custom plugin you need to include your plugin handle in front of your template path. For example

plugin-handle/_components/fields/Regionselect_settings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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