1

I'm currently porting my contributed module to Drupal 8, and sometimes I realize I've chosen the wrong field type. I also know that I will need to add additional fields later on, and it would be nice to avoid having to uninstall and reinstall the module every time I do this.

I looked at Add field to existing content entity, but it did not seem to be the actual question.

My question is basically: what's the equivalent of similar D7 update.php code with D8 and entities?

<?php
function MYMODULE_update_7101() {
  db_add_field('mymodule_table', 'some_new_field', $field_schema);
}
?>
2

Adding fields just works. Define the new field, run update.php, it will compare the new and existing definitions and figure out that there's a new field to create.

Renaming/deleting fields works too, but only when you have no data. When you have data, then you will still need to write an update hook, or a migration. We're missing documentation on how that will look exactly, but basically, you use the same schema functions as in 7.x and then update the last installed field definitions.

| 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.