Injecting content in a view with the view.layout event

Hi all, I am writing a module which should be able to add content to the body of any view of a site. So I have attached a listener to the view.layout event :

public function attachListeners(SharedEventManagerInterface $sharedEventManager) {
    $sharedEventManager->attach('*', 'view.layout', [$this, 'hookViewLayout']);
}

Then in my “hook” function, I want to inject custom content to the body of the view:

public function hookViewLayout(Event $event)
{
    $services = $this->getServiceLocator();
    if (!$services->get('Omeka\Status')->isSiteRequest()) {
        return;
    }
    $view = $event->getTarget();
    // inject custom content
}

Reading the Laminas View documentation, I could not find a way to do that. Could someone point me in the right direction please?

Hi again, I understand there are more questions than answers, but I am quite sure someone else has already solved this problem or even has an idea about how to solve it :wink:

We often use this event to inject into the various placeholders used for parts of the <head>: for scripts, stylesheets, etc.

For the body… I believe in the view object you should have a ->content property that contains the body to be rendered, and you can modify that?

Thanks John, this will help for sure. For my understanding, can you please tell me where the ->content property is set in Omeka’s code?

This is how the Laminas layout system works, see the Laminas docs on views. We’re not doing anything within Omeka’s code that’s special for this.

Basically the view for the current action gets rendered first, and that result gets set to content on the layout, and the layout renders after.