murph-doc/docs/tree/page.md

167 lines
4 KiB
Markdown
Raw Normal View History

2021-05-31 23:23:15 +02:00
# Page
2022-03-24 16:41:12 +01:00
A page is a doctrine entity that contains blocks and form builder.
2022-03-24 16:50:15 +01:00
You can run `php bin/console make:page` and generate a new page in an interactive way.
2021-05-31 23:23:15 +02:00
2023-02-09 21:50:06 +01:00
```php-inline title="src/Entity/Page/YourPage.php"
2021-05-31 23:23:15 +02:00
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;
2023-02-09 21:50:06 +01:00
#[ORM\Entity]
2021-05-31 23:23:15 +02:00
class YourPage extends Page
{
2021-10-20 22:22:01 +02:00
public function buildForm(FormBuilderInterface $builder, array $options)
2021-05-31 23:23:15 +02:00
{
$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:
2023-02-09 21:50:06 +01:00
```yaml
2021-05-31 23:23:15 +02:00
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`.
2022-03-24 16:41:12 +01:00
### ChoiceBlockType
`App\Core\Form\Site\Page\ChoiceBlockType` will render a symfony `ChoiceType`.
2021-05-31 23:23:15 +02:00
### FileBlockType
`App\Core\Form\Site\Page\FileBlockType` will render a symfony `FileType` with a download link.
In the getter, you must specify the block:
2023-02-09 21:50:06 +01:00
```php-inline
2021-05-31 23:23:15 +02:00
use App\Core\Entity\Site\Page\FileBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', FileBlock::class);
}
```
2022-03-24 16:41:12 +01:00
### FilePickerBlockType
`App\Core\Form\Site\Page\FilePickerBlockType` will render a specific widget that use the file manager.
2022-04-14 22:44:53 +02:00
### EditorJsTextareaBlockType
2022-04-19 11:38:55 +02:00
`App\Core\Form\Site\Page\EditorJsTextareaBlockType` will render a [EditorJs widget](/utils/editors/editorjs/).
2022-04-14 22:44:53 +02:00
### GrapesJsBlockType
2022-04-19 11:38:55 +02:00
`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/).
2022-04-14 22:44:53 +02:00
2021-05-31 23:23:15 +02:00
### 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:
2023-02-09 21:50:06 +01:00
```php-inline
2021-05-31 23:23:15 +02:00
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.
2023-02-09 21:50:06 +01:00
```php-inline
2021-05-31 23:23:15 +02:00
use App\Core\Entity\Site\Page\CollectionBlock;
public function getMyBlock(): Block
{
return $this->getBlock('myBlock', CollectionBlock::class);
}
```
2022-02-21 16:24:26 +01:00
## Event
2022-02-21 16:31:17 +01:00
When a page is being edited, the options can be set as follows:
2022-02-21 16:24:26 +01:00
2023-02-09 21:50:06 +01:00
```php-inline title="src/EventSubscriber/PageEventSubscriber.php"
2022-12-13 18:12:41 +01:00
namespace App\EventSubscriber;
2022-02-21 16:24:26 +01:00
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
]);
}
}
}
```