How to add JavaScript code to contributor form in OMP 3.4

Hi everyone,

I have a question regarding the contributor form in OMP 3.4. For OMP 3.3. I used Common::UserDetails::AdditionalItems to add a new field + javascript via a template. For OMP 3.4. I used Form::config::before to add a new field like so:

$form->addField(new FieldText('myField', [
            'label' => __('plugins.generic.myField.field.title'),
	        'description' => __('plugins.generic.myField.field.desc'),
            'isRequired' => true,
        ]), [FIELD_POSITION_BEFORE, 'email']);

However, I am not sure how to add JavaScript this way. I found this post that suggested to use FieldHTML: Insert in submission_settings table - #6 by tnier01

But maybe there is a better way to do this (or a hook that I am overlooking)?


Since authorForm.tpl (which uses userDetails.tpl and thus the Common::UserDetails::AdditionalItems Hook) is deprecated in 3.4.I found a workaround that is certainly not perfect:
Using the Form::config::before hook, I added my field as mentioned above. Then I used the following code in the same hook function:

	$request = Application::get()->getRequest();
	$url = $request->getBaseUrl() . '/' . $this->getPluginPath() . '/js/myFieldJs.js';
	$templateMgr = TemplateManager::getManager($request);
	$templateMgr->addJavaScript('myFieldJs',$url, ['contexts' => 'backend'] );

In the JavaScript file I used a bunch of query selectors and event listeners to get the effect I wanted but it is a very clumsy solution. If anyone still has any advice on how to add fields and javascript to the contributor form in OMP 3.4., it would be nice to know.

Hi @vmayer,

The latest release of the custom header plugin allows for content to be added to editorial back-end pages as well; you might try that.

Alec Smecher
Public Knowledge Project Team

1 Like