Last active
February 6, 2024 11:52
-
-
Save janit/36ebf9dd1766676a7fc64402b7542596 to your computer and use it in GitHub Desktop.
Convert HTML to eZ Platform Rich Text DocBook XML format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// This is the namespace you want to use | |
use EzSystems\EzPlatformRichText\eZ\FieldType\RichText\Type as RichTextFieldType; | |
// this would be a method in your class (you'll need to inject RichTextFieldType) | |
// It has some extra wrangling of input not required, but makes it moarrr robust | |
private function prepareRichText($inputText){ | |
if($inputText === ''){ | |
$inputText = '<p> </p>'; | |
} | |
$tidyConfig = array( | |
'show-body-only' => true, | |
'output-xhtml' => true, | |
'wrap' => -1); | |
$inputText = tidy_parse_string($inputText, $tidyConfig); | |
$inputText = str_replace(array("\r\n", "\r", "\n"), "", $inputText); | |
$content = ['xml' => '<?xml version="1.0" encoding="UTF-8"?><section xmlns="http://ez.no/namespaces/ezpublish5/xhtml5/edit">'. $inputText . '</section>']; | |
return $this->richTextFieldType->fromHash($content); | |
} | |
// HTML content. To see full range of options (embeds, links, etc.) it's best to see the RichTextFieldType xhtml5 input test | |
// fixtures here: https://github.com/ezsystems/ezplatform-richtext/tree/master/tests/lib/eZ/RichText/Converter/Xslt/_fixtures/xhtml5/edit | |
$htmlContent = '<p>Foo <b>Bar</b>.</p><blockquote>Bar Foo</blockquote><p>Baz Bay</p>'; | |
// This is how you would use the helpere to populare a field in a content create struct | |
$contentCreateStruct->setField('body', $this->prepareRichText($htmlContent)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice to hear you found it helpful :)