1

I have a custom field with a setting for regionType, but when I try to save the field I get this error:

Setting unknown property: kgrote\siteutilities\fields\Regionselect::regionType

In my fieldtype file I have this to load the settings field:

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

And the settings template looks like this:

{% import "_includes/forms" as forms %}

{% set input %}
    {{ forms.select({
        id: 'regionType',
        name: 'regionType',
        options: ({
            'usStates' : 'US States & Canadian Provinces',
            'countries' : 'Countries'
        })
    }) }}
{% endset %}

{{ forms.field(
    {
        label: "Region Type"|t('app')
    }, 
    input 
) }}

My field is a simple pre-filled dropdown, so I'm importing most of the methods for my field from craft/vendor/craftcms/cms/src/fields/BaseOptionsField.php.

I don't see much in the source code that relates to settings, so I'm not really sure what's causing this error.

| improve this question | | | | |
1

The name of your select is regionType thus it will try to store the property regionType in your field. When you call the field value it will set the property value in you class.

You either need to include the property or change it to an existing one.

| improve this answer | | | | |
  • Got it, thanks! Why is it that when I use settings.regionType in my template I get a value but when I use $this->getSettings()->regionType in my methods I get a Trying to get property of non-object error? – kmgdev Sep 22 '18 at 16:47
  • 1
    I'm not sure why you are using getSettings to begin with. It will return an array of your properties, so it's the same as this->regionType (just a little bit slower). Twig doesn't separate between array or object syntax, it's the same but in PHP you have a different syntax between arrays and objects – Robin Schambach Sep 22 '18 at 16:51
  • I think I'm not understanding how to access pre-stored settings for a field. In response to your original answer I just added public $regionType to my field class. But how do I grab the stored settings so I can use them to set which options display in the field? If I just use $this->regionType then all I get is a null value. I thought $this->getSettings() was the way to pull in the stored settings? – kmgdev Sep 22 '18 at 16:57
  • 1
    What are pre-stored settings? Each field has settings that represents certain attributes of the class. When you save a field Craft will serialize your body params and store them as settings in your fields table. When you populate it the next time Yii2 will configure your field class with those attributes that are stored as JSON in your fields table – Robin Schambach Sep 22 '18 at 17:07
  • Ah, OK I see now. I thought I had to explicitly go and grab the settings properties but they're already pre-loaded for me. – kmgdev Sep 22 '18 at 22:46

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.