Problem/Motivation
The Image Style config entity would need to implement ThirdPartySettingsInterface
if we want to allow contrib modules to store module specific metadata to the image styles. See use case in the OP.
Proposed resolution
Implement ThirdPartySettingsInterface
and ThirdPartySettingsTrait
in ImageStyle.
Remaining tasks
Agree approach.
Review patch.
User interface changes
None
API changes
None
Original report by @mondrake
Updated: Comment #0
Problem/Motivation
In Drupal 7, I can add an extra column to the {image_styles} table to store some metadata specific for my module, working with hook_schema_alter
and db_add_field
.
When the image style is loaded, the content of that column is added to the image style array and I can get/set the information I need. and this is stored back in the db.
Now, this seems not possible in D8. I can implement a mymodule_entity_presave
hook and set a variable within the entity, but it does not get flushed in the config file. If I understand correctly, this is because ConfigStorageController::save
requires the entity to provide the properties to be saved via ConfigEntityBase::getExportProperties
, but only those declared public in the ImageStyle class (+effects) are actually saved to config (based on ReflectionClass).
So if I want to add a property to an existing class I would have to extend the ImageStyle class. But I would like to just 'hook' in the core one, not build an entirely new one.
Is there any suggestion? or am I missing some pieces?
Proposed resolution
Remaining tasks
User interface changes
API changes
Related Issues
Comment | File | Size | Author |
---|---|---|---|
#22 | interdiff_20-22.txt | 567 bytes | mondrake |
#22 | 2084985-22.patch | 6.55 KB | mondrake |
#20 | 2084985-20.patch | 6 KB | mondrake |
Comments
Comment #1
mondraketagging
Comment #2
mondraketagging
Comment #3
mondrakeComment #4
tim.plunkettYes, the base implementation of getExportProperties() only picks up public properties, and all of the overridden implementations call that and then loop through a specific set of protected properties.
FilterFormat and ImageStyle are unique in that they call actual methods in there, to get their plugin configuration.
Assuming this is a behavior we want to continue supporting, this is a bug.
Comment #5
claudiu.cristeaI can see 2 solutions:
getExportProperties()
and pass the entity (so that hook implementations will know the source) and properties list to be alteredConfigEntityBase
(as 'status' is now), acting like storage and initialized to an empty array. Modules can read/write/delete data from that storage. Being public will always be saved and restored.Comment #6
swentel CreditAttribution: swentel commentedSo how do we make this work with the config schema's then ?
Comment #7
gddThis feels like a won't fix to me, and personally I've always felt that using hook_schema_alter() was tantamount to hacking core. We have made the explicit decision not to implement alters anywhere in the configuration chain. I'm not a fan of creating a dumping ground for random configuration either. There is a solution now, it is solid architecturally. This seems like a won't fix.
Comment #8
claudiu.cristeaI only partially agree with @heyrocker, on hook_schema_alter(), but I disagree on "won't fix". Configurables should allow contrib modules to add new properties without extending the class. If 2 modules will need to add new properties to the same configurable, extending the class is not a solution.
As #1497374: Switch from Field-based storage to Entity-based storage is now in, we can take into consideration next approach:
field_*
.Comment #9
mondrakeFrom reading #2005434: Let 3rd party modules store extra configuration in EntityDisplay, it looks to me this issue is quite related to that - here is a more general need rather than being widget/formatter specific.
In #2005434: Let 3rd party modules store extra configuration in EntityDisplay, the part of the proposed solution
would be pretty much what I fancy for ConfigEntity objects here.
I'm sorry I can't contribute more...
Comment #10
tim.plunkettAfter #2256679: Use config schema to determine which config entity properties to export regardless of whether they are public or protected went in, the schema wins out here.
I've already had needs for extending the schema from contrib in page_manager, that could be the way forward.
Comment #11
mondrakeLooks like #2224761-95: Add a generic way to add third party configuration on configuration entities and implement for field configuration is going to introduce a generic capability for modules to store information in ConfigEntities via the ThirdPartySettingsInterface?
Comment #12
mondrakeComment #14
mondrakeFixed test to check against 'third_party_settings' array elements too.
Comment #15
mondrakeComment #16
slashrsm CreditAttribution: slashrsm commentedLooks good. One comment.
Let's make ImageStyleInterface extend ThirdPartySettingsIntefrace instead extending it directly.
Comment #17
mondrakeOK
Comment #18
slashrsm CreditAttribution: slashrsm commentedRTBC if green.
Comment #19
BerdirDo we need some more tests that this actually works? We can see that it shows up in the schema, so that's good. We're almost only just re-using standard functionality now, so I'm not sure.
Maybe just add or extend a kernel test that creates an image style with the API, adds a third party setting, saves and loads it again?
In #2324121: NodeType's settings array was meant to be able to store information from mutliple modules, we have an actual use case for this that I was able to convert.
Comment #20
mondrakeImplemented a presave hook in image_module_test that adds an arbitrary third party setting to image style entities, and added in the image style admin test a check to see that that gets actually done.
Comment #22
mondrakeUhm... let's see
Comment #23
mondrakeOK, green. The problem in #20 was that 'image_module_test' was marked as a Core module, not a Testing one, so it got installed in the ConfigImportAllTest, and likely was adding third party settings to the shipped image styles that ended up in the update list. Changed the module's package from 'Core' to 'Testing' fixed that (I see other test modules have the same setting, so this maybe this was a leftover of the removal of the 'hidden' attribute in the info.yml file).
Comment #24
slashrsm CreditAttribution: slashrsm commentedLooks good to me.
Comment #25
alexpottCommitted 73b0592 and pushed to 8.0.x. Thanks!