PHP code example of openstudio / page-builder-bundle

1. Go to this page and download the library: Download openstudio/page-builder-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

openstudio / page-builder-bundle example snippets


// config/bundles.php
return [
    // ...
    OpenStudio\PageBuilderBundle\OpenStudioPageBuilderBundle::class => ['all' => true],
];

// src/Form/PageType.php
$builder
    ->add('projectData', HiddenType::class)
    ->add('html', HiddenType::class)
    ->add('css', HiddenType::class);

use OpenStudio\PageBuilderBundle\Contract\ImageUploadPortInterface;
use OpenStudio\PageBuilderBundle\Contract\ImageLibraryPortInterface;
use OpenStudio\PageBuilderBundle\Dto\ImageUploadResponse;
use OpenStudio\PageBuilderBundle\Dto\ImageRecord;

final readonly class ImageUploadAdapter implements ImageUploadPortInterface
{
    public function upload(UploadedFile $file, ?string $context = null, ?string $uploadedBy = null): ImageUploadResponse
    {
        // store $file, then return where it lives
        return new ImageUploadResponse(id: '...', url: '...', originalFileName: $file->getClientOriginalName());
    }

    public function delete(string $imageId): void { /* ... */ }
}

#[Route('/admin/page-builder/render-template', methods: ['POST'])]
final class PageBuilderRenderController extends AbstractController
{
    private const ALLOWED = [
        'cta_button' => 'page_builder/blocks/cta_button.html.twig',
    ];

    public function __invoke(Request $request): JsonResponse
    {
        $payload = $request->toArray();
        $template = (string) ($payload['templateName'] ?? '');

        if (!isset(self::ALLOWED[$template])) {
            return $this->json(['error' => 'Unknown template'], 400);
        }

        return $this->json([
            'content' => $this->renderView(self::ALLOWED[$template], (array) ($payload['parameters'] ?? [])),
        ]);
    }
}