-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBackendUiExamplePlugin.inc.php
163 lines (142 loc) · 4.93 KB
/
BackendUiExamplePlugin.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/**
* @file BackendUiExamplePlugin.inc.php
*
* Copyright (c) 2023 Simon Fraser University
* Copyright (c) 2023 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @brief Backend Ui Example Plugin
*/
use PKP\plugins\GenericPlugin;
use PKP\plugins\Hook;
use PKP\components\forms\FormComponent;
use PKP\components\forms\FieldText;
class BackendUiExamplePlugin extends GenericPlugin {
/**
* @copydoc Plugin::register()
*
* @param null|mixed $mainContextId
*/
public function register($category, $path, $mainContextId = null)
{
if (parent::register($category, $path, $mainContextId)) {
if ($this->getEnabled($mainContextId)) {
// To Attach smarty template to the settings template
Hook::add('Template::Settings::website', [$this, 'callbackShowWebsiteSettingsTabs']);
// To Attach data to the page, so it can be passed
Hook::add('TemplateManager::display', [$this, 'callbackTemplateManagerDisplay']);
// Registering build file for JS and CSS to be loaded
$request = Application::get()->getRequest();
$templateMgr = TemplateManager::getManager($request);
$this->addJavaScript($request, $templateMgr);
$templateMgr->addStyleSheet('backendUiExampleStyle',"{$request->getBaseUrl()}/{$this->getPluginPath()}/public/build/build.css", [
'contexts' => ['backend']
] );
}
return true;
}
return false;
}
public function addJavaScript($request, $templateMgr)
{
$templateMgr->addJavaScript(
'BackendUiExample',
"{$request->getBaseUrl()}/{$this->getPluginPath()}/public/build/build.iife.js",
[
'inline' => false,
'contexts' => ['backend'],
'priority' => TemplateManager::STYLE_SEQUENCE_LAST
]
);
}
/**
* Extend the website settings tabs to include example plugin
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackShowWebsiteSettingsTabs($hookName, $args)
{
$templateMgr = $args[1];
$output = & $args[2];
$output .= $templateMgr->fetch($this->getTemplateResource('settingsTab.tpl'));
// Permit other plugins to continue interacting with this hook
return false;
}
/**
* Create a simple example form using FormComponent
*
* @return FormComponent
*/
public function getExampleForm()
{
$request = Application::get()->getRequest();
$context = $request->getContext();
$dispatcher = $request->getDispatcher();
$site = $request->getSite();
$locales = $site->getSupportedLocaleNames();
$locales = array_map(fn (string $locale, string $name) => ['key' => $locale, 'label' => $name], array_keys($locales), $locales);
// Create the form
$form = new FormComponent(
'examplePluginForm',
'POST',
$dispatcher->url(
$request,
Application::ROUTE_API,
$context->getData('urlPath'),
'examplePlugin/saveSettings'
),
$locales
);
// Add text field
$form->addField(new FieldText('exampleTitle', [
'label' => __('plugins.generic.backendUiExample.form.title'),
'isRequired' => true,
'groupId' => 'exampleSettings',
'value' => '',
]));
return $form;
}
/**
* Add state for your component when needed
*
* @param string $hookName The name of the invoked hook
* @param array $args Hook parameters
*
* @return bool Hook handling status
*/
public function callbackTemplateManagerDisplay($hookName, $args)
{
$templateMgr = $args[0];
$request = & Registry::get('request');
$dispatcher = $request->getDispatcher();
$context = $request->getContext();
if($request->getRequestedOp() == 'settings') {
// Get the form configuration
$form = $this->getExampleForm();
$templateMgr->setState([
'backendUiExampleData' => [
'customForm' => $form->getConfig()
]
]);
}
// Permit other plugins to continue interacting with this hook
return false;
}
/**
* Get the display name of this plugin.
* @return String
*/
function getDisplayName() {
return __('plugins.generic.backendUiExample.displayName');
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return __('plugins.generic.backendUiExample.description');
}
}