|
| 1 | +--- |
| 2 | +title: Plugin Categories - Plugin Guide for OJS, OMP and OPS |
| 3 | +book: dev-plugin-guide |
| 4 | +version: 3.4 |
| 5 | +--- |
| 6 | + |
| 7 | +# Plugin Categories |
| 8 | + |
| 9 | +A plugin's category determines when it is loaded and how it can modify the application. For example, a [block](#blocks) plugin can add a block of content to the sidebar in the reader-facing website. But it can't do anything else and won't be loaded on the backend. |
| 10 | + |
| 11 | +Each plugin must extend one of the plugin category classes. In the [Getting Started](./getting-started) tutorial, the Tutorial Example plugin extended the `GenericPlugin` class. |
| 12 | + |
| 13 | +```php |
| 14 | +namespace APP\plugins\generic\tutorialExample; |
| 15 | + |
| 16 | +use PKP\plugins\GenericPlugin; |
| 17 | + |
| 18 | +class TutorialExamplePlugin extends GenericPlugin |
| 19 | +{ |
| 20 | + ... |
| 21 | +} |
| 22 | +``` |
| 23 | + |
| 24 | +A block plugin will extend the `BlockPlugin` class. |
| 25 | + |
| 26 | +```php |
| 27 | +namespace APP\plugins\blocks\tutorialBlock; |
| 28 | + |
| 29 | +use PKP\plugins\BlockPlugin; |
| 30 | + |
| 31 | +class TutorialBlockPlugin extends BlockPlugin |
| 32 | +{ |
| 33 | + ... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Each plugin category class provides methods that must be implemented. For example, a [report](#reports) plugin extends the `ReportPlugin` class and implements the `ReportPlugin::display()` method to deliver a CSV file with the report contents. |
| 38 | + |
| 39 | +```php |
| 40 | +namespace APP\plugins\reports\tutorialExample; |
| 41 | + |
| 42 | +use PKP\plugins\ReportPlugin; |
| 43 | + |
| 44 | +class TutorialExamplePlugin extends ReportPlugin |
| 45 | +{ |
| 46 | + public function display($args, $request) |
| 47 | + { |
| 48 | + header('content-type: text/comma-separated-values'); |
| 49 | + header('content-disposition: attachment; filename=reviews.csv'); |
| 50 | + $fp = fopen('php://output', 'wt'); |
| 51 | + fputcsv($fp, [/* the review details in the report */]); |
| 52 | + fclose($fp); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Blocks {#blocks} |
| 58 | + |
| 59 | +Block plugins provide content that can be displayed in the sidebar on any page of the public-facing website. They require a template file. |
| 60 | + |
| 61 | +``` |
| 62 | +ojs |
| 63 | +│ |
| 64 | +├─┬ plugins |
| 65 | +│ │ |
| 66 | +│ └─┬ blocks |
| 67 | +│ │ |
| 68 | +│ └─┬ madeBy |
| 69 | +│ │ |
| 70 | +│ ├─┬ templates |
| 71 | +│ │ └── block.tpl |
| 72 | +│ ├── MadeByPlugin.php |
| 73 | +│ └── version.xml |
| 74 | +``` |
| 75 | + |
| 76 | +The template file should include all of the HTML for your block. |
| 77 | + |
| 78 | +```html |
| 79 | +<div class="pkp_block block_madeBy"> |
| 80 | + Made with ❤ by the Public Knowledge Project |
| 81 | +</div> |
| 82 | +``` |
| 83 | + |
| 84 | +Add a `getContents()` method to pass data to the template. |
| 85 | + |
| 86 | +```php |
| 87 | +namespace APP\plugins\blocks\madeBy; |
| 88 | + |
| 89 | +use PKP\plugins\BlockPlugin; |
| 90 | + |
| 91 | +class MadeByPlugin extends BlockPlugin |
| 92 | +{ |
| 93 | + public function getContents($templateMgr, $request = null) |
| 94 | + { |
| 95 | + $templateMgr->assign([ |
| 96 | + 'madeByText' => 'Made with ❤ by the Public Knowledge Project', |
| 97 | + ]); |
| 98 | + |
| 99 | + return parent::getContents($templateMgr, $request); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +```html |
| 105 | +<div class="pkp_block block_madeBy"> |
| 106 | + {$madeByText|escape} |
| 107 | +</div> |
| 108 | +``` |
| 109 | + |
| 110 | +Block plugins can use any HTML code. However, themes provided by PKP expect blocks to use the following markup. |
| 111 | + |
| 112 | +```html |
| 113 | +<div class="pkp_block"> |
| 114 | + <h2 class="title"> |
| 115 | + <!-- Add the title of your block here --> |
| 116 | + </h2> |
| 117 | + <div class="content"> |
| 118 | + <!-- Add the main content for your block here --> |
| 119 | + </div> |
| 120 | +</div> |
| 121 | +``` |
| 122 | + |
| 123 | +## Import/Export {#import-export} |
| 124 | + |
| 125 | +> View an [example import/export plugin](https://github.com/pkp/exampleImportExport). |
| 126 | +{:.notice} |
| 127 | + |
| 128 | +Import/export plugins provide tools for getting data into and out of OJS, OMP, and OPS. They can be used when you are moving between our application and another platform to migrate users, submissions, back issues and more. |
| 129 | + |
| 130 | + |
| 131 | +Each import/export plugin can be run on the command line. |
| 132 | + |
| 133 | +``` |
| 134 | +$ php tools/importExport.php ExampleImportExportPlugin import filename.csv |
| 135 | +``` |
| 136 | + |
| 137 | +Use the `{plugin_url ...}` smarty function in the template to submit a form to one of the import or export paths. |
| 138 | + |
| 139 | +``` |
| 140 | +<form method="POST" action="{plugin_url path="exportAll"}"> |
| 141 | + <button type="submit">Export All</button> |
| 142 | +</form> |
| 143 | +``` |
| 144 | + |
| 145 | +Check the `path` to detect when the form is submitted. |
| 146 | + |
| 147 | +```php |
| 148 | +namespace APP\plugins\importexport\exampleImportExport; |
| 149 | + |
| 150 | +use PKP\plugins\ImportExportPlugin; |
| 151 | + |
| 152 | +class ExampleImportExportPlugin extends ImportExportPlugin |
| 153 | +{ |
| 154 | + // ... |
| 155 | + |
| 156 | + public function display($args, $request) |
| 157 | + { |
| 158 | + parent::display($args, $request); |
| 159 | + |
| 160 | + $path = array_shift($args); |
| 161 | + |
| 162 | + if ($path === 'exportAll') { |
| 163 | + // do something |
| 164 | + } |
| 165 | + } |
| 166 | +} |
| 167 | +``` |
| 168 | + |
| 169 | +## Reports {#reports} |
| 170 | + |
| 171 | +> Reports can return any file format, but they usually generate a CSV file for use in spreadsheet software. |
| 172 | +{:.notice} |
| 173 | + |
| 174 | +Report plugins provide a simple interface for generating a file download. Reports may be generated for article usage statistics, reviewers or anything you want. |
| 175 | + |
| 176 | +Report plugins extend the `ReportPlugin` class and implement the `display()` method. |
| 177 | + |
| 178 | +```php |
| 179 | +namespace APP\plugins\reports\exampleReport; |
| 180 | + |
| 181 | +use APP\submission\Submission; |
| 182 | +use PKP\plugins\ReportPlugin; |
| 183 | + |
| 184 | +class ExampleReportPlugin extends ReportPlugin |
| 185 | +{ |
| 186 | + public function display($args, $request) { |
| 187 | + |
| 188 | + // Get the first 100 submissions |
| 189 | + $submissions = Repo::submission() |
| 190 | + ->getCollector() |
| 191 | + ->filterByContextIds([$context->getId()]) |
| 192 | + ->limit(100) |
| 193 | + ->getMany(); |
| 194 | + |
| 195 | + // Stream to a CSV file |
| 196 | + header('content-type: text/comma-separated-values'); |
| 197 | + header('content-disposition: attachment; filename=articles-' . date('Ymd') . '.csv'); |
| 198 | + $fp = fopen('php://output', 'wt'); |
| 199 | + fputcsv($fp, ['ID', 'Title']); |
| 200 | + /** @var Submission $submission */ |
| 201 | + foreach ($submissions as $submission) { |
| 202 | + fputcsv($fp, [ |
| 203 | + $submission->getId(), |
| 204 | + $submission->getCurrentPublication()->getLocalizedTitle() |
| 205 | + ]); |
| 206 | + } |
| 207 | + fclose($fp); |
| 208 | + } |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +## Themes {#themes} |
| 213 | + |
| 214 | +Themes control the design and layout of a journal, press or preprint server. Read the [Theming Guide](/pkp-theming-guide/en) to learn how to build your own themes. |
| 215 | + |
| 216 | +## Generic {#generic} |
| 217 | + |
| 218 | +Generic plugins are loaded with every request. They hook into the application early in the [Request Lifecycle](/dev/documentation/en/architecture-request) and can be used to modify almost everything. |
| 219 | + |
| 220 | +Generic plugins use [Hooks](/dev/documentation/en/utilities-hooks) to intervene in the application. Hooks should be added in a plugin's `register()` method. |
| 221 | + |
| 222 | +> Always check if the plugin is enabled before adding a hook. Otherwise, your plugin will run even when it has been disabled. |
| 223 | +{:.warning} |
| 224 | + |
| 225 | +```php |
| 226 | +namespace APP\plugins\generic\tutorialExample; |
| 227 | + |
| 228 | +use PKP\plugins\GenericPlugin; |
| 229 | +use PKP\plugins\Hook; |
| 230 | + |
| 231 | +class TutorialExamplePlugin extends GenericPlugin |
| 232 | +{ |
| 233 | + public function register($category, $path, $mainContextId = null) |
| 234 | + { |
| 235 | + $success = parent::register($category, $path); |
| 236 | + |
| 237 | + if ($success && $this->getEnabled()) { |
| 238 | + Hook::add('Example::hookName', [$this, 'doSomething']); |
| 239 | + } |
| 240 | + |
| 241 | + return $success; |
| 242 | + } |
| 243 | + |
| 244 | + public function doSomething(string $hookName, array $args): ?bool |
| 245 | + { |
| 246 | + // Do something... |
| 247 | + |
| 248 | + return false; |
| 249 | + } |
| 250 | +} |
| 251 | +``` |
| 252 | + |
| 253 | +Generic plugins are very powerful and can use any hook in the application. Look at the [examples](./examples) for ideas and learn about the most [common hooks](/dev/documentation/en/utilities-hooks#common-hooks). |
| 254 | + |
| 255 | +## Other {#other} |
| 256 | + |
| 257 | +Other plugin categories are not often used. The best way to learn about them is to read the source code of one of the existing plugins. These categories include: |
| 258 | + |
| 259 | +- `auth` plugins allow you to authorize and synchronize user accounts with a third-party source. |
| 260 | +- `gateways` plugins allow you to add a new URL and respond to requests to that URL. |
| 261 | +- `metadata` plugins implement a description of a metadata format. |
| 262 | +- `oaiMetadataFormats` plugins add a metadata format to the application's OAI endpoint. |
| 263 | +- `paymethod` plugins allow you to implement your own payment handling when using subscription and article fees. |
| 264 | +- `pubIds` plugins allow you to add support for publication identifiers like URNs. |
| 265 | + |
| 266 | +--- |
| 267 | + |
| 268 | +Learn how to [translate your plugin](./translation) so that it can be used by many journals. |
0 commit comments