2

For example: Users. You can only have one fieldlayout per ElementType user which you customize in the settings section.

I've got as far as abstracting out the fieldlayout to it's own view, but when I go to set the field layout in the controller I'm borrowing the following code:

// Set the field layout
$fieldLayout = craft()->fields->assembleLayoutFromPost(false);
$fieldLayout->type = ElementType::MyElementType;

To which I get the following (pretty much expected) error:

 Fatal error: Undefined class constant 'MyElementType' in /controllers/MyElementTypeController.php on line 115

I've looked at how the plugin example "Events" is handling this and Brandon seems to be setting the fieldlayout->type to Assets:

$fieldLayout = craft()->fields->assembleLayoutFromPost();
$fieldLayout->type = ElementType::Asset;
$calendar->setFieldLayout($fieldLayout);

Is this how we're supposed to be setting these up? i.e., borrowing from other elementtypes?

2
  • Quick question... Are you trying to permanently associate those fields with your plugin, or can they be different for every website which uses your plugin? (If you're not publicly releasing your plugin, then it almost doesn't matter.) – Lindsey D Jun 20 '14 at 17:19
  • For learning purposes, I'd like them to be dynamic per install. So, different for every website which uses the plugin. – Peter Tell Jun 20 '14 at 17:20
4

You're bumping into this error because of a minor misunderstanding of how Pixel & Tonic are referencing their Element Types. The problem you have is with this line of code:

$fieldLayout->type = ElementType::MyElementType;

When P&T use that syntax, it's because they've already pre-defined all of their element types as enums. Open up the /app folder and take a look at this file to see what they've done:

/craft/app/enums/ElementType.php

This allows them to use a syntax like ElementType::Asset.

You don't have that option (unless you create your own enum). What you would do instead, simply use a normal string to specify your element type:

$fieldLayout->type = 'MyPlugin_MyElementType';

This is the same string that you wrote when you defined the Model for your Element Type. It should be one of the first lines of code within your Model:

protected $elementType = 'MyPlugin_MyElementType';

Setting up enums is a different topic, but just go ahead and use the string and it should resolve that error for you!

5
  • 1
    I think that I myself still call these enums and have done so incorrectly in a few CraftX hangouts that we've had but the technical term for them is actually Class Constants. Enum refers to special enumarated types in other languages like Java and they don't actually exist in PHP. I make this correction because I've been quoted on this before and wanted to make sure that I don't share false information with you guys. Keep on rocking bro, you're a master at this! – Selvin Ortiz Jun 20 '14 at 19:31
  • Awesome, thanks for the clarification @SelvinOrtiz! Just wondering, is it then accurate for Craft to have an "enums" directory, or is that a bit of a misnomer? (Not trying to call out P&T, just curious.) – Lindsey D Jun 20 '14 at 19:51
  • 1
    They are using them in the same way you would use enums, with a single purpose so unless you're a purist, their convention is totally acceptable IMHO. – Selvin Ortiz Jun 20 '14 at 19:58
  • 1
    This is great, thanks for the answer, explanation, and discussion guys. Very helpful! – Peter Tell Jun 20 '14 at 20:23
  • 1
    Well this answer just saved me a whole lot of heartache - thanks @LindseyD! Just goes to show, always search SE first... – Josh Angell Jan 20 '15 at 11:18

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.