I am trying to make a field not required in drupal 8 through hook form alter in the following way, which does not seem to work:

function mymodule_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if ($form_id == "menu_link_content_menu_link_content_form") {
    $form['link']['#required'] = FALSE;

To get this working in the form alter hook you need to set #required to FALSE in the widget:

$form['link']['widget']['0']['uri']['#required'] = FALSE;

The red asterix mark for a required field is no longer there, but the form does not validate. To get the form validated you would need to do this in the field definition. Implement a hook hook_entity_base_field_info_alter and set required to FALSE:

function mymodule_entity_base_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type) {
  if ($entity_type->id() == 'menu_link_content') {

But this doesn't help either, because there is code in the menu link content class that depends on $this->link, so even if you manage to pass the validation you get an error later, that the link is missing.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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