简体   繁体   中英

First attempt php Joomla! plugin

New to php and plugin building. I am a front end guy but need to learn templates building in Joomla. I have a small one working that gives you the ability to insert and change some text in the function onContentAfterTitle .

class plgContentmyplugin extends JPlugin
{
public function onContentAfterTitle($context, $article, $params,  $limitstart)
    {
        if ($this->params->get('alt-text')) {
            return $this->params->get('alt-text');
        } else {
            return "<p>Hello World!</p>";
        }

Now I am understanding that the XML determines the backend options in the admin control panel. Then using the php to display it.

I want to be able to extend this plugin, for mostly learning purposes. I have some XML displaying options to change font colour and size. I am just a little unsure on the php and which function I should be calling in order to achieve that. Should I be using one of the other parameters? eg $context or $article? Any help would be much appreciated.

<field name="font-size" type="list" default="12" description="What size font should the message use?" label="Font size">
                <option value="8">8px</option>
                <option value="12">12px</option>
                 <option value="16">16px</option>
            </field>

Again my intention here is to expand my php knowledge and ultimately be able to build templates that other people can use. I have already worked out how to assemble and hack a template by just styling what is already there which would be totally acceptable but I am not sure how much the end user will need to change and the options they will need. Which does require building on the backend.

If I understand correctly from your question, you want to add params to your plugin backend and use whatever it represents in the plugin output?

If so, first step is to add the param XML, ie:

<field type="radio" name="isenabled" label="Is enabled?" class="btn-group btn-group-yesno" default="1">
                <option value="1">JYes</option>
                <option value="0">JNo</option>
            </field>

For most params, your fields will sit inside the "basic" fieldset. For more on Joomla field types: https://docs.joomla.org/Standard_form_field_types

Then, in your plugin template file you simply get the param in which ever way you want. In my example I use my "isenabled" param in an if statement:

if ($this->params->get("isenabled")) { ... }

If I used instead

echo $this->params->get("isenabled);

I would just get back the value the param is set to in the backend; in this case, yes or no.

This also applies to modules.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM