8

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?

10

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:

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:

  • Asset
  • Category
  • Entry
  • Global Set
  • Matrix Block
  • Tag
  • User

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:

Craft Commerce:

  • Commerce_Product Element
  • Commerce_Order Element

Sprout Plugins:

  • 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)
3
  • No mention of P&T's Events plugin? It isn't meant as a full plugin, only as a practical example of how to build a custom Element Type.
    – Lindsey D
    Sep 25 '16 at 19:15
  • Added to the primary resource list. Sep 25 '16 at 21:26
  • Thanks for this wonderful resources Ben, This will help me to build a custom element element type.
    – uncle drew
    Sep 26 '16 at 2:02

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.