Skip to main content
added 228 characters in body
Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You wouldTo get this working in the form alter hook you need to do thisset #required to FALSE in the widget:

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

The red asterix mark for a required field definitionis no longer there, otherwisebut the entity form won'tdoes 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') {
    $fields['link']->setRequired(FALSE);
  }
}

But this doesn't help for the menu linkeither, 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.

You would need to do this in the field definition, otherwise the entity form won't validate. 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') {
    $fields['link']->setRequired(FALSE);
  }
}

But this doesn't help for the menu link, 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.

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') {
    $fields['link']->setRequired(FALSE);
  }
}

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.

Source Link
4uk4
  • 101.7k
  • 7
  • 173
  • 217

You would need to do this in the field definition, otherwise the entity form won't validate. 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') {
    $fields['link']->setRequired(FALSE);
  }
}

But this doesn't help for the menu link, 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.