2

enter image description here

I want that checkbox to be unchecked by default, because currently it's always checked.

And I want this result:

enter image description here

The structure of my content is as follows:

enter image description here

enter image description here

1
  • 1
    The question is unclear, because you don't need a form alter hook to set default values for entity fields. This is configuration if it belongs to a bundle since the entire bundle is configuration (see the answer from @leymannx) or if the entity doesn't have bundles this is set in the base field definition which you can alter in a hook if it is not your own entity.
    – 4uk4
    Aug 20, 2018 at 14:26

2 Answers 2

1

With a form alter this should work:

function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (isset($form['#entity_type']) && $form['#entity_type'] == 'MY_ENTITY_TYPE') {
    $form['status']['widget']['value']['#default_value'] = 0;
  }
}
4

Simply edit your content type's default publishing options under example.com/admin/structure/types/manage/MYCONTENTTYPE (Structure > Content types > MYCONTENTTYPE > Edit).

enter image description here

2
  • It's a good answer thank you, but in my case the content it has been created generating custom modules so my Content types tab is empty. Aug 20, 2018 at 11:03
  • @FranciscoJavierMartnezPrez – OK then please update your question to incorporate this key information and I see that I update my answer.
    – leymannx
    Aug 20, 2018 at 11:05

Your Answer

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

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