Merge pull request #1841 from Icinga/feature/generic-object-form-hook

IcingaObjectFormHook: new generic hook
This commit is contained in:
Thomas Gelf 2019-04-23 18:22:35 +02:00 committed by GitHub
commit 93a882f8c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 62 additions and 2 deletions

View File

@ -0,0 +1,22 @@
<?php
namespace Icinga\Module\Director\Hook;
use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use Icinga\Web\Hook;
abstract class IcingaObjectFormHook
{
protected $settings = [];
abstract public function onSetup(DirectorObjectForm $form);
public static function callOnSetup(DirectorObjectForm $form)
{
/** @var static[] $implementations */
$implementations = Hook::all('director/IcingaObjectForm');
foreach ($implementations as $implementation) {
$implementation->onSetup($form);
}
}
}

View File

@ -38,7 +38,7 @@ abstract class DirectorForm extends QuickForm
]);
}
protected function addBoolean($key, $options, $default = null)
public function addBoolean($key, $options, $default = null)
{
if ($default === null) {
return $this->addElement('OptionalYesNo', $key, $options);

View File

@ -8,6 +8,7 @@ use Icinga\Module\Director\Db;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Data\Db\DbObjectWithSettings;
use Icinga\Module\Director\Exception\NestingError;
use Icinga\Module\Director\Hook\IcingaObjectFormHook;
use Icinga\Module\Director\IcingaConfig\StateFilterSet;
use Icinga\Module\Director\IcingaConfig\TypeFilterSet;
use Icinga\Module\Director\Objects\IcingaTemplateChoice;
@ -772,14 +773,18 @@ abstract class DirectorObjectForm extends DirectorForm
$this->setDefaultsFromObject($this->object);
}
$this->prepareFields($this->object());
IcingaObjectFormHook::callOnSetup($this);
if ($this->hasBeenSent()) {
$this->handlePost();
}
try {
$this->loadInheritedProperties();
$this->addFields();
$this->callOnRequestCallables();
} catch (Exception $e) {
$this->addUniqueException($e);
return;
}
if ($this->shouldBeDeleted()) {
@ -918,7 +923,7 @@ abstract class DirectorObjectForm extends DirectorForm
return $this->getSentValue($name) === $this->getElement($name)->getLabel();
}
protected function abortDeletion()
public function abortDeletion()
{
if ($this->hasDeleteButton()) {
$this->setSentValue($this->deleteButtonName, 'ABORTED');

View File

@ -68,6 +68,10 @@ abstract class QuickForm extends QuickBaseForm
protected $calledSuccessCallbacks = false;
protected $onRequestCallbacks = [];
protected $calledOnRequestCallbacks = false;
public function __construct($options = null)
{
parent::__construct($options);
@ -447,6 +451,32 @@ abstract class QuickForm extends QuickBaseForm
$this->redirectOnSuccess();
}
/**
* @param callable $callable
* @return $this
*/
public function callOnRequest($callable)
{
if (! is_callable($callable)) {
throw new InvalidArgumentException(
'callOnRequest() expects a callable'
);
}
$this->onRequestCallbacks[] = $callable;
return $this;
}
protected function callOnRequestCallables()
{
if (! $this->calledOnRequestCallbacks) {
$this->calledOnRequestCallbacks = true;
foreach ($this->onRequestCallbacks as $callable) {
$callable($this);
}
}
}
/**
* @param callable $callable
* @return $this
@ -539,6 +569,7 @@ abstract class QuickForm extends QuickBaseForm
protected function onRequest()
{
$this->callOnRequestCallables();
}
public function setRequest(Request $request)
@ -550,6 +581,8 @@ abstract class QuickForm extends QuickBaseForm
$this->request = $request;
$this->prepareElements();
$this->onRequest();
$this->callOnRequestCallables();
return $this;
}