How to create a plugin to change a button behavior

Hello,
I have recently started to work with OJS and I am in charge to recover 3 small journals in the same instance on OJS release 2.4.3. The software has been modified, not by me, and not with a clean manner. I have installed the ojs-stable-2_4_6 branch with GIT. I shall redo the previous modifications and others in the future.
I plan to do these modifications by creating plugins that each journal will activate or not.
The first modification is to change the behaviour of a menu item (User home : when the user is login to go to the user home page of the journal, not the general user page). This is simple, but may be nice for me to understand how to write a plugin.
So my questions:
. Is it a good option to create a plugin or plugins to modify code ?
. Is it possible to change a menu behavior with a plugin ?
. Where can I find a good and simple example of plugin similar I want to do ? I found :
http://pkp.sfu.ca/wiki/index.php?title=Reference_Plugins
http://pkp.sfu.ca/wiki/index.php?title=Writing_a_Block_Plugin
https://www.martineve.com/2011/09/17/writing-a-plugin-for-open-journal-systems-part-1/ (old)
Thanks,
Jean-Luc

Hi @JLA,

The most varied plugins are in the “generic” category; you can find a lot of diverse plugins there. See plugins/generic for these.

A typical way to modify a small piece of a page is to use the TemplateManager hooks to intercept requests for a specific template; see e.g. plugins/generic/googleAnalytics/GoogleAnalyticsPlugin.inc.php for an example of this.

Another way to alter generated output us to use a Smarty postfilter. There are no examples of this included in OJS, but have a look at the Smarty documentation for information on postfilters; your plugin would need to register one of these.

If you can describe a little more specifically what your goal is, I can potentially give you more detailed suggestions.

Regards,
Alec Smecher
Public Knowledge Project Team

Thanks Alec,
To be more precise, we want to modify the result of the ‘User Home’ button when a user is logged as member of a journal.

Eg when a user is logged as an author of EJP journal :
. On page :
http://test-ojs.math.cnrs.fr/index.php/JEP/index
We want that :
‘User home’ → http://test-ojs.math.cnrs.fr/index.php/JEP/user
and not http://test-ojs.math.cnrs.fr/index.php/index/user.
This can be done by modifying templates/common/navbar.tpl.

But we’d like to not modify the code. Is it possible to do that by writing a plugin ?

I hope this explanation clarify my question

Thanks,
Jean-Luc

Hi @JLA,

I think the Smarty postfilter method would work best here. You’ll need to come up with a regular expression or substring to watch for (e.g. including the “user home” URL that you want to replace), and either preg_replace or substr_replace or similar to put the new URL in its place.

Regards,
Alec Smecher
Public Knowledge Project Team

Just to inform you the results of my tests.
I gave up the idea of creating a plugin to do the job :
. With the classic hook system the only hook I’ve found is
Templates::Common::Header::Navbar::CurrentJournal but we can only add a menu, not modify one
. I have not found the way to register a smarty postfilter. Without example it’s too hard for me.
So, I have modified navbar.tpl. But I have learned how to create a plugin based on a OJS hook. Thanks.
Regards,
JLuc.

Hi @JLA,

Actually, a Smarty output filter may be what you’re looking for, not a postfilter. Check the following examples (these ship with OJS 2.4.x):

  • plugins/generic/openAds/OpenAdsPlugin.inc.php
  • plugins/generic/dataverse/DataversePlugin.inc.php
  • plugins/generic/sehl/SehlPlugin.inc.php
  • plugins/generic/roundedCorners/RoundedCornersPlugin.inc.php

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

We have an installation of 3.1.0. We want to alter the way the usagestats chart is displayed on article pages. usageStats is already a plugin, shall I write a plugin to alter it’s behavior? What is the best way to do it without changing the core source code?

In my case PKPUsageStatsPlugin::loadJavascript and UsageStatsFrontendHandler.js have to be changed.

Regards
Ghazal

Hi @salehig,

I’m not sure I can provide detailed guidance on this, but it should be possible to create a generic plugin that adds javascript after the Usage Stats Plugin javascript in order to override its behavior. Without knowing exactly what you’re doing, I suspect adding some late-loading javascript to intervene might be enough.

Regards,
Alec Smecher
Public Knowledge Project Team