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 14, 2023
1 parent 5992a89 commit d282375
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Contao/View/Contao2BackendView/EditMask.php
Expand Up @@ -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 @@ -652,24 +653,32 @@ protected function handleSubmit(ModelInterface $model)
*/
protected function getHeadline()
{
$environment = $this->getEnvironment();
$definitionName = $this->getDataDefinition()->getName();
$translator = $this->getEnvironment()->getTranslator();
$translator = $environment->getTranslator();

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

if ('editRecord' !== $headline) {
return $headline;
$subHeadline = $headline;
}
return $translator->translate('MSC.editRecord', null, [$this->model->getId()]);
$subHeadline = $translator->translate('MSC.editRecord', null, [$this->model->getId()]);
}

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

return $translator->translate('MSC.editRecord', null, ['']);
$subHeadline = $translator->translate('MSC.editRecord', null, ['']);

$event = new GetEditMaskSubHeadlineEvent($this->environment, $this->model);
$event->setHeadline($subHeadline);

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

return $event->getHeadline();
}

/**
Expand Down
@@ -0,0 +1,90 @@
<?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
*/
protected $model;

/**
* 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()
{
return $this->model;
}

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

return $this;
}

/**
* Get the sub-headline.
*
* @return string
*/
public function getHeadline(): string
{
return $this->subHeadline;
}
}

0 comments on commit d282375

Please sign in to comment.