# Custom Fields + Set Relation in Plugin erstellen

**URL:** https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486
**Category:** Programmierung
**Created:** [18. Oktober 2019 um 13:43 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486 "2019-10-18T13:43:15Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![DenKon26](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@DenKon26](https://forum.shopware.com/u/DenKon26)
#### Post date: [18. Oktober 2019 um 13:43 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/1 "2019-10-18T13:43:15Z")

</div>

Hallo,

&nbsp;

Ich probiere gerade über ein Plugin custom Fields und ein custom Field set anzulegen.

Das klappt soweit mit folgendem Code:

&nbsp;

```
$customFieldSetRepository = $this->container->get('custom_field_set.repository');

$customFieldSetRepository->create([
    [
        'name' => 'my_test_logos',
        'config' => [
            'label' => [
                'de-DE' => 'Test Logos',
                'en-GB' => 'test logos'
                ]
            ],
        'customFields' => [
            [
                'name' => 'test_logo1_url',
                'type' => CustomFieldTypes::TEXT,
                'config' => [
                    'label' => [
                        'de-DE' => 'Artikel Logo 1',
                        'en-GB' => 'Article logo 1'
                    ],
                    'componentName' => "sw-media-field",
                    'customFieldType' => "media"
                ]
            ],
            [
                'name' => 'test_logo1_tooltip',
                'type' => CustomFieldTypes::TEXT
            ]
        ]
    ]
], $installContext->getContext());

```

&nbsp;

Nun habe ich im Code herausgefunden, dass über einen Parameter “relations” eine custom\_field\_set\_relation erzeugt werden kann.

&nbsp;

Wie kriege ich es also nun (am besten mit Codebeispiel) hin, beim Erstellen dieses Custom Field Sets auch gleichzeitig eine Relation auf “product” zu erzeugen?

&nbsp;

```
$customFieldSetRepository = $this->container->get('custom_field_set.repository');

$customFieldSetRepository->create([
    [
        'name' => 'my_test_logos',
        'config' => [
            'label' => [
                'de-DE' => 'Test Logos',
                'en-GB' => 'test logos'
                ]
            ],
        'customFields' => [
            [
                'name' => 'test_logo1_url',
                'type' => CustomFieldTypes::TEXT,
                'config' => [
                    'label' => [
                        'de-DE' => 'Artikel Logo 1',
                        'en-GB' => 'Article logo 1'
                    ],
                    'componentName' => "sw-media-field",
                    'customFieldType' => "media"
                ]
            ],
            [
                'name' => 'test_logo1_tooltip',
                'type' => CustomFieldTypes::TEXT
            ]
        ],
        'relations' => [
            // was muss hier rein um eine relation zu "product" anzulegen?
        ]
    ]
], $installContext->getContext());

```

&nbsp;

&nbsp;

Vielen Dank im Voraus!

&nbsp;

Grüße

Dennis

---

<div class="post-metadata">

### Author: ![Krispin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/krispin/32/12849_2.png) [@Krispin](https://forum.shopware.com/u/Krispin)
#### Post date: [18. Oktober 2019 um 14:38 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/2 "2019-10-18T14:38:21Z")

</div>

Hi Dennis,

der JSON-String sieht dann folgender Maßen zu aus:

&nbsp;

```
{  
   "id":"b115ccb2e28c4465be04e634e5a39369",
   "name":"migration_tdztrz5656_product",
   "config":{  
      "label":{  
         "de-DE":"Product migration custom fields (attributes)"
      },
      "translated":true
   },
   "relations":[  
      {  
         "id":"9afdfcd84c5248e49d3ce176e6a91cc7",
         "entityName":"product"
      }
   ],
   "customFields":[  
      {  
         "id":"f0ecc93a01e74955840bd8e0d862418e",
         "name":"migration_tdztrz5656_product_attr2",
         "type":"text",
         "config":{  
            "componentName":"sw-field",
            "label":{  
               "de-DE":"Freitext-2"
            },
            "helpText":{  
               "de-DE":"Freitext zur Anzeige auf der Detailseite"
            },
            "customFieldPosition":2,
            "type":"text",
            "customFieldType":"text"
         }
      }
   ]
}

```

Somit bei dir dann in PHP:

```
'relations' => [
    [
       'id' => // eine UUID,
       'entityName' => 'product'
    ]
]

```

Hoffe das hilft dir.

Gruß

Krispin

---

<div class="post-metadata">

### Author: ![mdsw](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mdsw](https://forum.shopware.com/u/mdsw)
#### Post date: [23. Oktober 2019 um 13:50 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/3 "2019-10-23T13:50:21Z")

</div>

Gibt es hierzu ein How To? Wo muss die JSON liegen? Was ist noch notwendig?

---

<div class="post-metadata">

### Author: ![Krispin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/krispin/32/12849_2.png) [@Krispin](https://forum.shopware.com/u/Krispin)
#### Post date: [23. Oktober 2019 um 14:14 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/4 "2019-10-23T14:14:13Z")

</div>

Hi mdsw,

die JSON habe ich nur dazu geschrieben, da dies genutzt werden kann um per JSON-API-Call die Daten zu erstellen.  
Wenn man das in PHP macht, wie bei DenKon26, dann muss man daraus ein Array machen (wie auch in meiner Antwort beschrieben).  
Hoffe das hilft dir weiter.

Gruß

Krispin

---

<div class="post-metadata">

### Author: ![mdsw](https://avatars.discourse-cdn.com/v4/letter/m/94ad74/32.png) [@mdsw](https://forum.shopware.com/u/mdsw)
#### Post date: [24. Oktober 2019 um 05:55 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/5 "2019-10-24T05:55:22Z")

</div>

Achso, das macht man also einfach wieder in der Lifecycle-Methode InstallContext. Einen CRUD-Service oder sowas gibt es nicht?!

---

<div class="post-metadata">

### Author: ![Krispin](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/krispin/32/12849_2.png) [@Krispin](https://forum.shopware.com/u/Krispin)
#### Post date: [24. Oktober 2019 um 06:12 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/6 "2019-10-24T06:12:15Z")

</div>

Hi mdsw,

wenn man das über ein Plugin machen möchte, kann man das am Besten über das DAL machen.  
Da muss man dann kein JSON oder ähnliches schreiben, sondern man erstellt das Object per Array und speichert seine Daten über das Repository (wie es DenKon26 auch gemacht hat). Siehe Doku:&nbsp;[https://docs.shopware.com/en/shopware-platform-dev-en/internals/core/data-abstraction-layer/write?category=shopware-platform-dev-en/internals/core/data-abstraction-layer](https://docs.shopware.com/en/shopware-platform-dev-en/internals/core/data-abstraction-layer/write?category=shopware-platform-dev-en/internals/core/data-abstraction-layer)

Gruß

Krispin

---

<div class="post-metadata">

### Author: ![Liverson](https://avatars.discourse-cdn.com/v4/letter/l/b19c9b/32.png) [@Liverson](https://forum.shopware.com/u/Liverson)
#### Post date: [8. September 2020 um 08:04 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/7 "2020-09-08T08:04:12Z")

</div>

Hallo,

ich versuche grade via Plugin ein customField für die Entity „property\_group“ zu erstellen. In der DB wird in den Tabellen custom\_field\_… alles korrekt angelegt bei der Installation des Plguins. Allerdings wird bei den Eigenschaften kein Zusatzfeld im Admin angezeigt.

Wenn ich die custom\_field\_set\_relation testweise auf product mappe, wird in bei den Produkten das Zusatzfeld ausgegeben.

Kann mir jemand sagen, wie ich das Zusatzfeld bei dem Eigenschaften-Set reinbekomme?

Gruss,

Oliver

---

<div class="post-metadata">

### Author: ![Mirco](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@Mirco](https://forum.shopware.com/u/Mirco)
#### Post date: [24. Februar 2021 um 17:20 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/8 "2021-02-24T17:20:57Z")

</div>

> [@Liverson schrieb:](https://forum.shopware.com/profile/6799/Liverson "Liverson")
> 
> Hallo,
> 
> ich versuche grade via Plugin ein customField für die Entity „property\_group“ zu erstellen. In der DB wird in den Tabellen custom\_field\_… alles korrekt angelegt bei der Installation des Plguins. Allerdings wird bei den Eigenschaften kein Zusatzfeld im Admin angezeigt.
> 
> Wenn ich die custom\_field\_set\_relation testweise auf product mappe, wird in bei den Produkten das Zusatzfeld ausgegeben.
> 
> Kann mir jemand sagen, wie ich das Zusatzfeld bei dem Eigenschaften-Set reinbekomme?
> 
> Gruss,
> 
> Oliver

Hallo Zusammen,

habe das gleiche Problem wie Oliver. Versuche ein customField über den Code zu erstellen, jedoch bekomm ich es weder im ADM-Backend, noch beim Dump im der Storefront angezeigt. Das Feld existiert in der DB:

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/1/147f2a84b05e13069f8512ba9ec200e30770fa26.png)&nbsp;

Ich hätte vermutet, dass es zumidest im Dump angezeigt wird:

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/9/9eb4778e961e5bedf07ca29a9bd0846fe5bcfc6a.png)

Habe auch schon probiert das Feld dieser Doku&nbsp;[Add custom input field to existing component | Shopware Documentation](https://docs.shopware.com/en/shopware-platform-dev-en/how-to/add-admin-new-field) im Backend hinzuzufügen, allerdings weiß ich nicht wo es „einsortiert“ ist. Mein Versuch: „propertyGroup.translated.customFields.bit\_optionVisible“:

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/a/a6665e24b1c744188add73222ffc56c5b1f80357.png)

Der „Switch“ wird angezeigt, jedoch lässt er sich nicht speichern. Daher vermute ich das die Feld-Zuordnung falsch ist. Eine Fehlermeldung in der Konsole erscheint nicht.

 ![](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/2X/5/5171733538faadc8482a6b7807fb9a0c8337ae19.png)

&nbsp;

Kann mir hier jemand weiterhelfen, oder einen Tipp geben. Vielen Dank im voraus!

Mirco

---

<div class="post-metadata">

### Author: ![yicdeniz](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/yicdeniz/32/7847_2.png) [@yicdeniz](https://forum.shopware.com/u/yicdeniz)
#### Post date: [8. März 2021 um 15:09 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/9 "2021-03-08T15:09:37Z")

</div>

Ich habe ähnliche Probleme. Vielleicht ist jemand so freundlich und stellt ein Beispiel zu Verfügung?!..

---

<div class="post-metadata">

### Author: ![tomtomd](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tomtomd](https://forum.shopware.com/u/tomtomd)
#### Post date: [8. April 2021 um 13:03 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/10 "2021-04-08T13:03:52Z")

</div>

Weiß nicht, ob das jemandem hilft, aber hier mal ein komplettes Beispiel für die Plugin Installation:

```
<?php

declare(strict_types=1);

namespace MyPlugin;

use Shopware\Core\Framework\Plugin;
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\Uuid\Uuid;

class MyPluginClass extends Plugin
{
    public function install(InstallContext $installContext): void
    {
        $customFieldSetRepository = $this->container->get('custom_field_set.repository');

        $customFieldSetRepository->create([
            [
                'name' => 'mp_custom_field_set',
                'config' => [
                    'label' => [
                        'de-DE' => 'Ein Label'
                    ]
                ],
                'customFields' => [
                    [
                        'name' => 'mp_custom_field_set_number',
                        'type' => CustomFieldTypes::FLOAT,
                        'config' => [
                            'label' => [
                                'de-DE' => 'Ein Nummernfeld mit Zusatzoptionen'
                            ],
                            'type' => 'number',
                            'numberType' => 'float',
                            'customFieldType' => 'number',
                            'customFieldPosition' => 1,
                            'step' => 0.1,
                            'min' => 0,
                            'max' => 100
                        ]
                    ],
                    [
                        'name' => 'mp_custom_field_set_text',
                        'type' => CustomFieldTypes::TEXT,
                        'config' => [
                            'label' => [
                                'de-DE' => 'Ein Textfeld'
                            ],
                            'type' => 'text',
                            'customFieldType' => 'text',
                            'placeholder' => [
                                'de-DE' => 'Ein Platzhalter'
                            ],
                            'customFieldPosition' => 2
                        ]
                    ]
                ],
                'relations' => [
                    [
                        'id' => Uuid::randomHex(),
                        'entityName' => 'product'
                    ]
                ]
            ]
        ], $installContext->getContext());
    }
}

```

Die Zusatzoptionen lassen sich finde ich am einfachsten herausfinden, indem man sich ein Feld zusammenklickt und dann in der DB beim entsprechenden Eintrag in die `config` Spalte in der Tabelle `custom_fields` schaut. Hoffe es hilft.

Edit: Wenn jemand weiß, wie man die Sets dann wieder anständig löscht wäre ein Beispiel super. Ich würde das Repo mit einer `Criteria` auf `name` durchsuchen und die Matchings dann als Ids an `delete` übergeben. Gibt es da eine weniger umständliche Lösung?

---

<div class="post-metadata">

### Author: ![trade](https://avatars.discourse-cdn.com/v4/letter/t/5fc32e/32.png) [@trade](https://forum.shopware.com/u/trade)
#### Post date: [8. April 2021 um 13:39 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/11 "2021-04-08T13:39:41Z")

</div>

Gib doch beim Erstellen gleich eine id an und verwende diese dann zum lösche

---

<div class="post-metadata">

### Author: ![tomtomd](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@tomtomd](https://forum.shopware.com/u/tomtomd)
#### Post date: [29. April 2021 um 17:15 UTC](https://forum.shopware.com/t/custom-fields-set-relation-in-plugin-erstellen/62486/12 "2021-04-29T17:15:40Z")

</div>

@trade Stimmt, danke dir! So wie ich das mittlerweile verstanden habe, kann man entweder eine ID mitgeben, oder diese Property weg lassen, dann übernimmt das DAL das Anlegen der ID. Das Erstellen wäre also auch so möglich:

```
...
    'relations' => [
        ['entityName' => 'product']
    ]
...
```
