src/Controller/Website/DefaultController.php line 28

Open in your IDE?
  1. <?php
  2. namespace App\Controller\Website;
  3. use App\BlockManager\ContentBlockManager;
  4. use App\BlockManager\SidebarBlockManager;
  5. use App\Snippet\Button;
  6. use App\Utils\SchemaManager;
  7. use Spatie\SchemaOrg\Schema;
  8. use Sulu\Bundle\MediaBundle\Api\Media;
  9. use Sulu\Component\Content\Compat\StructureInterface;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\HttpFoundation\RequestStack;
  12. use Symfony\Component\HttpFoundation\Response;
  13. class DefaultController extends SuluExtendController
  14. {
  15.     private Request $request;
  16.     public function __construct(
  17.         private readonly ContentBlockManager    $contentBlockManager,
  18.         private readonly SidebarBlockManager                            $sidebarBlockManager,
  19.         public           RequestStack                                   $requestStack,
  20.     ) {
  21.         $this->request $this->requestStack->getCurrentRequest();
  22.     }
  23.     public function viewAction(
  24.         StructureInterface       $structure,
  25.         ContentBlockManager      $contentBlockManager,
  26.         SchemaManager            $schemaManager,
  27.         Button                   $button,
  28.                                  $attributes = [],
  29.                                  $preview false,
  30.                                  $partial false,
  31.     ): Response
  32.     {
  33.         $attributes $this->getAttributes([], $structure);
  34.         $attributes['breadcrumbs'] = $this->getBreadcrumbs('page'$attributes['uuid']);
  35.         $attributes['content_section_props'] = [
  36.             'is_container' => false,
  37.             'spacing' => 'top-0',
  38.         ];
  39.         $schema $schemaManager->getDefaultSchemas($attributes['breadcrumbs']);
  40.         $pageSchema Schema::webPage();
  41.         // ---------------------------------------
  42.         // Header
  43.         // ---------------------------------------
  44.         $attributes['template_content']['post'] = [
  45.             'header' => [
  46.                 'title' => $attributes['content']['title'],
  47.                 'perex' => $attributes['content']['description'],
  48.             ],
  49.         ];
  50.         // ---------------------------------------
  51.         // Content
  52.         // ---------------------------------------
  53.         $attributes['content_blocks'] = [];
  54.         $mappedContentBlocks $this->contentBlockManager->mapBlocksAttributes($attributes['content']['content_blocks']);
  55.         $pageSchemaImages = [];
  56.         foreach ($mappedContentBlocks as $block) {
  57.             if($block['type'] == 'image' || $block['type'] == 'imageWithText') {
  58.                 $imgUrl $this->request->getSchemeAndHttpHost() . $block['content']['img']?->getUrl();
  59.                 $pageSchemaImages[] = $imgUrl;
  60.             }
  61.             elseif($block['type'] == 'gallery' && isset($block['content']['images'])) {
  62.                 /** @var Media $image */
  63.                 foreach ($block['content']['images'] as $image) {
  64.                     $imgUrl $this->request->getSchemeAndHttpHost() . $image?->getUrl();
  65.                     $pageSchemaImages[] = $imgUrl;
  66.                 }
  67.             }
  68.             $attributes['content_blocks'][] = $block;
  69.         }
  70.         
  71.         $pageSchema->image($pageSchemaImages);
  72.         $schema[] = $pageSchema;
  73.         // ---------------------------------------
  74.         // Sidebar
  75.         // ---------------------------------------
  76.         if ($attributes['content']['sidebar'] && array_key_exists('blocks'$attributes['content']['sidebar'])) {
  77.             $attributes['props']['aside'] = true;
  78.             $attributes['sidebar']['blocks'] = $this->sidebarBlockManager->mapBlocksAttributes($attributes['content']['sidebar']['blocks']);
  79.         }
  80.         // ---------------------------------------
  81.         // Blocks under post
  82.         // ---------------------------------------
  83.         if(isset($attributes['content']['blocks'])) {
  84.             $blockList $this->resolveBlocks($attributes['content']['blocks'], $schema);
  85.             $attributes['content']['blocks'] = $blockList->getBlocks();
  86.             $schema $blockList->getSchema();
  87.         }
  88.         $attributes['_schema'] = implode(''$schema);
  89.         
  90.         return $this->renderAll('pages/default.html.twig'$attributes$preview$partial);
  91.     }
  92. }