1

I need to get the field type in my templates like I used to do: field.getField.type but that does not seem to be working in Craft 3 anymore. The result is always null. It also seems that the »type« column is gone completely from the craft_fields table.

How do I get the field type in Craft 3?

3

There might be other ways, but here's one possibility:

{{ craft.app.fields.getFieldByHandle('yourFieldHandle').className() }}

This will return a string of craft\fields\PlainText, craft\fields\Lightswitch etc, which you can then use in conditionals/switch statements.

| improve this answer | |
  • Thank you, that worked! But why is it suddenly that complicated? – Marcus Scheller Jun 22 at 19:12
  • 1
    A question I ask myself almost daily... not so much in relation to Craft, but the whole web dev industry. I'm not best placed to answer it in relation to Craft, but I would hazard a guess that these kind of changes relate to the fact that Craft 3 is far more sophisticated than Craft 2: the added sophistication means technical debt needs to be very tightly managed, and limiting a field's output to be a simple string instead of a complex object with actionable methods like getField() is probably a significant performance boost. Just guessing, who knows... – James Smith Jun 23 at 10:20

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.