I am building a plugin in Craft, I want to use the Element Types, the documentation seems so hard to grasp. Can you give an example or a template code?
Element Types are powerful and there are probably too many possible implementations to give a very meaningful example without additional information.
Here are a couple resources beyond the docs that could help you get started:
- Master the Elements - A comparison of all native Craft CMS Element Types and what makes them unique.
- Introduction to Element Types in Craft CMS - In this Hangout Brandon Kelly walks through a lot of details about Element Types. There are also a couple of other related videos you can find in the resources section here: Getting Started with Plugin Development for Craft CMS
- Events Sample Plugin - Example plugin that demonstrates how Craft plugins can take advantage of Craft’s Element Type APIs.
I'd recommend taking a close look at the
ElementType Class and related Element Type
Model Class for each of the native Elements in Craft:
- Global Set
- Matrix Block
Each Element implements the API slightly differently and understanding those differences will help you make sense of what you may need to include in your Element, or where you can piggy back on existing behavior.
There are also several plugins that use Elements. Each implementation is fairly custom, so which examples will work best for you are hard to say. Off the top of my head, there are plenty of examples:
- Commerce_Product Element
- Commerce_Order Element
- SproutForms_Form Element (Sprout Forms)
- SproutForms_Entry Element (Sprout Forms)
- SproutEmail_NotificationEmail Element (Sprout Email)
- SproutEmail_CampaignEmail Element (Sprout Email)
- SproutEmail_SentEmail Element (Sprout Email)
- SproutSeo_Redirect Element (Sprout SEO)
A few others (if anybody would like to share others in the comments, happy to add them to the list)
- Neo_Block Element (Neo)
- SuperTable_Block Element (Super Table)