I have a node type "Kaboodle" and its image field is media entity reference field. I want to set default image fid in image field value if user has not uploaded any image on the field while saving node edit form or adding new node.

Please provide solution if anyone has or share your logic so that I can solve this.

Thanks

2

There are 2 best solutions below

2
On

In a hook_node_presave(NodeInterface $node), for the $node->bundle() you want, if the value of your media field is empty set it to the media entity id you want (be careful this media entity id might be different between your dev and prod environments, you might need to load the media by title property in order to get its id). Just set the value for the field, it will be saved automatically when node_save wil be called.

0
On

If you dont wanna save the data of your media you can even create a preprocess on your node type hook_preprocess_node__node_type(&$variables)

And add a media if the field is empty.