<p>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.</p>

<p>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.<br>
So far so good.</p>

<p>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.</p>

<p>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?</p>

CC BY-SA 4.0
2019-02-25T18:41:58.663Z
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
CC BY-SA 4.0
2019-02-26T10:12:35.360Z
@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
CC BY-SA 4.0
2019-02-26T10:22:43.047Z
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
CC BY-SA 4.0
2019-02-27T15:57:23.207Z
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
<relations><modules>
How do I use a relation field in an element?
# How do I use a relation field in an element?

(Asked by: username_0 on 2019-02-25)

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?

**Tags:** relations, modules
> **Comments:**
> **username_1**: 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.  
> **username_0**: @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  
> **username_1**: 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.  
> **username_0**: 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 !  

---

## 0 answers

