Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add event to manipulate sub-headline in edit mask
- Loading branch information
Showing
4 changed files
with
201 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/Contao/View/Contao2BackendView/Event/GetEditMaskSubHeadlineEvent.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
src/Contao/View/Contao2BackendView/EventListener/CreateSubHeadlineListener.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters