Skip to content

Adds rich_text twig filter #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 26, 2025
Merged

Conversation

silasjoisten
Copy link
Collaborator

@silasjoisten silasjoisten commented Mar 26, 2025

This PR requires the storyblok/php-tiptap-extension and adds the twig filter rich_text to the bundle.

There is a built in Editor for users who dont need to customize anything. It works out of the box with the BlockRegistry and renders registered blocks automatically. In case you want to override the default editor you can implement the Storyblok\Bundle\Tiptap\EditorBuilderInterface and implement it by yourself here is an example:

<?php

declare(strict_types=1);

namespace App\Bridge\Storyblok\Tiptap;

use App\Bridge\Storyblok\Tiptap\Node\Heading;
use App\Exception\InvalidContentTypeConfigurationException;
use Storyblok\Bundle\Block\Renderer\RendererInterface;
use Storyblok\Bundle\Tiptap\EditorBuilderInterface;
use Storyblok\Tiptap\Extension\Storyblok;
use Symfony\Component\DependencyInjection\Attribute\AsAlias;
use Tiptap\Editor;

#[AsAlias(EditorBuilderInterface::class)]
final readonly class CustomEditorBuilder implements EditorBuilderInterface
{
    public function __construct(
        private RendererInterface $renderer,
    ) {
    }

    public function getEditor(array $values): Editor
    {
        $editor = new Editor([
            'extensions' => [
                new Storyblok([
                    'blokOptions' => [
                        'renderer' => function (array $value): string {
                            try {
                                return $this->renderer->render($value);
                            } catch (\InvalidArgumentException|\ValueError $e) {
                                throw new InvalidContentTypeConfigurationException($e->getMessage(), $e->getCode(), $e);
                            }
                        },
                    ]
                ]),
                'heading' => new Heading(),
            ],
        ]);

        $editor->setContent($values);

        return $editor;
    }
}

in this example we want to override the tiptap extension Heading and we want to throw a dedicated exception in the render function.

Usage:

<div>
    {{ story.content|rich_text }}
</div>

@silasjoisten silasjoisten self-assigned this Mar 26, 2025
@codecov-commenter
Copy link

codecov-commenter commented Mar 26, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.19%. Comparing base (e4a88f4) to head (29f38cb).

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #19      +/-   ##
============================================
+ Coverage     91.69%   92.19%   +0.49%     
- Complexity       70       75       +5     
============================================
  Files            12       14       +2     
  Lines           313      333      +20     
============================================
+ Hits            287      307      +20     
  Misses           26       26              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@silasjoisten silasjoisten merged commit 7fc5574 into master Mar 26, 2025
3 checks passed
@silasjoisten silasjoisten deleted the feature/adds-twig-extension branch March 26, 2025 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants