Skip to content

Commit

Permalink
Add event to manipulate sub-headline in edit mask
Browse files Browse the repository at this point in the history
  • Loading branch information
zonky2 committed Jan 25, 2023
1 parent 5992a89 commit 2a25827
Show file tree
Hide file tree
Showing 4 changed files with 201 additions and 18 deletions.
42 changes: 24 additions & 18 deletions src/Contao/View/Contao2BackendView/EditMask.php
Expand Up @@ -3,7 +3,7 @@
/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2022 Contao Community Alliance.
* (c) 2013-2023 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
Expand All @@ -18,7 +18,7 @@
* @author Stefan Heimes <stefan_heimes@hotmail.com>
* @author Sven Baumann <baumann.sv@gmail.com>
* @author Richard Henkenjohann <richardhenkenjohann@googlemail.com>
* @copyright 2013-2022 Contao Community Alliance.
* @copyright 2013-2023 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/
Expand All @@ -33,6 +33,7 @@
use ContaoCommunityAlliance\Contao\Bindings\Events\Controller\RedirectEvent;
use ContaoCommunityAlliance\Contao\Bindings\Events\System\GetReferrerEvent;
use ContaoCommunityAlliance\Contao\Bindings\Events\System\LogEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetEditMaskSubHeadlineEvent;
use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetEditModeButtonsEvent;
use ContaoCommunityAlliance\DcGeneral\Data\DefaultEditInformation;
use ContaoCommunityAlliance\DcGeneral\Data\ModelId;
Expand Down Expand Up @@ -649,27 +650,32 @@ protected function handleSubmit(ModelInterface $model)
* Determine the headline to use.
*
* @return string.
*
* @deprecated This is deprecated since 2.3 and will be removed in 3.0.
*/
protected function getHeadline()
private function getHeadline(): string
{
$definitionName = $this->getDataDefinition()->getName();
$translator = $this->getEnvironment()->getTranslator();
// @codingStandardsIgnoreStart
@\trigger_error(__CLASS__ . '::' . __METHOD__ . ' is deprecated - use getSubHeadline()!', E_USER_DEPRECATED);
// @codingStandardsIgnoreEnd

if ($this->model->getId()) {
$headline = $translator->translate('editRecord', $definitionName, [$this->model->getId()]);
$this->getSubHeadline();
}

if ('editRecord' !== $headline) {
return $headline;
}
return $translator->translate('MSC.editRecord', null, [$this->model->getId()]);
}
/**
* Determine the headline to use.
*
* @return string.
*/
protected function getSubHeadline(): string
{
$environment = $this->getEnvironment();

$headline = $translator->translate('newRecord', $definitionName, [$this->model->getId()]);
if ('newRecord' !== $headline) {
return $headline;
}
$event = new GetEditMaskSubHeadlineEvent($this->environment, $this->model);

$environment->getEventDispatcher()->dispatch($event, $event::NAME);

return $translator->translate('MSC.editRecord', null, ['']);
return $event->getHeadline();
}

/**
Expand Down Expand Up @@ -862,7 +868,7 @@ public function execute()
'versions' => $dataProviderInformation->isVersioningEnabled() ? $dataProvider->getVersions(
$this->model->getId()
) : null,
'subHeadline' => $this->getHeadline(),
'subHeadline' => $this->getSubHeadline(),
'table' => $definition->getName(),
'enctype' => 'multipart/form-data',
'error' => $editInformation->getFlatModelErrors($this->model),
Expand Down
@@ -0,0 +1,101 @@
<?php

/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2023 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2023 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event;

use ContaoCommunityAlliance\DcGeneral\Data\ModelInterface;
use ContaoCommunityAlliance\DcGeneral\EnvironmentInterface;
use ContaoCommunityAlliance\DcGeneral\Event\AbstractEnvironmentAwareEvent;

/**
* Class GetEditModeButtonsEvent.
*
* This event is triggered when the sub-headline is generated.
*/
class GetEditMaskSubHeadlineEvent extends AbstractEnvironmentAwareEvent
{
/**
* The name of the event.
*/
public const NAME = 'dc-general.view.contao2backend.get-edit-mask-subheadline';

/**
* The model attached to the event.
*
* @var ModelInterface
*/
private $model;

/**
* The sub-headline.
*
* @var string|null
*/
private $subHeadline;

/**
* Create a new instance.
*
* @param EnvironmentInterface $environment The environment in use.
* @param ModelInterface $model The model attached to the event.
*/
public function __construct(EnvironmentInterface $environment, ModelInterface $model)
{
parent::__construct($environment);
$this->model = $model;
}

/**
* Retrieve the attached model.
*
* @return ModelInterface
*/
public function getModel(): ModelInterface
{
return $this->model;
}

/**
* Set the sub-headline.
*
* @param string $subHeadline The sub-headline to be returned.
*
* @return $this
*/
public function setHeadline(string $subHeadline): self
{
$this->subHeadline = $subHeadline;

return $this;
}

/**
* Get the sub-headline.
*
* @return string|null
*/
public function getHeadline(): ?string
{
if (false === isset($this->subHeadline)) {
return null;
}

return $this->subHeadline;
}
}
@@ -0,0 +1,70 @@
<?php

/**
* This file is part of contao-community-alliance/dc-general.
*
* (c) 2013-2023 Contao Community Alliance.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* This project is provided in good faith and hope to be usable by anyone.
*
* @package contao-community-alliance/dc-general
* @author Ingolf Steinhardt <info@e-spin.de>
* @copyright 2013-2023 Contao Community Alliance.
* @license https://github.com/contao-community-alliance/dc-general/blob/master/LICENSE LGPL-3.0-or-later
* @filesource
*/

namespace ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\EventListener;

use ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\Event\GetEditMaskSubHeadlineEvent;

/**
* This class handles events to handle sub-headline at input mask.
*/
class CreateSubHeadlineListener
{
/**
* Handle the edit mask sub-headline event.
*
* @param GetEditMaskSubHeadlineEvent $event
*
* @return void
*/
public function __invoke(GetEditMaskSubHeadlineEvent $event): void
{
if (null !== $event->getHeadline()) {
return;
}

$status = $event->getModel()->getId() ? 'editRecord' : 'newRecord';
$this->createSubHeadline($status, $event);
}

/**
* Create the sub-headline at input mask.
*
* @param string $status The status.
* @param GetEditMaskSubHeadlineEvent $event The event.
*
* @return void
*/
private function createSubHeadline(string $status, GetEditMaskSubHeadlineEvent $event): void
{
$environment = $event->getEnvironment();
$definitionName = $environment->getDataDefinition()->getName();
$translator = $environment->getTranslator();

$headline = $translator->translate($status, $definitionName, [$event->getModel()->getId()]);

if ($status !== $headline) {
$subHeadline = $headline;
} else {
$subHeadline = $translator->translate('MSC.' . $status, null, [$event->getModel()->getId()]);
}

$event->setHeadline($subHeadline);
}
}
6 changes: 6 additions & 0 deletions src/Resources/config/contao/event_common_subscribers.yml
Expand Up @@ -10,3 +10,9 @@ services:
public: true
tags:
- name: kernel.event_subscriber

ContaoCommunityAlliance\DcGeneral\Contao\View\Contao2BackendView\EventListener\CreateSubHeadlineListener:
public: false
tags:
- name: kernel.event_listener
event: dc-general.view.contao2backend.get-edit-mask-subheadline

0 comments on commit 2a25827

Please sign in to comment.