# Page

A page is a doctrine entity that contains blocks and form builder.
You can run `php bin/console make:page` and generate a new page in an interactive way.

```php-inline title="src/Entity/Page/YourPage.php"
namespace App\Entity\Page;

use App\Core\Entity\Site\Page\Block;
use App\Core\Entity\Site\Page\Page;
use App\Core\Form\Site\Page\TextBlockType;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;

#[ORM\Entity]
class YourPage extends Page
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add(
            'myBlock',
            TextBlockType::class,
            [
                'label' => 'My block',
                'options' => [
                    // options given to the sub form
                ],
            ]
        );

        // ...
    }

    public function setMyBlock(Block $block)
    {
        return $this->setBlock($block);
    }

    public function getMyBlock(): Block
    {
        return $this->getBlock('myBlock');
    }

    // ...
}
```

Then edit `config/packages/app.yaml` and add your page:

```yaml
core:
    site:
        pages:
            App\Entity\Page\SimplePage:
                name: 'Simple page'
                templates:
                    - {name: "Default", file: "page/simple/default.html.twig"}
            App\Entity\Page\YourPage:
                name: 'Your page'
                templates:
                    - {name: "Template 1", file: "page/your_page/template1.html.twig"}
                    - {name: "Template 2", file: "page/your_page/template2.html.twig"}
```

## Blocks

### TextBlockType

`App\Core\Form\Site\Page\TextBlockType` will render a symfony `TextType`.

### TextareaBlockType

`App\Core\Form\Site\Page\TextareaBlockType` will render a symfony `TextareaType`.

### ChoiceBlockType

`App\Core\Form\Site\Page\ChoiceBlockType` will render a symfony `ChoiceType`.

### FileBlockType

`App\Core\Form\Site\Page\FileBlockType` will render a symfony `FileType` with a download link.

In the getter, you must specify the block:

```php-inline
use App\Core\Entity\Site\Page\FileBlock;

public function getMyBlock(): Block
{
    return $this->getBlock('myBlock', FileBlock::class);
}
```

### FilePickerBlockType

`App\Core\Form\Site\Page\FilePickerBlockType` will render a specific widget that use the file manager.

### EditorJsTextareaBlockType

`App\Core\Form\Site\Page\EditorJsTextareaBlockType` will render a [EditorJs widget](/utils/editors/editorjs/).

### GrapesJsBlockType

`App\Core\Form\Site\Page\GrapesJsBlockType` will render a [GrapesJS editor](/utils/editors/grapesjs/).

### TinymceTextareaBlockType

`App\Core\Form\Site\Page\TinymceTextareaBlockType` will render a [Tinymce editor](/utils/editors/tinymce/).

### ImageBlockType

`App\Core\Form\Site\Page\ImageBlockType` will render a symfony `FileType` with a preview of the image.

In the getter, you must specify the block:

```php-inline
use App\Core\Entity\Site\Page\FileBlock;

public function getMyBlock(): Block
{
    return $this->getBlock('myBlock', FileBlock::class);
}
```

### CollectionBlockType

`App\Core\Form\Site\Page\CollectionBlockType` will a render a symfony `CollectionType ` with availabity to add and remove elements.

```php-inline
use App\Core\Entity\Site\Page\CollectionBlock;

public function getMyBlock(): Block
{
    return $this->getBlock('myBlock', CollectionBlock::class);
}
```

## Event

When a page is being edited, the options can be set as follows:

```php-inline title="src/EventSubscriber/PageEventSubscriber.php"
namespace App\EventSubscriber;

use App\Core\Event\Page\PageEditEvent;
use App\Entity\Page\YourPage;

class PageEventSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return [
            PageEditEvent::FORM_INIT_EVENT => ['onFormInit'],
        ];
    }

    public function onFormInit(PageEditEvent $event)
    {
        if ($event->getPage() instanceof YourPage) {
            $event->addPageBuilderOptions([
                // options
            ]);
        }
    }
}
```
