Skip to main content

Element Types in Craft CMS 2

Craft Element Types - Teaser

Element Types are a key part of Craft. With Craft 2, Pixel & Tonic is finally explaining in detail what they are and how they work.

One of the key innovations in Craft – make that the key innovation – is a new concept called “Element Types”. We’ve mentioned Element Types here and there, but we haven’t explained what they are yet, much less how plugins can take advantage of them. Now that Craft 2 is out, we’re finally ready to lift the curtain.

— Brandon Kelly

Elements

File systems manage files. iPhoto manages photos. iTunes manages media. Blogging systems manage entries. In most content management systems, managing content usually meant managing entries. Users, categories, and files could be managed too, but only entries had features like custom fields and relations.

Craft does things differently. Instead of just managing entries, Craft manages elements. Everything in Craft is an element: users, files, categories, tags, globals, Matrix blocks, and entries. Custom fields and relations are available for all elements, not just entries or users.

The difference from WordPress shows this clearly. In WordPress, each content type has its own API: posts use WP_Query, users use WP_User_Query, and taxonomies use get_terms(). Each function has its own parameters, quirks, and bugs. You have to manage relations between types manually with meta fields and IDs. Craft takes a different approach: everything is an element. There is one query API, one relations API, and one field system.

The seven Element Types in Craft 2

Craft 2 ships with seven built-in Element Types:

  • Entry – the main content type, organized into sections (Singles, Channels, Structures)
  • Asset – files and media, organized into Asset Sources
  • User – user accounts
  • Category – taxonomy, organized into Category Groups
  • Tag – lightweight tags, organized into Tag Groups
  • Global Set – site-wide content like footers or settings
  • Matrix Block – content blocks within Matrix fields

All seven share the same main features: custom fields, relations, search, status management, and localization. This is the core idea behind Element Types.

Element Types

Element Types are the classes that identify different elements in Craft. For example, there is a UserElementType, an AssetElementType, and an EntryElementType. If you have built a custom field type for Craft, you will notice the pattern: the relationship between an element and its Element Type is like the relationship between a field and its field type.

In templates, all Element Types can be queried through a unified API:

    {% set entries = craft.entries.section('articles').limit(10).find() %}
{% set assets = craft.assets.kind('image').find() %}
{% set users = craft.users.group('authors').find() %}

Each Element Type sets which fields can be searched, which attributes can be used for sorting, how elements appear in table views, and how they are saved.

Plugin developers benefit

It took some time for the Element Type API to be ready for plugins. With Craft 2, that is now possible. Plugin developers can create their own Element Types and add new kinds of content that fit right into Craft. For example, if you build an event system, you get search, relations, custom fields, a CP index view, and localization included.

Pixel & Tonic has already shown a private demo of the API to some plugin developers. In two weeks, there will be a three-hour workshop on Element Type development at Peers.

Conclusion

Element Types are more than just a technical detail. They shape how the whole system works.

Craft values consistency instead of special cases. There is one field system, one query API, and one relations API for everything. If you know how entries work, you also understand assets, users, and categories. Custom fields are not limited to entries.

Plugin developers benefit from this extensibility. A custom Element Type automatically gets search, relations, custom fields, a CP view, and localization. This is why the Craft plugin ecosystem creates powerful plugins, even with a smaller community.

The data model is future-proof. Since all elements follow the same rules, Pixel & Tonic can improve the whole system at once in future versions. For example, they can boost query performance or add eager loading without updating each Element Type separately.

Without Element Types, Craft would be just another CMS. With them, everything clicks into place.

Thomas Sausen

Web Developer

Self-employed web developer from Germany who started with WordPress websites in 2005, then moved to ExpressionEngine and lost his heart to Craft CMS in 2013. As the founder of Craftentries, he has been covering the Craft ecosystem since 2015.