6

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.

1 Answer 1

5
+200

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']);
  }
}
2
  • 4
    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
    – Hudri
    Nov 22, 2021 at 16:30
  • 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😤😤😤 Nov 22, 2021 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.