I'm attempting to add a custom image field to my menu from a custom module like so
function my_module_entity_base_field_info(EntityTypeInterface $entity_type) {
$fields = array();
if ($entity_type->id() === 'menu_link_content') {
$fields['menu_link_secondary_image'] = BaseFieldDefinition::create('image')
->setLabel(t('Menu link secondary image'))
->setDescription(t('Upload a secondary image related to this menu item'))
->setDisplayOptions('form',array('type' => 'image','weight' => 1))
->setDisplayConfigurable('form',TRUE)
->setSettings([
'file_directory' => 'public://',
'alt_field_required' => 'FALSE',
'file_extensions' => 'png jpg jpeg',
]);
return $fields;
}
}
Then in my update function I have
function my_module_update_8001(){
$entity_manager = \Drupal::entityManager();
$definitions = $entity_manager->getLastInstallFieldStorageDefinitions('menu_link_content');
if(!isset($definitions['menu_link_secondary_image']) || empty($definitions['menu_link_secondary_image'])){
$definition = $entity_manager->getFieldStorageDefinitions('menu_link_content')['menu_link_secondary_image'];
$entity_manager->onFieldStorageDefinitionCreate($definition);
}
}
But when I attempt to run update.php it hangs and I get errors like
Notice: Undefined index: menu_link_secondary_image__alt in Drupal\Core\Entity\Sql\SqlContentEntityStorage->loadFromSharedTables()
Strangely enough I added a menu link afterwards and my field was actually there. I couldn't tell if my update actually worked and just didn't act as if it did, or if it already worked before hand and the update was unnecessary. Did I do something wrong?
UPDATE: I see that although my field appeared I'm unable to save it due to this error
Column not found: 1054 Unknown column 'menu_link_secondary_image__target_id' i