4

I’m working on a plugin that includes a custom fieldtype. Using Craft 2.6.2911, the fieldtype’s defineSettings looks like this:

public function defineSettings ( ) {
    return [
        'json_data'  =>  [
            AttributeType::String,
            'column'  =>  ColumnType::LongText
        ]
    ];
}

When I add this field to a tab group, the new column is added to the craft_content table as a varchar(255), not a text-type column. Is there an additional definition I need somewhere?

I did not see a relevant note in the changelog for builds of Craft between 2.6.2911 and 2.6.2944.

When reviewing my database schema, I'm seeing the following:

| field_jsonSync             | varchar(255)           | YES  |     | NULL    |                |

1 Answer 1

3

The Field Types defineSettings method defines the settings that get handled when you create the field. Those field settings get stored as json in the craft_fields.settings column when the field is created.

The BaseFieldType class has another method you can add to your class called defineContentAttribute. In your case, getting the column to be the data type you want would look like:

public function defineContentAttribute()
{
    return [
        AttributeType::String,
        'column'  =>  ColumnType::LongText
    ];
}

The PlainTextFieldType and RichTextFieldType classes have similar use cases which modify the default defineContentAttribute value if you want to take a look at a few more examples.

1
  • This was exactly what I had overlooked. Implementing this resolved my problem. Thank you.
    – Sonja Leaf
    Commented Sep 27, 2016 at 20:26

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.