Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] Add events to assign own data to view and moduleTemplate #595

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Classes/Controller/ActionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Controller;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Event\View\ModifyViewEvent;

abstract class ActionController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
{
protected function dispatchModifyViewEvent(): void
{
$modifyViewEvent = new ModifyViewEvent(
$this->request,
$this->settings,
$this->view
);

$this->eventDispatcher->dispatch($modifyViewEvent);
}
}
22 changes: 19 additions & 3 deletions Classes/Controller/Backend/Order/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Extcode\Cart\Domain\Repository\Order\ItemRepository;
use Extcode\Cart\Event\Order\NumberGeneratorEvent;
use Extcode\Cart\Event\Template\Components\ModifyButtonBarEvent;
use Extcode\Cart\Event\Template\Components\ModifyModuleTemplateEvent;
use Psr\Http\Message\ResponseInterface;
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Template\ModuleTemplate;
Expand Down Expand Up @@ -92,6 +93,8 @@ public function listAction(int $currentPage = 1): ResponseInterface
$pdfRendererInstalled = ExtensionManagementUtility::isLoaded('cart_pdf');
$this->moduleTemplate->assign('pdfRendererInstalled', $pdfRendererInstalled);

$this->dispatchModifyModuleTemplateEvent();

return $this->moduleTemplate->renderResponse('List');
}

Expand Down Expand Up @@ -129,6 +132,8 @@ public function showAction(Item $orderItem): ResponseInterface
$pdfRendererInstalled = ExtensionManagementUtility::isLoaded('cart_pdf');
$this->moduleTemplate->assign('pdfRendererInstalled', $pdfRendererInstalled);

$this->dispatchModifyModuleTemplateEvent();

return $this->moduleTemplate->renderResponse('Show');
}

Expand Down Expand Up @@ -241,16 +246,27 @@ protected function getLanguageService(): LanguageService

private function dispatchModifyButtonBarEvent(?Item $orderItem = null): array
{
$modifyButtonBarEvent = new ModifyButtonBarEvent(
$event = new ModifyButtonBarEvent(
$this->request,
$this->settings,
$this->searchArguments,
$orderItem
);

$this->eventDispatcher->dispatch($modifyButtonBarEvent);
$this->eventDispatcher->dispatch($event);

return $event->getButtons();
}

private function dispatchModifyModuleTemplateEvent(): void
{
$event = new ModifyModuleTemplateEvent(
$this->request,
$this->settings,
$this->moduleTemplate
);

return $modifyButtonBarEvent->getButtons();
$this->eventDispatcher->dispatch($event);
}

private function setDocHeader(array $buttons): void
Expand Down
2 changes: 1 addition & 1 deletion Classes/Controller/Cart/ActionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use Extcode\Cart\Utility\CartUtility;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;

abstract class ActionController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController
abstract class ActionController extends \Extcode\Cart\Controller\ActionController
{
protected SessionHandler $sessionHandler;

Expand Down
4 changes: 3 additions & 1 deletion Classes/Controller/Cart/CartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,8 +145,10 @@ public function showAction(
if (count($this->cart->getProducts()) === 0 && $currentStep > 1) {
return $this->redirect('show', null, null, ['step' => 1])->withStatus(303);
}
return $this->htmlResponse();

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

public function clearAction(): ResponseInterface
Expand Down
2 changes: 2 additions & 0 deletions Classes/Controller/Cart/CartPreviewController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public function showAction(): ResponseInterface

$this->view->assign('cart', $this->cart);

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}
}
2 changes: 2 additions & 0 deletions Classes/Controller/Cart/CountryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ public function updateAction(): ResponseInterface

$this->view->assign('cart', $this->cart);

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}
}
2 changes: 2 additions & 0 deletions Classes/Controller/Cart/CurrencyController.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public function updateAction(): ResponseInterface
$this->parseServicesAndAssignToView();
}

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}
}
5 changes: 5 additions & 0 deletions Classes/Controller/Cart/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ public function createAction(
$isPropagationStopped = $this->dispatchOrderCreateEvents($orderItem);

if ($isPropagationStopped) {
$this->dispatchModifyViewEvent();
// @todo Check the Response Type
return $this->htmlResponse();
}
Expand All @@ -127,13 +128,17 @@ public function createAction(
$this->redirectToUri($paymentSettings['options'][$paymentId]['redirects']['success']['url'], 0, 200);
}

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

public function showAction(Item $orderItem): ResponseInterface
{
$this->view->assign('orderItem', $orderItem);

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

Expand Down
3 changes: 3 additions & 0 deletions Classes/Controller/Cart/PaymentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function updateAction(int $paymentId): ResponseInterface
$this->view->assign('cart', $this->cart);

$this->parseServicesAndAssignToView();

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

Expand Down
3 changes: 3 additions & 0 deletions Classes/Controller/Cart/ShippingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function updateAction(int $shippingId): ResponseInterface
$this->view->assign('cart', $this->cart);

$this->parseServicesAndAssignToView();

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

Expand Down
7 changes: 6 additions & 1 deletion Classes/Controller/Order/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Controller\ActionController;
use Extcode\Cart\Domain\Model\Order\Item;
use Extcode\Cart\Domain\Repository\Order\ItemRepository;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -19,7 +21,6 @@
use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
use TYPO3\CMS\Extbase\Annotation\IgnoreValidation;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Pagination\QueryResultPaginator;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

Expand Down Expand Up @@ -65,6 +66,8 @@ public function listAction(int $currentPage = 1): ResponseInterface
]
);

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}

Expand Down Expand Up @@ -106,6 +109,8 @@ public function showAction(Item $orderItem): ResponseInterface
$pdfRendererInstalled = ExtensionManagementUtility::isLoaded('cart_pdf');
$this->view->assign('pdfRendererInstalled', $pdfRendererInstalled);

$this->dispatchModifyViewEvent();

return $this->htmlResponse();
}
}
39 changes: 39 additions & 0 deletions Classes/Event/Template/Components/ModifyModuleTemplateEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Event\Template\Components;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use TYPO3\CMS\Backend\Template\ModuleTemplate;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;

class ModifyModuleTemplateEvent
{
public function __construct(
private readonly RequestInterface $request,
private readonly array $settings,
private readonly ModuleTemplate $moduleTemplate,
) {}

public function getRequest(): RequestInterface
{
return $this->request;
}

public function getSettings(): array
{
return $this->settings;
}

public function getModuleTemplate(): ModuleTemplate
{
return $this->moduleTemplate;
}
}
39 changes: 39 additions & 0 deletions Classes/Event/View/ModifyViewEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace Extcode\Cart\Event\View;

/*
* This file is part of the package extcode/cart.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3Fluid\Fluid\View\ViewInterface;

class ModifyViewEvent
{
public function __construct(
private readonly RequestInterface $request,
private readonly array $settings,
private readonly ViewInterface $view,
) {}

public function getRequest(): RequestInterface
{
return $this->request;
}

public function getSettings(): array
{
return $this->settings;
}

public function getView(): ViewInterface
{
return $this->view;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.. include:: ../../Includes.rst.txt

==============================================================
Feature: #593 - Add event to assign own data to moduleTemplate
==============================================================

See `Issue 593 <https://github.com/extcode/cart/issues/593>`__

Description
===========

Adding your own data to module templates is not so easy. In order to assign
own data to the moduleTemplate, you need a way to assign your own data if you
do not want to develop your own backend module.

The `\Extcode\Cart\Event\Template\Components\ModifyModuleTemplateEvent` event
is dispached in the OrderController for the backend module.

An own EventListener can use the methods `getRequest()`, `getSettings()`,
`getModuleTemplate()` to retrieve all the necessary information to assign custom
data.
The request object can be used to assign data for specific actions.

Impact
======

No impact is expected.


.. index:: Backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.. include:: ../../Includes.rst.txt

====================================================
Feature: #593 - Add event to assign own data to view
====================================================

See `Issue 593 <https://github.com/extcode/cart/issues/593>`__

Description
===========

Adding your own data to plugin templates is not so easy. In order to assign
own data to the view, you need a lib in TypoScript and a f:cObject in the
template with some JavaScript on top.

This `\Extcode\Cart\Event\View\ModifyViewEvent` event is dispached in all
actions for which an output is generated via a template. Actions that are
forwarded to another action do not require the event.

An own EventListener can use the methods `getRequest()`, `getSettings()`,
`getView()` to retrieve all the necessary information to assign custom data.
The request object can be used to assign data for specific actions. For example,
saved addresses of a returning logged-in user can be displayed for selection
instead of the form for the billing address.

Impact
======

No impact is expected.


.. index:: Backend
Loading
Loading