Skip to content

Commit

Permalink
feat:add html to textblock transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
saoulcx committed Jul 29, 2024
1 parent 58c4446 commit 3840469
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ services:
$htmlToRichtextTransformer: '@AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\HtmlToRichtextTransformer'
tags:
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.text_to_richtext}

AlmaviaCX\Bundle\IbexaImportExport\Item\ValueTransformer\Ibexa\HtmlToTextBlockTransformer:
tags:
- { name: almaviacx.import_export.item.value_transformer, alias: almaviacx.import_export.item.value_transformer.html_to_textblock}
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);
}
}

0 comments on commit 3840469

Please sign in to comment.