Issue Extending Classes in Plugin (OJS 3.4.0-7)

Describe the issue or problem
I created a class in my plugin that extends the ReviewFormElement class to modify it, but it remains empty because the other classes that call ReviewFormElement aren’t recognizing or calling the new class I made. As a result, it’s not being populated.

I expected the new class to be instantiated in place of ReviewFormElement, but it looks like the core system isn’t picking it up. How can I ensure that my extended class is used instead of the original one?

Steps I took leading up to the issue

  1. Created a plugin and extended ReviewFormElement with my custom class.
  2. Registered the plugin, but it’s still not using the new class I created.

What application are you using?
OJS 3.4.0-7

Additional information
Any advice on how to correctly override or extend this class and make sure it’s used by the rest of the system would be greatly appreciated. Thanks in advance!

Hi @bilalelkhouly,

I haven’t done this specifically, but I believe you’ll have to extend the relevant DAO classes and override some of their functions too – especially the newDataObject function, which is responsible for instantiating the data object classes.

Regards,
Alec Smecher
Public Knowledge Project Team

Hi @asmecher,

Thanks for the suggestion! I tried extending the ReviewFormElement class as you suggested, but I’m still having trouble getting it to work. Here’s what I currently have:

In my plugin, I created a SliderReviewFormElementDAO class that extends ReviewFormElementDAO, and in the newDataObject() method, it returns an instance of my custom SliderReviewFormElement class. My SliderReviewFormElement class extends ReviewFormElement and includes the new slider type (constant REVIEW_FORM_ELEMENT_TYPE_SLIDER = 7), with the necessary options in getReviewFormElementTypeOptions().

Here are the main parts of the code:

<?php

namespace APP\plugins\generic\sliderItemType\classes\reviewForms;

use PKP\reviewForm\ReviewFormElementDAO;
use APP\plugins\generic\sliderItemType\classes\reviewForms\SliderReviewFormElement;

class SliderReviewFormElementDAO extends ReviewFormElementDAO
{
    public function newDataObject()
    {
        return new SliderReviewFormElement();
    }
    
}
<?php

namespace APP\plugins\generic\sliderItemType\classes\reviewForms;

use PKP\reviewForm\ReviewFormElement;

class SliderReviewFormElement extends ReviewFormElement
{
    public const REVIEW_FORM_ELEMENT_TYPE_SLIDER = 7;  // Define a new constant for the slider

    /**
     * Override or extend any methods you need
     */
    public function getReviewFormElementTypeOptions()
    {
        return [
            '' => 'manager.reviewFormElements.chooseType',
            self::REVIEW_FORM_ELEMENT_TYPE_SMALL_TEXT_FIELD => 'manager.reviewFormElements.smalltextfield',
            self::REVIEW_FORM_ELEMENT_TYPE_TEXT_FIELD => 'manager.reviewFormElements.textfield',
            self::REVIEW_FORM_ELEMENT_TYPE_TEXTAREA => 'manager.reviewFormElements.textarea',
            self::REVIEW_FORM_ELEMENT_TYPE_CHECKBOXES => 'manager.reviewFormElements.checkboxes',
            self::REVIEW_FORM_ELEMENT_TYPE_RADIO_BUTTONS => 'manager.reviewFormElements.radiobuttons',
            self::REVIEW_FORM_ELEMENT_TYPE_DROP_DOWN_BOX => 'manager.reviewFormElements.dropdownbox',
            self::REVIEW_FORM_ELEMENT_TYPE_SLIDER => 'manager.reviewFormElements.slider',
        ];
    }

    public function getMultipleResponsesElementTypes()
    {
        return [
            self::REVIEW_FORM_ELEMENT_TYPE_CHECKBOXES,
            self::REVIEW_FORM_ELEMENT_TYPE_RADIO_BUTTONS,
            self::REVIEW_FORM_ELEMENT_TYPE_DROP_DOWN_BOX,
            self::REVIEW_FORM_ELEMENT_TYPE_SLIDER,
        ];
    }
}

I also created the necessary templates for rendering the slider, but the system doesn’t seem to be calling my custom SliderReviewFormElementDAO class, so the new slider type isn’t showing up in the dropdown for the element types.

I think I’ve implemented what you suggested regarding the newDataObject() function, but I’m still missing something to get it fully working.

Any further advice would be appreciated!

Hi @bilalelkhouly,

You’ll have to the DAORegistry::registerDAO to register your overridden DAO.

Regards,
Alec Smecher
Public Knowledge Project Team

1 Like

Hi @asmecher,

Thank you for your response I indeed did that and it worked now! I just had one more question, if the class doesn’t have a related DAO classes, for example ReviewFormElementForm.php, if I want to extend that class how would I get it to work? Thank you once again.

Hi @bilalelkhouly,

I actually recommend avoiding overriding classes if you can, because another plugin wouldn’t be able to come along and use the same approach without colliding with the first. If it’s possible, it’s better to use hooks. For the form class, I’d suggest looking for hook calls you can use. In most cases, form classes call the hooks in the parent class implementation; see lib/pkp/classes/form/Form.inc.php and look for calls to HookRegistry::call.

Regards,
Alec Smecher
Public Knowledge Project Team

This topic was automatically closed after 15 days. New replies are no longer allowed.