1

I have a craft project which I've inherited and I'm trying to make some minor changes. I don't know PHP, Craft, or Twig very well so forgive me if this is an XY problem. This website is a company website and has pages for projects, jobs, case studies etc.

Basically, I have something called an entry in the templates and this has attributes like .title, .offerings etc. This entry is a project and if I view the text in the admin interface, I can see some fields there. There are parts of the code that look like this

  {% set projectDescription = entry.title%}

My question is, how do I know what are the attributes that entry has so that I can pull it out and display it?

Thanks!

1 Answer 1

3

The entry variable is an instance of craft\elements\Entry. Most of the attributes you are going to be interested in are either properties of the class or customf fields.

Class properties

All entries have properties like a the title, id, postDate, slug, authorId, etc. You can find them in the class reference for the Entry class. Check out the base Element class as well – all element types (entries, categories, assets, etc) inherit from it.

Custom fields

Custom fields are the fields you have defined in your Craft installation and added to the entry's Entry type. You can check the entry type in Settings -> Sections -> [Name of the section] -> Tab "Entry Types" -> [Name of the Entry type].

If you're not sure which section or entry type your entry has, you can find out like this:

{% dd [entry.section.name, entry.type.name] %}

You can also display field handles in the entry edit form. Go to your account preferences and check the option *Show field handles in edit forms *. Then go to edit the entry – the field handles will be displayed in the upper right of each field.

Finally, here's a handy oneliner to get an array of custom field handles for an entry:

{% dd entry.getFieldLayout().getFields()|column('handle') %}

Further reading: How to check for available fields for an entry

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.