1

Currently I have a field that extends BaseRelationField, and 2 types of elements. I want the field on elements A to be able to select from elements B.

So far the field itself works, which I tested by registering it and adding it to an entry. It shows elements B as options and stores it in the relations table.
So far so good.

But I don't want to add this field to an entry, but to elements A. My current attempts revolved around using Craft::$app->fields->createField() and Craft::$app->fields->saveField() to create the field (which also gives it an id) since Craft::$app->getRelations()->saveRelations() requires the field to have an id (which makes sense to me). But this means my field will show up in the settings/fields in the cp. Also using this method, I don't know how to properly retrieve the values to fill the index page of the element and the edit page.

Is there some way to add the field without adding it to the 'normal' part of the cp while being able to use the relations table or am I going at this completely wrong? And also how to retrieve the stored data in the most convenient way?

  • Why can't you just don't add it to the layout? Wouldn't it be enough to hide the field and not render it in an entries layout? I don't really see a reason to attach fields that much to an element type since that's somehow against Crafts current idea behind fields. – Robin Schambach Feb 25 '19 at 18:41
  • @RobinSchambach I'm trying to keep the module completely self-contained as I cannot trust those that will use it to not mess things up. If the field is available in the settings they might mess with those settings. I mainly find it weird that I can add standard stuff (like text fields) to an element without it showing up in the fields, but not relation fields. So I assume that I'm missing something – bastiaanwuisman Feb 26 '19 at 10:12
  • When you add a text field to a custom element type it will show up in the field settings and in the field layout as well. Keep in mind there is a difference between fields and object properties and macros. The properties of your object and html rendered via macro don't appear in the field settings but all fields in your field layout will be there. So there is no way to add a text field to an elements layout that is not in the settings. – Robin Schambach Feb 26 '19 at 10:22
  • Aah oke, I'll keep going that way and if I find any pitfalls I'll add it here for future reference if anyone stumbles across this question. Thanks for the help @RobinSchambach ! – bastiaanwuisman Feb 27 '19 at 15:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.