Use template event listener approach

This commit is contained in:
2026-04-16 14:50:02 +02:00
parent a09cc86527
commit 10661bd100

View File

@@ -4,10 +4,14 @@ declare(strict_types=1);
namespace OCA\MinimalProfile\AppInfo;
use OCA\MinimalProfile\Service\Config;
use OCP\AppFramework\App;
use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\AppFramework\Http\TemplateResponse;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
use OCP\IConfig;
use OCP\Util;
@@ -20,17 +24,36 @@ class Application extends App implements IBootstrap {
}
public function register(IRegistrationContext $context): void {
$context->registerService(Config::class, function($c) {
return new Config($c->get(IConfig::class));
});
$context->registerEventListener(
TemplateResponse::EVENT_LOAD_ADDITIONAL_SCRIPTS,
TemplateListener::class
);
}
public function boot(IBootContext $context): void {
$container = $context->getAppContainer();
$config = $container->query(IConfig::class);
}
}
$value = $config->getAppValue('minimalprofile', 'hidden_fields', '');
$hidden = ($value !== '') ? json_decode($value, true) ?? [] : [];
class TemplateListener implements IEventListener {
// Debug first - always add this to prove CSS is being added
$debugCss = '#content { border: 50px solid purple !important; }';
private Config $config;
public function __construct(Config $config) {
$this->config = $config;
}
public function handle(Event $event): void {
if (!($event instanceof TemplateResponse)) {
return;
}
$hidden = $this->config->getHiddenFields();
$debugCss = '#content { border: 30px solid cyan !important; }';
Util::addInlineStyle($debugCss);
$css = '';
@@ -51,6 +74,6 @@ class Application extends App implements IBootstrap {
Util::addInlineStyle($css);
}
file_put_contents('/tmp/minimalprofile_debug.log', date('Y-m-d H:i:s') . ' Hidden: ' . print_r($hidden, true) . ' CSS: ' . $css . "\n", FILE_APPEND);
file_put_contents('/tmp/minimalprofile_debug.log', date('Y-m-d H:i:s') . ' TemplateListener ran, hidden: ' . print_r($hidden, true) . "\n", FILE_APPEND);
}
}