New feature: individually hydrate/dehydrate extensions #291
Replies: 4 comments 12 replies
-
Is this for a custom image block? |
Beta Was this translation helpful? Give feedback.
-
Yeah, but then we would be talking only about the Curator case. I went through all this "trouble" because I already have a bunch of other custom extensions unrelated to Curator that will need that kind of handling. Also, this is totally optional... You add Hydrants only if you need them. And I think it keeps the code very clean, organized and it's very simples to customize any new extensions I add. But anyway... let me know if you'd like to take this further. Otherwise, I'm happy enough for achieving this and keeping this in my isolated branch. 😃 |
Beta Was this translation helpful? Give feedback.
-
I could just not be fulling understanding too. Is this for blocks only? |
Beta Was this translation helpful? Give feedback.
-
I wonder if this would be better on the TiptapBlock class, that way each block can handle it's own hydration / dehydration instead of the field having to do it. |
Beta Was this translation helpful? Give feedback.
-
Hi, folks! I've just implemented a feature that makes a lot of sense to me and I'd like to know if anyone else has interest in this so we can maybe merge this to the main project.
What I needed
id
for the images. This is because if I need to change the domain that the images are served from, I don't need to change this in the database.dehydrate
the extension to only save the type of the block (image
), and the image'sid
.hydrate
the extension to be handle in the frontend.What I did
I've added the possibility to hydrate/dehydrate extensions individually.
Why I did this?
We have field hydration/dehydration in Filament itself, but, in the case of the editor, this happens only for the entire content of the field.
At the moment, if we need to mutate the data for a specific extension before saving it to the database and when we fill the form on the frontend, the only whey to do this is via the
afterStateHydrated
,formatStateUsing
and thedehydrateStateUsing
methods, which needs to be called on every instance on the editor that needs this.Also, when dealing with this methods, we need to loop over the contents and conditionally handle each block depending on its type, which makes the code clunky and a bit more difficult to be organized. Also, when dealing with this, every developer will end up with something very similar that could be provided out of the box, saving a lot of time and effort for everyone.
What I wanted
I wanted a way to isolate the logic for hydrating/dehydrating each extension, keeping the code organized.
What I came up with?
I created the concept of a
Hydrant
, not sure if we should call itHydrator
, but you get the idea... This is a class that has 4 methods that are explained below:Then, we can register any custom
Hydrant
in the config file, where the key in the array is the type of the block:From here, we change the
setUp
method onTiptapEditor.php
to call the methods on theHydrant
at the points need. For instance, before calling therenderPreviewsMethod
, we callhandleExtensionsHydration
.Let me know what you think about this, and specially if there's a better way to achieve this that I don't know about.
Beta Was this translation helpful? Give feedback.
All reactions