4

I'm attempting to create my first plugin! However, I want the user to be able to define a number that's then used in specific parts of the plugin. This plugin consists only of an xml file and a php file. How would I grab this specific parameter from the xml file that's submitted from the user, and then utilize it in the PHP?

Here's the XML below.

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="1.0.5" method="upgrade" group="content">
<name>Content - Article Title Truncator</name>
<creationDate>2016-01-16</creationDate>
<version>1.0.5</version>
<releaseDate>2016-01-16 07:08:00</releaseDate>
<releaseType>First public release!</releaseType>
<author>Michael Baas / Jesse Humphry</author>
<authorEmail>jesse.humphry@gmail.com</authorEmail>
<authorUrl>http://www.geeksgamers.com</authorUrl>
<copyright>(c) 2016 Jesse Humphry</copyright>
<description>This plugin has been adapted from a code publically released on
    stackoverflow.com by Michael Baas in 2013. It has been adapted for 
    Joomla 3.x and given a back-end option for selection of character limit.
</description>
<files>
<filename plugin="Article Title Truncator">foo.php</filename>
</files>
<config>
<fields name="params">
    <fieldset name="Basic">
    <field name="Character Limit" type="text" default="25" filter="integer" maxlength="5" label="Character Limit">
    </field>
    </fieldset>
</fields>
</config>
</extension>

And here's the PHP file.

<?php

class plgContentFoo extends JPlugin
    {
    public function onContentPrepare($context,$article,$params,$limitstart)
    {

    $view = JRequest::getCmd('view');
    $fp = in_array($view,array("featured","addmore"));

        if ($fp && 34<strlen($article->title)) {
            $article->title = trim(substr($article->title,0,31)) . "...";
            return true;
        }
    }


}

?>

I need to get whatever is input from the XML form's "Character Limit" field and use it in place of the first integer in the PHP form. Now I'm new to all of this, so please be gentle.

2

Firstly, you should always use a single string for the name attribute. If there are multiple words, it's best to separate with with an underscore and always ensure all characters are lowercase to avoid confusion.

So in your case, replace:

name="Character Limit"

with:

name="character_limit"

Note: Any XML parameter changes that are made when the plugin is installed, require you to "Save" once done.


You can then simply call the parameter value like so:

$value = $this->params->get('character_limit', 50);

The second argument (50) is just an example and is used as a default value if no value by the user has been specified.

Hope this helps


Oh and on a side note, you've tagged this question as Joomla 3.x, therefore JRequest which you are using on your code is deprecated. Instead, use Jinput and an example can be seen here

And version="1.0.5" at the top of your XML should be the Joomla version, not your plugin version ;)

  • Thanks a ton, mate! I just did a bit of fiddling with your invaluable information and I now have my first plugin working! Much appreciated! – Jesse Humphry Jan 17 '16 at 3:14
  • 1
    @JesseHumphry - You're welcome. Glad it helped :) – Lodder Jan 17 '16 at 3:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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