Skip to content

Commit

Permalink
[NodeBundle] Deprecate SlugActionInterface and replace it by PageRend…
Browse files Browse the repository at this point in the history
…er setup
  • Loading branch information
acrobat committed Sep 29, 2021
1 parent 319bce7 commit 885db3c
Show file tree
Hide file tree
Showing 38 changed files with 636 additions and 98 deletions.
71 changes: 71 additions & 0 deletions UPGRADE-5.9.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,77 @@ GeneratorBundle
* The "kuma:generate:bundle" command and related classes is deprecated and will be removed in 6.0
* The "kuma:generate:entity" command and related classes is deprecated and will be removed in 6.0, use the "make:entity" command of the symfony/maker-bundle.

NodeBundle
----------

* `Kunstmaan\NodeBundle\Controller\SlugActionInterface` is deprecated and will be removed in 6.0. Implement the `Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface`
interface on your page entity and provide page render service id. That service should implement `Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface` and will allow you to customize the twig view and variables.

Before:

```php
<?php
use Kunstmaan\NodeBundle\Controller\SlugActionInterface
use Kunstmaan\NodeBundle\Entity\AbstractPage
class BlogOverviewPage extends AbstractPage implements SlugActionInterface
{
// ...
public function getControllerAction()
{
return 'App\Controller\BlogOverviewController::serviceAction';
}
}
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class BlogOverviewController extends AbstractController
{
public function serviceAction(){
// Custom render logic
}
}
```

After:

```php
<?php
use App\PageRenderer\BlogOverviewPageRenderer;
use Kunstmaan\NodeBundle\Entity\AbstractPage
use Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface
class BlogOverviewPage extends AbstractPage implements CustomViewDataProviderInterface
{
// ...
public function getViewDataProviderServiceId(): string
{
return BlogOverviewPageRenderer::class;
}
}
use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Symfony\Component\HttpFoundation\RedirectResponse;
class BlogOverviewPageViewDataProvider implements PageViewDataProviderInterface
{
public function provideViewData(RenderContext $renderContext): void
{
// Set a variable to be used in the twig template
$renderContext['custom_var'] = 'text';
// Or return a response and stop the twig render
$renderContext->setResponse(new RedirectResponse('/'));
}
}
```

NodeSearchBundle
------------

Expand Down
47 changes: 31 additions & 16 deletions docs/content-management/creating-a-pagetype.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,39 +137,54 @@ render it below the page parts.
{% endblock %}
```

Next we'll have to pass the employees to the Twig function. To do that we currently have a to implements the SlugActionInterface.
Next we'll have to pass the employees to the Twig function. To do that we have to implement the `Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface` interface.

So add the following in `EmployeesPage.php` :

```php
use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
use App\PageRenderer\EmployeesPageRenderer;
use Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface;

public function getControllerAction()
class EmployeesPage /* ... */ implements CustomViewDataProviderInterface
{
return 'MyProjectWebsiteBundle:EmployeesPage:service';
public function getViewDataProviderServiceId(): string
{
return EmployeesPageViewDataProviderr::class;
}
}
```

And create a new Controller `EmployeesPageController.php` to handle the logic :
And create a page viewdata provider `EmployeesPageViewDataProviderr.php` to handle the logic :

```php
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
...

public function serviceAction(Request $request)
namespace App\ViewDataProvider;

use Doctrine\ORM\EntityManagerInterface;use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
use Kunstmaan\NodeBundle\Helper\RenderContext;

class EmployeesPageRenderer implements PageViewDataProviderInterface
{
$em = $this->get('doctrine.orm.entity_manager');
$employees = $em->getRepository('MyProjectWebsiteBundle:Employee')->findAll();

$context['employees'] = $employees;
$request->attributes->set('_renderContext',$context);
/** @var EntityManagerInterface */
private $em;

public function __construct(EntityManagerInterface $em)
{
$this->em = $em;
}

public function provideViewData(RenderContext $renderContext): void
{
$employees = $this->em->getRepository('MyProjectWebsiteBundle:Employee')->findAll();

$renderContext['employees'] = $employees;
}
}
```

This class must be a service and tagged with `kunstmaan.node.page_view_data_provider`. If you use service autowiring/autoconfigure then this
is done automatically.
As you can see we just fetch all employees (using Doctrine), and pass them into the RenderContext (which is passed into Twig, so you'll get the list in your Twig template as the `employees` variable).


## Under the hood

- `src/YourVendor/YourWebsiteBundle/Resources/config/pagetemplates` contains the YML files defining page templates you can use.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
* @deprecated since KunstmaanArticleBundle 5.9 and will be removed in KunstmaanArticleBundle 6.0.
*/
class AbstractArticleOverviewPageController extends Controller
{
public function serviceAction(Request $request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
namespace Kunstmaan\ArticleBundle\Entity;

use Kunstmaan\ArticleBundle\PagePartAdmin\AbstractArticleOverviewPagePagePartAdminConfigurator;
use Kunstmaan\ArticleBundle\ViewDataProvider\ArticlePageViewDataProvider;
use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
use Kunstmaan\NodeBundle\Entity\AbstractPage;
use Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
use Kunstmaan\PagePartBundle\PagePartAdmin\AbstractPagePartAdminConfigurator;

/**
* The article overview page which shows its articles
*/
abstract class AbstractArticleOverviewPage extends AbstractPage implements HasPagePartsInterface, SlugActionInterface
abstract class AbstractArticleOverviewPage extends AbstractPage implements HasPagePartsInterface, SlugActionInterface, CustomViewDataProviderInterface
{
/**
* @return array
Expand Down Expand Up @@ -44,8 +46,16 @@ public function getDefaultView()
return '@KunstmaanArticle/AbstractArticleOverviewPage/view.html.twig';
}

/**
* @deprecated since KunstmaanArticleBundle 5.9 and will be removed in KunstmaanArticleBundle 6.0. Use the `Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface` and a custom page render service instead.
*/
public function getControllerAction()
{
return 'KunstmaanArticleBundle:AbstractArticleOverviewPage:service';
}

public function getViewDataProviderServiceId(): string
{
return ArticlePageViewDataProvider::class;
}
}
7 changes: 5 additions & 2 deletions src/Kunstmaan/ArticleBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
parameters:

services:
kunstmaan_articlebundle.twig.extension:
class: 'Kunstmaan\ArticleBundle\Twig\ArticleTwigExtension'
arguments: ['@doctrine.orm.entity_manager', '@router']
tags:
- { name: twig.extension }

Kunstmaan\ArticleBundle\ViewDataProvider\ArticlePageViewDataProvider:
arguments: [ '@doctrine.orm.entity_manager', '@request_stack' ]
tags:
- { name: 'kunstmaan.node.page_view_data_provider' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Kunstmaan\ArticleBundle\ViewDataProvider;

use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Pagerfanta\Adapter\ArrayAdapter;
use Pagerfanta\Pagerfanta;
use Symfony\Component\HttpFoundation\RequestStack;

final class ArticlePageViewDataProvider implements PageViewDataProviderInterface
{
/** @var EntityManagerInterface */
private $em;
/** @var RequestStack */
private $requestStack;

public function __construct(EntityManagerInterface $em, RequestStack $requestStack)
{
$this->em = $em;
$this->requestStack = $requestStack;
}

public function provideViewData(NodeTranslation $nodeTranslation, RenderContext $renderContext): void
{
$request = $this->requestStack->getMasterRequest();
if (null === $request) {
return;
}

$entity = $nodeTranslation->getRef($this->em);
$repository = $entity->getArticleRepository($this->em);
$pages = $repository->getArticles($request->getLocale());

$adapter = new ArrayAdapter($pages);
$pagerfanta = new Pagerfanta($adapter);
$pagerfanta->setMaxPerPage(5);

$pagenumber = $request->get('page');
if (!$pagenumber || $pagenumber < 1) {
$pagenumber = 1;
}

$pagerfanta->setCurrentPage($pagenumber);
$renderContext['pagerfanta'] = $pagerfanta;
}
}
1 change: 1 addition & 0 deletions src/Kunstmaan/ArticleBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"require": {
"php": "^7.2|^8.0",
"kunstmaan/adminlist-bundle": "^5.2",
"kunstmaan/node-bundle": "^5.9",
"kunstmaan/pagepart-bundle": "^5.2",
"pagerfanta/core": "^2.4",
"pagerfanta/twig": "^2.4"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
* @deprecated since KunstmaanFormBundle 5.9 and will be removed in KunstmaanFormBundle 6.0.
*/
class AbstractFormPageController extends Controller
{
public function serviceAction(Request $request)
Expand Down
11 changes: 10 additions & 1 deletion src/Kunstmaan/FormBundle/Entity/AbstractFormPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
use Doctrine\ORM\Mapping as ORM;
use Kunstmaan\FormBundle\Form\AbstractFormPageAdminType;
use Kunstmaan\FormBundle\Helper\FormPageInterface;
use Kunstmaan\FormBundle\ViewDataProvider\FormPageViewDataProvider;
use Kunstmaan\NodeBundle\Controller\SlugActionInterface;
use Kunstmaan\NodeBundle\Entity\AbstractPage;
use Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Kunstmaan\PagePartBundle\Helper\HasPagePartsInterface;
Expand All @@ -19,7 +21,7 @@
* Furthermore it's possible to configure an administrative email to be send when a form is submitted with in it an
* overview of all the submitted fields.
*/
abstract class AbstractFormPage extends AbstractPage implements FormPageInterface, HasPagePartsInterface, SlugActionInterface
abstract class AbstractFormPage extends AbstractPage implements FormPageInterface, HasPagePartsInterface, SlugActionInterface, CustomViewDataProviderInterface
{
/**
* The thank you text to be shown when the form was successfully submitted
Expand Down Expand Up @@ -186,10 +188,17 @@ public function getFormElementsContext()
}

/**
* @deprecated since KunstmaanFormBundle 5.9 and will be removed in KunstmaanFormBundle 6.0. Use the `Kunstmaan\NodeBundle\Entity\CustomViewDataProviderInterface` and a custom page render service instead.
*
* @return string
*/
public function getControllerAction()
{
return 'KunstmaanFormBundle:AbstractFormPage:service';
}

public function getViewDataProviderServiceId(): string
{
return FormPageViewDataProvider::class;
}
}
5 changes: 5 additions & 0 deletions src/Kunstmaan/FormBundle/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ services:
arguments: ['@kunstmaan_form.form_mailer']
tags:
- { name: 'kernel.event_listener', event: 'kunstmaan_form.add_submission', method: 'onSubmission' }

Kunstmaan\FormBundle\ViewDataProvider\FormPageViewDataProvider:
arguments: ['@request_stack', '@kunstmaan_form.form_handler', '@doctrine.orm.entity_manager']
tags:
- { name: 'kunstmaan.node.page_view_data_provider' }
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Kunstmaan\FormBundle\ViewDataProvider;

use Doctrine\ORM\EntityManagerInterface;
use Kunstmaan\FormBundle\Helper\FormHandlerInterface;
use Kunstmaan\NodeBundle\Entity\NodeTranslation;
use Kunstmaan\NodeBundle\Entity\PageViewDataProviderInterface;
use Kunstmaan\NodeBundle\Helper\RenderContext;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;

final class FormPageViewDataProvider implements PageViewDataProviderInterface
{
/** @var RequestStack */
private $requestStack;
/** @var FormHandlerInterface */
private $formHandler;
/** @var EntityManagerInterface */
private $em;

public function __construct(RequestStack $requestStack, FormHandlerInterface $formHandler, EntityManagerInterface $em)
{
$this->requestStack = $requestStack;
$this->formHandler = $formHandler;
$this->em = $em;
}

public function provideViewData(NodeTranslation $nodeTranslation, RenderContext $renderContext): void
{
$request = $this->requestStack->getMasterRequest();
if (null === $request) {
return;
}

$thanksParam = $request->get('thanks');
$entity = $nodeTranslation->getRef($this->em);
$renderContext['nodetranslation'] = $nodeTranslation;
$renderContext['slug'] = $request->attributes->get('url');
$renderContext['page'] = $entity;
$renderContext['resource'] = $entity;

if (!empty($thanksParam)) {
$renderContext['thanks'] = true;
}

$result = $this->formHandler->handleForm($entity, $request, $renderContext);
if ($result instanceof Response) {
$renderContext->setResponse($result);
}
}
}
2 changes: 1 addition & 1 deletion src/Kunstmaan/FormBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"symfony/swiftmailer-bundle": "^2.3|^3.0",
"kunstmaan/admin-bundle": "^5.7",
"kunstmaan/adminlist-bundle": "~5.2",
"kunstmaan/node-bundle": "~5.2",
"kunstmaan/node-bundle": "^5.9",
"kunstmaan/pagepart-bundle": "~5.2",
"stof/doctrine-extensions-bundle": "^1.3",
"symfony/css-selector": "^3.4|^4.4"
Expand Down
Loading

0 comments on commit 885db3c

Please sign in to comment.