How to get component parameters?

风流意气都作罢 提交于 2019-11-29 23:51:20
Lobo-X

There's a more simple way. First import Joomla Component Helper:

jimport('joomla.application.component.helper'); // not required in Joomla 3.x

And then retrieve any attribute you want like this:

$params = JComponentHelper::getParams('com_dashboard');
$dashboardID = $params->get('dashboardID');

Greetings.

Bernardo Siu
$app = JFactory::getApplication('site');
$componentParams = $app->getParams('com_example');
$param = $componentParams->get('paramName', defaultValue);
J.T. Blum

Similar to the answer provided by LoboX, I'd recommend using the component helper to get component parameters:

jimport('joomla.application.component.helper'); // Import component helper library
$params = JComponentHelper::getParams(JRequest::getVar('option')); // Get parameter helper (corrected 'JRquest' spelling)
$params->get('parameter_name'); // Get an individual parameter

The JRequest::getVar('option') returns your component's name with the com_ prefix. Aside from that, it looks like you're trying to mix a little bit of 1.5/1.6 syntax into your configuration file. If you haven't seen it yet, try reading through the 2.5 version of the documentation. I hope that helps!

It is simular to J.T. Blum answer, but in Joomla 3.x JRequest is depricated. It is another way to get the apllication's params.

    $app = JFactory::getApplication();
    $input = $app ->input;
    $componentName = $input ->get('option');
    $params = JComponentHelper::getParams($componentName);
    print_r($params);

I had a similar problem. I only got the data:protected result until I went to the configuration of my component and saved it. Though there were default values printed in the textfields Joomla wasn't able to read them before clicking on 'save'.

Helper Function to get Params Object in all Joomla Versions 1.5 / 2.5 /3.x

class myCompoHelper{

    public static function getParams($option)
    {

        if (version_compare(JVERSION, '1.5', 'g'))
        {
            $application = JFactory::getApplication();
            if ($application->isSite())
            {
                $params = $application->getParams($option);
            }
            else
            {
                jimport('joomla.application.component.helper');
                $params = JComponentHelper::getParams($option);
            }
        }
        else
        {
            jimport('joomla.application.component.helper');
            $params = JComponentHelper::getParams($option);
        }
        return $params;
    }

}

$params=myCompoHelper::getParams('com_mycomponent');
echo $params->get('myParamName',null);

Since version 3.1 Joomla is in process to deprecate all J classes, matter effect, version 4.0 will deprecate almost all J classes, the recommended way from now on to retrieve components param is either calling the entire namespace function:

Joomla\CMS\Component\ComponentHelper::getParams('com_dashboard')->get('dashboardID');

or, if you are working on a model, you can call use keyword in order to import the file and use the class down in the document, like

use Joomla\CMS\Component\ComponentHelper;

function myFunction() {
    $param = ComponentHelper::getParams('com_dashboard');
    $dashboardID = $param->get('dashboardID');
}

I had the same problem and the solution was this:

$input = JFactory::getApplication()->input;
$parametername = $input->getInt('parametername');
echo $parametername;

This is the name of a parameter in the default.xml in the views tmpl folder. here is the xml

<?xml version="1.0" encoding="utf-8"?>
<metadata>
    <layout title="Offers">
         <message>This is a component in order to display offers</message>
    </layout>
    <state>
        <name>name</name>
            <description>description</description>

        <url addpath="/administrator/components/com_component/elements">
            <param name="category_id" section="com_component" type="categoriesmenu"  value_field="category_name" label="COM_COMPONENT_CATEGORY_FIELD_SELECT_TITLE" description="COM_COMPONENT_CATEGORY_FIELD_SELECT_TITLE_DESC" />
        </url>
    </state>

    <fields name="request" >
        <fieldset name="request" addfieldpath="/administrator/components/com_component/models/fields">
            <field name="parametername" type="category"
                label="category"
                required="true"
                description="Perigrafh"
            />
        </fieldset>
    </fields>


</metadata>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!