Closed speedupmate closed 2 years ago
As I did a deeper investigation how this works then currently although serialiser is implemented there's no way to pass a custom serialiser closure to each element that gets rendered .
Would be cool if we could attach a serialiser method inside our own block or inject it within our templates set, get
to child or parent or current block and that would allow access to RichText html fragments and make it possible to manipulate those per content type and content field . Usecase would be here like : H2 title in prismic > serialise > H2 class="tailwindclass" as output
Hi @speedupmate thanks for bringing this up, the SDK is not that pluggable as Magento is...
By default, Prismic uses a custom labels concept... This means you need to think ahead and it will become the responsibility of content editors to select the correct "label" which will reflect as a "class" in all Prismic implementations.. [Here](https://github.com/elgentos/prismicio-php- kit/blob/4bcf06cf1194625fae0391ed0e7c97573009206b/src/Prismic/Fragment/AttributesTrait.php#L14) is an example how default Prismic processes this...
Back to a custom serializer and the implementation in Prismic. This is possible, but needs some extra work!
tbd (will update this message)
I finally see the error... duh
I've updated https://github.com/elgentos/prismicio-php-kit/blob/master/src/Prismic/Dom/RichText.php#L46
This now only accepts a closure... I will add a fn() => ...
around it and it will be fixed!
Many thanks for bringing this up, this is a bug I created :)
To use a custom serializer in Magento you can create a plugin on \Elgentos\PrismicIO\ViewModel\HtmlSerializer::serialize
A afterSerialize
would get string $result = null, \Prismic\Fragment\BlockInterface $object, string $content
function afterSerialize (null $result, \Prismic\Fragment\BlockInterface $object, string $content)
{
if ($object->type === 'heading1') {
return '<h1 class="awesome">' . $content . '</h1>';
}
return $result;
}
If a custom serializer does not return null
that result will be returned, else the default serializer of Prismic will render the content
Again, sorry I broke it!
Actually you could just shortcut this to block scope and add getHtmlSerializer() type of reference on method and that would allow to just override any custom-type directly in template level where you work with the custom-type and donät have to detect it extra as you would in plugin :
you could
public function fetchDocumentView(): string
{
$html = PrismicRichText::asHtml(
$this->getContext(),
$this->getLinkResolver(),
$this->getHtmlSerializer() ?? fn($object, string $content) => $this->htmlSerializer->serialize($object, $content)
);
return $this->replaceRelativeUrl($html);
}
and then just in some template or block without the complexity of a plugin
$block->setHtmlSerializer(yourfunction)
Thanks
Seems like it fails to display rich-text fields defined in prismic content types due prismic-sdk now requiring a closure instead of array