Add a CSS to block plugin in 3.2.0-1

Hi there,

I’m trying to make a custom block plugin for OJS 3.2.0-1, but I’ve encountered a problem with adding a stylesheet to this plugin.

Usually I would do something like this:

  function displayCallback($hookName, $params) {
    $template = $params[1];
    $templateMgr = $params[0];
    $request = PKPApplication::get()->getRequest();
    $templateMgr->addStylesheet('languageFlags', $request->getBaseUrl() . DIRECTORY_SEPARATOR . $this->getPluginPath() . DIRECTORY_SEPARATOR . 'css' . DIRECTORY_SEPARATOR . 'languageFlags.css');

return false;
}

But it only works with generic plugins, not with block plugins. So I guess, something has changed there? Could you recommend a plugin where I can see how it is done, now?

Kind regards
Daniela

Hello again,

I’m still working on this problem and I think it might be a problem with this line:

HookRegistry::register(‘TemplateManager::display’, array($this, ‘displayCallback’));

I have the impression the function isn’t called properly in the block plugins. Nothing what happens in there works in my installation (in generic plugins it works, unless it behaves like a block plugin). Does anyone noticed something similar?

Kind regards
Daniela

Hi @UBWolf,

I suspect you’re running into differences in how plugins are loaded depending on their categories. The generic plugin category is loaded the earliest in handling a request; others are loaded later, or not at all depending on what the user is doing and what kind of plugin it is.

In the case of block plugins, these are loaded in lib/pkp/classes/template/PKPTemplateManager.inc.php in the displaySidebar function:

$plugins = PluginRegistry::loadCategory('blocks', true);

The displaySidebar function is attached to the Templates::Common::Sidebar hook, which is called from the lib/pkp/templates/frontend/components/footer.tpl template (or equivalent, if using a theme that overrides it). So the block plugins will only be able to react to hooks that are called after that template is prepared for sending to the user – which will be pretty late in the request lifecycle.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

this sound very comprehensible. Has the loading order been changed after 3.1.2? With the old OJS version it worked, so I think something must have been changed.

Kind regards
Daniela

Hi @UBWolf,

I’m not aware of anything that’s changed related to this since OJS 3.1.2. I’d suggest double-checking the versions table to make sure your plugin’s entry is accurate – if it isn’t, that can sometimes cause problems with hook registration.

Regards,
Alec Smecher
Public Knowledge Project Team