-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:add html to textblock transformer
- Loading branch information
Showing
2 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
...nts/ImportExportBundle/src/lib/Item/ValueTransformer/Ibexa/HtmlToTextBlockTransformer.php
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa; | ||
|
||
use AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\AbstractItemValueTransformer; | ||
use Ibexa\Core\FieldType\TextBlock\Value; | ||
|
||
class HtmlToTextBlockTransformer extends AbstractItemValueTransformer | ||
{ | ||
|
||
public function transform($value, array $options = []) | ||
{ | ||
if (null === $value) { | ||
return new Value(); | ||
} | ||
|
||
// Define block-level tags | ||
$blockTags = ['p']; | ||
|
||
// Add line breaks after block-level closing tags | ||
foreach ($blockTags as $tag) { | ||
$value = preg_replace('/<\/' . $tag . '>/', "</$tag>\n", $value); | ||
} | ||
|
||
// Replace <br> tags with newlines | ||
$value = preg_replace('/<br\s*\/?>/i', "\n", $value); | ||
|
||
// Strip HTML tags | ||
$text = strip_tags($value); | ||
|
||
// Decode HTML entities | ||
$text = html_entity_decode($text, ENT_QUOTES | ENT_HTML5, 'UTF-8'); | ||
|
||
// Trim leading and trailing whitespace/newlines | ||
$text = trim($text); | ||
|
||
return new Value($text); | ||
} | ||
} |