How can I remove default image from image fields?

ru flag

I need to create an image field with a default value, which must also be nullable on single content entities. Contrary to text fields or entity reference fields, I was unable to remove this default value on single content entities.

E.g. I can create a text field with default value "Lorem ipsum". On node creation, the field is prefilled with that text, but I can also delete that text and the field will stay empty.

But on image fields, there is no button/option to remove the default value at all. I can remove an override image, but not the default one. Even after uploading a non-default image, storing, and then removing that image again, the default value kicks in again.

How can I make the image field "nullable"?

Note that I can not use media entity references, because I must support SVG and those images also must not leak into the reuseable global media pool.

cn flag

Actually default image does not cause any data save but it affects image formatter so if it finds no values for image field, then it displays the default image which is set in field settings.

you can check in DB or simply create a content with default image A.png and then go to field settings and change the default image to B.png, and when you display your node you will see that B.png will shown in content.

So if you don't want to see the default image in some cases you can duplicate default image formatter and do whatever you like.

and if you really want to save a data for image field with default value, you can do it by altering node form like below:

function my_module_form_node_article_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
      array_unshift($form['actions'][$action]['#submit'], 'my_module_test_form_submit');

function my_module_test_form_submit(&$form, \Drupal\Core\Form\FormStateInterface $form_state) {
  $image = $form_state->getValue('field_image');
  if (empty($image[0]['fids'])) {
    $form_state->setValue('field_image', $form['field_image']['widget'][0]['#default_image']['fid']);
ru flag
Thanks for the hint with the field formatter, this explains a lof of issues I've had. The image field is the only field in core that doesn't store default values in the DB, but computes them in the field formatter. This is why the image isn't nullable and the storage API doesn't return correct default values. I've filed a [bug report on d.o]( about this
Alireza Tabatabaeian avatar
cn flag
I can imagine how exhaustive it is to realize you actually has not saved any values on a field for long time while you thought you did

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.