Adding new field to Journal Sections via plugin

Hi,

Any idea, how to add a customised field to the context sections by using a plugin?

I am trying to do something like the code below, but instead of adding the field to the context form, I want to add it to the journal sections form.
The problem is that I cannot find the Sections form id.

class InstitutionalHomePlugin extends GenericPlugin {

  public function register($category, $path, $mainContextId = null) {
		$success = parent::register($category, $path);
		if ($success && $this->getEnabled()) {

      // Use a hook to extend the context entity's schema
      HookRegistry::register('Schema::get::context', array($this, 'addToSchema'));

      // Use a hook to add a field to the masthead form in the journal/press settings.
      HookRegistry::register('Form::config::before', array($this, 'addToForm'));
		}
		return $success;
  }

  /**
   * Extend the context entity's schema with an institutionalHome property
   */
  public function addToSchema($hookName, $args) {
		$schema = $args[0];
    $schema->properties->institutionalHome = new stdObject();
    $schema->properties->institutionalHome->type = 'string';
    $schema->properties->institutionalHome->multilingual = true;
    $schema->properties->institutionalHome->validation = ['nullable'];
  }

  /**
   * Extend the masthead form to add an institutionalHome input field
   * in the journal/press settings
   */
	public function addtoForm($hookName, $form) {

    // Only modify the masthead form
		if (!defined('FORM_MASTHEAD') || $form->id !== FORM_MASTHEAD) {
			return;
    }

    // Don't do anything at the site-wide level
		$context = Application::getRequest()->getContext();
		if (!$context) {
			return;
    }

    // Add a field to the form
		$form->addField(new \PKP\components\forms\FieldText('institutionalHome', [
			'label' => 'Institutional Home',
			'groupId' => 'publishing',
			'value' => $context->getData('institutionalHome'),
		]));
	}
}

Thanks

Hi @sm2020,

What OJS version are you working with?

Hi @Vitaliy,

I am using OJS 3.2.0

I can add the field to the Journal masthead and contact forms but not the rest, not sure where I can find the group id and the form id?

Thanks
Salman

Interesting, this must be a new approach introduced to OJS 3.2. I’ve never worked with it.

@NateWr, will this work for all forms?

Hi @sm2020,

As you’ve discovered, there are two different form systems in use. We plan to work on documentation for both systems over the next six months.

The one you found for adding to context settings is our new form system, which relies on the REST API. It looks like you have found the example plugin showing how to do this.

The sections have not yet been migrated to this new setup. There is no schema or REST API for sections, so you can not extend them in the same way. It is not so easy to extend the forms in the old system, to add a field here or there. You typically need to override the template completely. The authorRequirements plugin does this so it may be a guide for you.

Hi @NateWr I just wanted to say thanks!, your help in this thread has been essential in understanding how to finalise the plugin Salman has been working on. I am going to create a new thread in the forum with a brief explanation of what the plugin does and where to retrieve the open source code for it.

Thanks to @Vitaliy as well for having started to help Salman and to @ajnyga for his offline help :smiley:

Thanks again!

3 Likes

That’s great @FrancescoDeVirgilio! Please do post about the plugin and tag me to make sure I see it. :+1: