[SOLVED] Get params from component

This forum is for general questions about extensions for Joomla! 2.5.

Moderators: pe7er, General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.
Locked
Lufti
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Mar 06, 2009 4:23 pm

[SOLVED] Get params from component

Post by Lufti » Thu Mar 03, 2011 5:19 pm

Hi people, :)

I'm having problems getting parameter of my 1.6 component. I've the following code:

Code: Select all

        $params =&JComponentHelper::getParams('com_myone');
        $imagepath = $params->get('imagepath');
A var_dump($params) prints:

Code: Select all

object(JRegistry)#88 (1) {
  ["data":protected]=>
  object(stdClass)#91 (1) {
    ["params"]=>
    object(stdClass)#96 (3) {
      ["imagepath"]=>
      string(6) "images"
      ["product_link_title"]=>
      string(38) "erfahren Sie mehr über dieses Bild"
      ["category_link_title"]=>
      string(48) "betrachten Sie weitere Produkte dieser Kategorie"
    }
  }
}
And a var_dump($imagepath) prints:

Code: Select all

NULL
What's wrong? Why can't I get the value of 'imagepath'?

Thanks a lot!
Lufti
Last edited by Lufti on Fri Mar 04, 2011 2:06 pm, edited 1 time in total.

User avatar
alikon
Joomla! Champion
Joomla! Champion
Posts: 5941
Joined: Fri Aug 19, 2005 10:46 am
Location: Roma
Contact:

Re: Get params from component

Post by alikon » Thu Mar 03, 2011 5:36 pm

try like this

Code: Select all

$app		= JFactory::getApplication();
$cparams = $app->getParams('com_yours');
$a=$cparams->get('yourparam');
Nicola Galgano
i know that i don't know
www.alikonweb.it

Lufti
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Mar 06, 2009 4:23 pm

Re: Get params from component

Post by Lufti » Thu Mar 03, 2011 5:50 pm

Thanks for your answer!

Your code leads to a "Fatal error: Call to undefined method JAdministrator::getParams()".

Any Idea?

User avatar
alikon
Joomla! Champion
Joomla! Champion
Posts: 5941
Joined: Fri Aug 19, 2005 10:46 am
Location: Roma
Contact:

Re: Get params from component

Post by alikon » Thu Mar 03, 2011 5:54 pm

are you in admin side of your component ??
Nicola Galgano
i know that i don't know
www.alikonweb.it

Lufti
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Mar 06, 2009 4:23 pm

Re: Get params from component

Post by Lufti » Thu Mar 03, 2011 10:46 pm

Yes, I am. :/

User avatar
alikon
Joomla! Champion
Joomla! Champion
Posts: 5941
Joined: Fri Aug 19, 2005 10:46 am
Location: Roma
Contact:

Re: Get params from component

Post by alikon » Fri Mar 04, 2011 10:53 am

please read this official docs
this is the example code

Code: Select all

  <?php
    $app = JFactory::getApplication();
 
    mycom_params =  & $app->getParams('com_content');
 
    echo $mycom_params->get('num_leading_articles');
 
    ?>

Nicola Galgano
i know that i don't know
www.alikonweb.it

Lufti
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Mar 06, 2009 4:23 pm

Re: Get params from component

Post by Lufti » Fri Mar 04, 2011 1:33 pm

Thanks for your help!

I've seen those lines before. They describe how to get the params on the frontend and not the backend. But I need it on the backend!

JSite (frontend) and JAdministrator (backend) both inherit from JApplication. If you call JFactory::getApplication() you will get an instance of one of these classes depending on the current context.

My context is the backend and the method returns an JAdministrator object. The problem is that JAdministrator contrary to JSite does not implement the method JSite::getParams(). This is why I get the error (Fatal error: Call to undefined method JAdministrator::getParams()...).

So this is the wrong way to get the params on the backend.

The wired thing is that with my code (from my first post) I get a params object which contains the value I want to get but a $params->get('imagepath') returns NULL. As written on the upgrade site you have posted this code should work but doesn't include the menu item. I think this is the point where we should watch for a solution.

Hope you or someone else can help me! :/
Thanks a lot!

Lufti

Lufti
Joomla! Apprentice
Joomla! Apprentice
Posts: 18
Joined: Fri Mar 06, 2009 4:23 pm

Re: Get params from component

Post by Lufti » Fri Mar 04, 2011 1:59 pm

I solved the problem on this way:

Code: Select all

		$params =& JComponentHelper::getParams('com_myone');
		$imagepath = $params->getValue('data.params.imagepath');
I had to specify the path. :/

Thanks again for you help!
Lufti
Last edited by Lufti on Fri Mar 04, 2011 2:21 pm, edited 1 time in total.

User avatar
alikon
Joomla! Champion
Joomla! Champion
Posts: 5941
Joined: Fri Aug 19, 2005 10:46 am
Location: Roma
Contact:

Re: [SOLVED] Get params from component

Post by alikon » Fri Mar 04, 2011 2:12 pm

well
i think this post will be usefull for other developer's
me too
Nicola Galgano
i know that i don't know
www.alikonweb.it

integr8er
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu May 05, 2011 6:16 pm

Re: [SOLVED] Get params from component

Post by integr8er » Fri Jun 24, 2011 9:26 pm

This was definitely useful. I was using all sorts of example code shown in re of 1.6 that showed the use of the new xml layout with fieldsets, fields and field tags and where it showed to use "basic" (ie. <fields name="basic">), was a part of the issue. When I saw your post - which gave me enough of a clue along with the var_dump output, I changed my code to use a path of data.params.myparameter and changed my config.xml file to dump the word "basic" in lieu of "params" and got working results. I still think this is not really the right way to get the parameters but it is now working and I have to move onward... There must be a proper way to do this and it would be nice to know what it is.

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<config>
	<fields addfieldpath="administrator/components/com_XXXXXXX"/>
	<fieldset name="params" label="Component Parameters" description="Component Parameters Description">
		<fields name="params">
			<field name="basedir"   type="text" size="40" default="images/EventMedia" label="Base Folder" description="The path within the webroot to the media base folder."/>
			<field name="catfile"   type="text" size="40" default="category_data.txt" label="Category Filename" description="The name to use for the category data file."/>
			<field name="eventfile" type="text" size="40" default="event_data.txt" label="Event Filename"    description="The name to use for the event data file." />
		</fields>
	</fieldset>
</config>
With the above xml file, in the front end component code, I can now get the value of the parameters like what follows:

Code: Select all

$app = JFactory::getApplication('site');
$params = &JComponentHelper::getParams('com_XXXXXX');
$baseDir = $params->getValue('data.params.basedir');
$catFile = $params->getValue('data.params.catfile');
$eventFile = $params->getValue('data.params.eventfile');

User avatar
fcoulter
Joomla! Ace
Joomla! Ace
Posts: 1685
Joined: Thu Sep 13, 2007 11:39 am
Location: UK
Contact:

Re: [SOLVED] Get params from component

Post by fcoulter » Mon Jul 18, 2011 6:08 pm

Many thanks for this solution, I was really tearing my hair out trying to get the parameters for my component. Even for the front end of the site the example code

Code: Select all

  <?php
    $app = JFactory::getApplication();

    mycom_params =  & $app->getParams('com_content');

    echo $mycom_params->get('num_leading_articles');

    ?>
doesn't work, it just returns an empty result.

Thankfully your method does work, and I have found that this code will also work in Joomla 1.5 so it does not cause any problems with backwards compatibility.
http://www.spiralscripts.co.uk for Joomla! extensions
http://www.fionacoulter.com/blog my personal website
Security Forum moderator :: VEL team member
"Wearing my tin foil hat with pride"

User avatar
fcoulter
Joomla! Ace
Joomla! Ace
Posts: 1685
Joined: Thu Sep 13, 2007 11:39 am
Location: UK
Contact:

Re: [SOLVED] Get params from component

Post by fcoulter » Wed Jul 20, 2011 12:51 pm

Actually I was being too optimistic about the backwards compatibility, you can use

Code: Select all

<?php
    $app = JFactory::getApplication();

    mycom_params =  & $app->getParams('com_content');

    echo $mycom_params->getValue('data.params.num_leading_articles');

    ?>
In 1.6, but this doesn't work for 1.5, it returns an empty result, you need to use

Code: Select all

<?php
    $app = JFactory::getApplication();

    mycom_params =  & $app->getParams('com_content');

    echo $mycom_params->get('num_leading_articles');

    ?>
http://www.spiralscripts.co.uk for Joomla! extensions
http://www.fionacoulter.com/blog my personal website
Security Forum moderator :: VEL team member
"Wearing my tin foil hat with pride"

User avatar
mediaguru
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 142
Joined: Mon Sep 19, 2005 3:56 am
Location: USA
Contact:

Re: [SOLVED] Get params from component

Post by mediaguru » Sun Aug 28, 2011 3:50 am

Code: Select all

$app = JFactory::getApplication('site');
$params = &JComponentHelper::getParams('com_XXXXXX');
$baseDir = $params->getValue('data.params.basedir');
$catFile = $params->getValue('data.params.catfile');
$eventFile = $params->getValue('data.params.eventfile');
Thank you thank you.

I've been busting my head against the wall for two days trying to convert a J1.5 component of mine to J1.6. NOWHERE in the 1.6 documentation did I find an example which had the "data.params.xxx" solution. This HELPED me a ton and now my component is close to being converted from 1.5 to 1.6. I hope converting it to 1.7 isn't as big of a PITA.

Thanks again.

Biomehaniker
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Tue Jul 11, 2006 8:17 pm

Re: [SOLVED] Get params from component

Post by Biomehaniker » Sat Jun 30, 2012 10:35 pm

Maybe useful for others as well is ... if the config.xml is deleted / didn´t exist you couldn´t get a result ;-).

I forgot to update my developer system with the latest version from my 2nd component, and I was unable to get the params for one hour now :-/.

Finally ... the config.xml was missing ...

:pop

User avatar
fcoulter
Joomla! Ace
Joomla! Ace
Posts: 1685
Joined: Thu Sep 13, 2007 11:39 am
Location: UK
Contact:

Re: [SOLVED] Get params from component

Post by fcoulter » Mon Jul 02, 2012 11:10 am

This problem seems to have been solved in Joomla 2.5, it was a bug that affected Joomla 1.6.

In Joomla 2.5 you can do the same as 1.5

Code: Select all

$params = &JComponentHelper::getParams('com_XXXXXX');
$paramValue = $params->get('paramValue','default');

If you use the second default parameter you should be able to get a value even if the parameter is not initialised - eg if you forgot the config.xml file, though of course you do want to include it.
http://www.spiralscripts.co.uk for Joomla! extensions
http://www.fionacoulter.com/blog my personal website
Security Forum moderator :: VEL team member
"Wearing my tin foil hat with pride"

AlohaYoga
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 104
Joined: Sat Jul 23, 2016 4:03 pm

Re: [SOLVED] Get params from component

Post by AlohaYoga » Tue Oct 17, 2017 11:37 pm

Is there a way to not include it in the component xml file? Can I create a template add-on xml?

EDIT: just saw this is in joomla 2.5. Does this still work in 3.8 +?


Locked

Return to “Extensions for Joomla! 2.5”