2

I have a Joomla plugin I wrote. There is a field for the Joomla Administrator to fill-in, it can't be left blank and there is no good default value to put in there.

How can I force it that the plugin can't be enabled unless the Joomla Administrator fills in the require field? Is this something that can be accomplished in the .xml manifest? Or what other method is most common and practical to do?

1

First in your xml you can set required=true for the field you want to force as required.

<field 
    name="myfield"
    type="text"
    class="inputbox"
    description="Please type your something"
    label="My Something"
    required="true"
    size="10"
    filter="raw"
/>

This won't allow the form to be saved if the field is left empty. But still the plugin can be enabled.

It is possible to write code to disable the plugin, in the next Joomla execution cycle, if that plugin's field is empty, but I think trying to do so it will be an overkill.

What is the best thing here to do, is to check early in your plugin's methods, if that parameter is not empty - or if it holds an expected value. If this test is passed, then let the plugin execute the remaining of its task, otherwise end your plugin's functions routines with a return.

So, early inside your plugin's event method that runs, among your other checkings you can have something like:

if ($this->params->get('myfield') === '') { 
    return true;
}

So if the myfield's value is empty, then you will jump out of the function and nothing more will occur (the plugin won't run further).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.