I've got a D8 media type that holds information obtained from an external source. The information comes in name value pairs that I wish to save as individual fields, mostly so I can index them easily with solr. On some files I get new property names that have to be persisted as fields, so I have a presave hook that creates and saves a FieldStorageConfig, and a StorageConfig for my each new field in my media bundle. It all works swimmingly.

THe problem is when i try to save a value to that new field in the presave function an error is thrown indicating that the field doesn't exist. (It does exist, but Drupal doesn't know about it yet). When I resave the media with the same input file everything goes as planned.

Is there a way to force Drupal to refresh the media's field list?


This is as simple as reloading the entity. The entity is loaded with the current fields, if new fields are added to the content type, the entity won't update on its own.

In my presave hook I check to see if new fields have been added at that time. If so I wait til the media update hook is fired to reload.

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.