<p>I am creating a plugin with a number of custom elements. </p>

<p>Being new to craft, I have come a long way using documentation and skimming existing code. I am stuck on the following issue. I added a field layout to a custom element following the instructions in the craft documentation (<a href="https://docs.craftcms.com/v3/extend/element-types.html#element-content" rel="nofollow noreferrer">https://docs.craftcms.com/v3/extend/element-types.html#element-content</a>). </p>

<p>I created the cp templates to edit and save the layout. I made the cp templates to create/edit the custom element. The field layout comes up nice and I can edit and save all the fields except when I add an asset field. I my case I want to add a photo. The asset field comes up in the screen, I can select a photo, it will show on the screen. But when I save en go back to the edit , the image is not saved. </p>

<p>I figured out that that field text content is saved in the content table and this is working fine (save, edit, delete ...) The assets seems to be handled in the relation database and I see nothing being saved in there. When I use my asset field for example in the user field layout , the data is saved. I double checked the docu and other code an do not seem to find anything else that explicitly saves this relation in the database. </p>

<p>I am developing on the latest version of Craft. Any help would be appreciated</p>

<p>Thank you</p>

<p>Bart</p>

CC BY-SA 4.0
2020-01-02T16:57:02.017Z
if you return to edit page, probably there is an error on saving element. so could you check this:  if (Craft::$app->elements->saveElement($element)){
craft::dd($element->getErrors())
} and see if there is any error?
user9975
CC BY-SA 4.0
2020-01-02T18:00:31.347Z
Hi, get an empty array back (ie no errors).... The element saves as expected , including any text fields from the field layout
Bart Roelands
CC BY-SA 4.0
2020-01-02T18:15:02.427Z
could you see posted asset param via browser console? it is something like fields[assetfieldhandle][]='asset id'. also make your asset field required and see if element saves again or not.
user9975
CC BY-SA 4.0
2020-01-02T18:41:55.283Z
also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error.
user9975
CC BY-SA 4.0
2020-01-02T18:46:17.560Z
Strange this is when I make the field required, and leave it empty, no errors are generated when saving the element ...
Bart Roelands
CC BY-SA 4.0
2020-01-02T18:52:25.063Z
I get the fields from the post request using the function setFieldValuesFromRequest('fields'); When I dump the request and add an asset I get following array [
    'moduleDisplayName' => 'Test 1'
    'moduleDisplaySummary' => '<p>Test 1</p>'
    'moduleDisplayImage' => [
        0 => '998'
    ]
]   The text fields are saved (in content table), the Image not (in relations table)
Bart Roelands
CC BY-SA 4.0
2020-01-02T18:55:53.680Z
what about this?  "also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error"
user9975
CC BY-SA 4.0
2020-01-02T18:59:42.843Z
That is set, also gives no errors when left empty. The strange this is, when I use teh same fields in a field layout for example with the standard craft user, the asset is save correctly. Does not seem to be a field problem. There must be something wrong in the way I use the field layout in my custom element, although I followed anything in the manual ...
Bart Roelands
CC BY-SA 4.0
2020-01-02T19:06:32.380Z
I found it ! Turned out I forgot to call  parent::afterSave($isNew); in the Element aftersave function ....
Bart Roelands
CC BY-SA 4.0
2020-01-02T19:09:36.413Z
glad you found it. you can post your answer now :)
user9975
<field-layout-designer>
Asset field in custom element field layout not saved
# Asset field in custom element field layout not saved

(Asked by: username_0 on 2020-01-01)

I am creating a plugin with a number of custom elements.

Being new to craft, I have come a long way using documentation and skimming existing code. I am stuck on the following issue. I added a field layout to a custom element following the instructions in the craft documentation (<https://docs.craftcms.com/v3/extend/element-types.html#element-content>).

I created the cp templates to edit and save the layout. I made the cp templates to create/edit the custom element. The field layout comes up nice and I can edit and save all the fields except when I add an asset field. I my case I want to add a photo. The asset field comes up in the screen, I can select a photo, it will show on the screen. But when I save en go back to the edit , the image is not saved.

I figured out that that field text content is saved in the content table and this is working fine (save, edit, delete ...) The assets seems to be handled in the relation database and I see nothing being saved in there. When I use my asset field for example in the user field layout , the data is saved. I double checked the docu and other code an do not seem to find anything else that explicitly saves this relation in the database.

I am developing on the latest version of Craft. Any help would be appreciated

Thank you

Bart

**Tags:** field-layout-designer
> **Comments:**
> **username_1**: if you return to edit page, probably there is an error on saving element. so could you check this:  if (Craft::$app->elements->saveElement($element)){ craft::dd($element->getErrors()) } and see if there is any error?  
> **username_0**: Hi, get an empty array back (ie no errors).... The element saves as expected , including any text fields from the field layout  
> **username_1**: could you see posted asset param via browser console? it is something like fields[assetfieldhandle][]='asset id'. also make your asset field required and see if element saves again or not.  
> **username_1**: also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error.  
> **username_0**: Strange this is when I make the field required, and leave it empty, no errors are generated when saving the element ...  
> **username_0**: I get the fields from the post request using the function setFieldValuesFromRequest('fields'); When I dump the request and add an asset I get following array [     'moduleDisplayName' => 'Test 1'     'moduleDisplaySummary' => '<p>Test 1</p>'     'moduleDisplayImage' => [         0 => '998'     ] ]   The text fields are saved (in content table), the Image not (in relations table)  
> **username_1**: what about this?  "also go to your field settings for that asset and make sure if Validate related assets is enabled to prevent element save on asset error"  
> **username_0**: That is set, also gives no errors when left empty. The strange this is, when I use teh same fields in a field layout for example with the standard craft user, the asset is save correctly. Does not seem to be a field problem. There must be something wrong in the way I use the field layout in my custom element, although I followed anything in the manual ...  
> **username_0**: I found it ! Turned out I forgot to call  parent::afterSave($isNew); in the Element aftersave function ....  
> **username_1**: glad you found it. you can post your answer now :)  

---

## 0 answers

