2

I'm trying to figure out how to add/extend the "Filter" options for a Custom Field. I first started looking to see if I could figure out where I could add my own filter to the list of options for the "Text" Custom Field. The options RAW, SafeHTML, Text, Alpha Numeric, Integer, Telephone etc are not ideal for my use case. Unable to see how I could extend the existing Text type of Custom Field filters, I figured I'd try creating my own Custom Field plugin, so I did that and basically duplicated the Text Custom Field plugin. However I'm still unable to make my custom field plugin come with my own special filter. I can't seem to figure out where the logic for this is handled. I'm able to add my own filter option in the custom field plugin XML file but that's as far as I got.

I'm technically wanting something like a REGEX choice where I can be very specific as to what is allowed and stripped out when saved. Even if I was to hard code the REGEX in the filter code if it was too complicated for it to be user set when configuring the custom field would be sufficient.

Here is my source of my plugin: plg_fields_fb fb.xml

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" version="3.7.0" group="fields" method="upgrade">
    <name>plg_fields_fb</name>
    <author>Joomla! Project</author>
    <creationDate>November 2005</creationDate>
    <copyright>Copyright (C) 2005 - 2013 Open Source Matters. All rights reserved.</copyright>
    <license>GNU General Public License version 2 or later; see LICENSE.txt</license>
    <authorEmail>admin@joomla.org</authorEmail>
    <authorUrl>www.joomla.org</authorUrl>
    <version>3.1.0</version>
    <description>PLG_FIELDS_FB_XML_DESCRIPTION</description>
    <files>
        <filename plugin="fb">fb.php</filename>
        <filename>fb.xml</filename>
        <filename>index.html</filename>
        <folder>params</folder>
        <folder>tmpl</folder>
        <folder>rules</folder>
    </files>


    <languages>
        <language tag="en-GB">en-GB.plg_fields_fb.ini</language>
        <language tag="en-GB">en-GB.plg_fields_fb.sys.ini</language>
    </languages>
    <config>
        <fields name="params">
            <fieldset name="basic">
                <field
                    name="filter"
                    type="list"
                    label="PLG_FIELDS_TEXT_PARAMS_FILTER_LABEL"
                    description="PLG_FIELDS_TEXT_PARAMS_FILTER_DESC"
                    class="btn-group"
                    default="JComponentHelper::filterText"
                    validate="options"
                    >
                    <option value="0">JNO</option>
                    <option value="raw">JLIB_FILTER_PARAMS_RAW</option>
                    <option value="safehtml">JLIB_FILTER_PARAMS_SAFEHTML</option>
                    <option value="JComponentHelper::filterText">JLIB_FILTER_PARAMS_TEXT</option>
                    <option value="alnum">JLIB_FILTER_PARAMS_ALNUM</option>
                    <option value="integer">JLIB_FILTER_PARAMS_INTEGER</option>
                    <option value="float">JLIB_FILTER_PARAMS_FLOAT</option>
                    <option value="tel">JLIB_FILTER_PARAMS_TEL</option>
                    <option value="fb">MY CUSTOM FILTER OPTION</option>
                </field>

                <field
                    name="maxlength"
                    type="number"
                    label="PLG_FIELDS_TEXT_PARAMS_MAXLENGTH_LABEL"
                    description="PLG_FIELDS_TEXT_PARAMS_MAXLENGTH_DESC"
                    filter="integer"
                />
            </fieldset>
        </fields>
    </config>
</extension>

fb.php - (Standard stuff here copied from the text custom field plugin.)

<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Fields.Text
 *
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JLoader::import('components.com_fields.libraries.fieldsplugin', JPATH_ADMINISTRATOR);

/**
 * Fields Text Plugin
 *
 * @since  3.7.0
 */
class PlgFieldsFb extends FieldsPlugin
{
}
?>

/tmpl/fb.php

<?php
/**
 * @package     Joomla.Plugin
 * @subpackage  Fields.Text
 *
 * @copyright   Copyright (C) 2005 - 2020 Open Source Matters, Inc. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

$value = $field->value;

if ($value == '')
{
    return;
}

if (is_array($value))
{
    $value = implode(', ', $value);
}

echo htmlentities($value);

/params/fb.php - (Standard stuff here copied from the text custom field plugin, but I try to load a addrule path thinking that perhaps this how I can add my filter but I'm thinking this is wrong for what I'm trying to do.)

<?xml version="1.0" encoding="utf-8"?>
<form
addrulepath="/plugins/fields/fb/rules/fb.php"
>
    <fields name="fieldparams">
        <fieldset name="fieldparams">
            <field
                name="filter"
                type="list"
                label="PLG_FIELDS_TEXT_PARAMS_FILTER_LABEL"
                description="PLG_FIELDS_TEXT_PARAMS_FILTER_DESC"
                class="btn-group"
                validate="options"
                >
                <option value="">COM_FIELDS_FIELD_USE_GLOBAL</option>
                <option value="0">JNO</option>
                <option value="raw">JLIB_FILTER_PARAMS_RAW</option>
                <option value="safehtml">JLIB_FILTER_PARAMS_SAFEHTML</option>
                <option value="JComponentHelper::filterText">JLIB_FILTER_PARAMS_TEXT</option>
                <option value="alnum">JLIB_FILTER_PARAMS_ALNUM</option>
                <option value="integer">JLIB_FILTER_PARAMS_INTEGER</option>
                <option value="float">JLIB_FILTER_PARAMS_FLOAT</option>
                <option value="tel">JLIB_FILTER_PARAMS_TEL</option>
                <option value="fb">MY CUSTOM FILTER OPTION</option>
            </field>

            <field
                name="maxlength"
                type="number"
                label="PLG_FIELDS_TEXT_PARAMS_MAXLENGTH_LABEL"
                description="PLG_FIELDS_TEXT_PARAMS_MAXLENGTH_DESC"
                filter="integer"
            />
        </fieldset>
    </fields>
</form>

/rules/fb.php (Here is where I'm thinking that trying to add the rule might be the way? I think this is wrong, I want to add a Filter.. I don't think a rule is what I need.?)

<?php
/**
* @version        $Id: mobile.php 20196 2011-01-09 02:40:25Z ian $
* @package        Joomla.Framework
* @subpackage     Form
*/

defined('JPATH_BASE') or die;

jimport('joomla.form.formrule');

/**
* Form Rule class for the Joomla Framework.
*
* @package        Joomla.Framework
* @since          1.6
*/
class JFormRuleFb extends JFormRule
{
    /**
    * Method to test the username for uniqueness.
    *
    * @param    object    $element    The JXMLElement object representing the <field /> tag for the
    *                                 form field object.
    * @param    mixed     $value      The form field value to validate.
    * @param    string    $group      The field name group control value. This acts as as an array
    *                                 container for the field. For example if the field has name="foo"
    *                                 and the group value is set to "bar" then the full field name
    *                                 would end up being "bar[foo]".
    * @param    object    $input      An optional JRegistry object with the entire data set to validate
    *                                 against the entire form.
    * @param    object    $form       The form object for which the field is being tested.
    *
    * @return   boolean               True if the value is valid, false otherwise.
    * @since    1.6
    * @throws   JException on invalid rule.
    */
    public function test(& $element, $value, $group = null, & $input = null, & $form = null)
    {
        /* 
         * Here we match the value with a specific format. You may also use any kind of validation,
         * If you need a value of another field as well from the same form then use the following method:
         * $userId = ($input instanceof JRegistry) ? $input->get('id') : '0';
         * this gived you the value of the Id field
         */
        return preg_match("/^\+{0,1}[0-9]{6,14}$/",$value);
    }
}

Any suggestions on how to extend or add a custom "Filter" option to my Custom Field plugin or extend the Text Custom Field plugin to accept a "Custom" "Filter" type that I can customize even hard coded. I attached a screenshot of the custom field screen and the "Filter" options I'm referring to.enter image description here Thanks in advance!

Your Answer

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

Browse other questions tagged or ask your own question.