How to configure component so every product can read from it?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
Oen44
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 16, 2018 4:44 pm

How to configure component so every product can read from it?

Post by Oen44 » Tue Jan 16, 2018 10:34 pm

Hello,

I've made my first component based on https://docs.joomla.org/J3.x:Developing ... _Component. Everything works fine as I want. I have a list of products that can be added, edited etc. from control panel. I'm not displaying each product separately but all at the same time on single page. There is also range slider and I want clients to change values (like min, max) for that slider, using configuration from control panel. By following https://docs.joomla.org/J3.x:Developing ... figuration I know that every product has it's own 'params' value and changing something in global configuration won't affect products already created.
From here https://docs.joomla.org/J3.x:Developing ... ic_backend I got list of products to save and read from database. They got simple values like name, description, price and image (well, that's all actually). In frontend I'm reading all products at once and then display on the page.

Frontend: https://i.imgur.com/HLB0mlg.png
Backend: https://i.imgur.com/QXI0OGC.png
Configuration: https://i.imgur.com/6n80Bv4.png

How can I make that configuration so every product will read from it?
Last edited by imanickam on Thu Jan 18, 2018 4:05 am, edited 3 times in total.
Reason: Changed the subject to reflect the content of post + moved the topic from the forum General Questions/New to Joomla! 3.8 to the forum Joomla! 3.x Coding

Oen44
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 16, 2018 4:44 pm

Re: How to configure component so every product can read from it?

Post by Oen44 » Thu Jan 18, 2018 10:54 pm

I really don't want to write second post here but... I got new question that is related. Is there any way to read from components Global Configuration through PHP? Any method that will return array/object of settings? There must be something :(

sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: How to configure component so every product can read from it?

Post by sozzled » Thu Jan 18, 2018 11:02 pm

Oen44 wrote:I really don't want to write second post here but... I got new question that is related. Is there any way to read from components Global Configuration through PHP? Any method that will return array/object of settings? There must be something :(
I don't really understand any of the questions you've asked (both in your first post and in your second one).

When you ask if there's any way to "read from components Global Configuration" what do you mean? What, specifically, do you want to "read"? Perhaps you can give one or two examples of what you want to "read", please?

Oen44
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 16, 2018 4:44 pm

Re: How to configure component so every product can read from it?

Post by Oen44 » Thu Jan 18, 2018 11:07 pm

Just follow this documentation https://docs.joomla.org/J3.x:Developing ... figuration
That's how we add global configuration to our component, right? Right.
I have config.xml with inputs etc. and I want to get their values in frontend view. That's all.

sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: How to configure component so every product can read from it?

Post by sozzled » Thu Jan 18, 2018 11:28 pm

Are you asking how to write a .XML file or how to read the parameters (initial settings or updated settings as the case may be) within your extension?

The parameters are stored in an array. The parameters may be read as an array with

Code: Select all

	$this->setState('params', JFactory::getApplication()->getParams());
You can read each parameter value with PHP by writing $this->params->get('parameter_name', 'value_if_not_defined'). So, let's say we have a parameter named as "myField" in the .XML file and you want to use it; you could store the value in a variable like this:

Code: Select all

    $myveryownparameter = $this->params->get('myField', '1');
This will store the value of "myField" in the variable $myveryownparameter (if "myField" is defined) or it will store "1" (if "myField" has not been set anywhere).

Does this help?

Oen44
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 16, 2018 4:44 pm

Re: How to configure component so every product can read from it?

Post by Oen44 » Thu Jan 18, 2018 11:34 pm

Sorry but not what I expected. I need this to be configurable by site admin in his control panel.

Here is my admin/config.xml

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<config>
	<fieldset
		name="company"
		label="Firmy"
		description=""
	>
		<field
			name="company_min_years"
			type="number"
			label="Minimalna liczba lat wynajęcia"
			description="Najmniejsza liczba lat do wyboru na suwaku"
			default="1"
		/>
        <field
			name="company_max_years"
			type="number"
			label="Maksymalna liczba lat wynajęcia"
			description="Największa liczba lat do wyboru na suwaku"
			default="10"
		/>
	</fieldset>
	<fieldset
		name="private"
		label="Osoby Prywatne"
		description=""
	>
		<field
			name="private_min_years"
			type="number"
			label="Minimalna liczba lat wynajęcia"
			description="Najmniejsza liczba lat do wyboru na suwaku"
			default="1"
		/>
        <field
			name="private_max_years"
			type="number"
			label="Maksymalna liczba lat wynajęcia"
			description="Największa liczba lat do wyboru na suwaku"
			default="5"
		/>
	</fieldset>
</config>
And when I'm in http://localhost/joomla/administrator/i ... aquaoferta (com_aquaoferta is my component name) I can change those values and they are saved somewhere. Now I want to retrieve those values. Tried with what you provided but that's not it.

sozzled
I've been banned!
Posts: 13639
Joined: Sun Jul 05, 2009 3:30 am
Location: Canberra, Australia

Re: How to configure component so every product can read from it?

Post by sozzled » Thu Jan 18, 2018 11:52 pm

This XML file is wrong or incomplete. The XML file should look something like this:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<extension type="component" version="3.8.3" method="upgrade">

	<name>COM_SOMETHING</name>
	<!-- The following elements are optional and free of formatting constraints -->
	<creationDate>February 2015</creationDate>
	<author>John Doe</author>
	<authorEmail>[email protected]</authorEmail>
	<authorUrl>http://www.example.org</authorUrl>
	<copyright>Copyright Info</copyright>
	<license>License Info</license>
	<version>1.0.0</version>
	<!-- The description is optional and defaults to the name -->
	<description>COM_SOMETHING_DESCRIPTION</description>

	<files folder="site">
		<filename>index.html</filename>
		<filename>helloworld.php</filename>
		<filename>controller.php</filename>
		<folder>views</folder>
		<folder>models</folder>
	</files>

        <languages folder="site/language">
		<language tag="en-GB">en-GB/en-GB.com_something.ini</language>
        </languages>

	<media destination="com_something" folder="media">
		<filename>index.html</filename>
		<folder>images</folder>
	</media>

	<administration>
		<!-- Administration Menu Section -->
		<menu link='index.php?option=com_something' img="../media/com_something/images/tux-16x16.png">COM_SOMETHING_MENU</menu>
		<!-- Administration Main File Copy Section -->
		<!-- Note the folder attribute: This attribute describes the folder
			to copy FROM in the package to install therefore files copied
			in this section are copied from /admin/ in the package -->
		<files folder="admin">
			<!-- Admin Main File Copy Section -->
			<filename>index.html</filename>
			<filename>config.xml</filename>
			<filename>something.php</filename>
			<filename>controller.php</filename>
			<filename>access.xml</filename>
			<!-- SQL files section -->
			<folder>sql</folder>
			<!-- tables files section -->
			<folder>tables</folder>
			<!-- models files section -->
			<folder>models</folder>
			<!-- views files section -->
			<folder>views</folder>
			<!-- controllers files section -->
			<folder>controllers</folder>
			<!-- helpers files section -->
			<folder>helpers</folder>
		</files>
		<languages folder="admin/language">
        		<language tag="en-GB">en-GB/en-GB.com_something.ini</language>
                        <language tag="en-GB">en-GB/en-GB.com_something.sys.ini</language>
		</languages>
	</administration>
	
	<config>
   <fieldset
      name="company"
      label="Firmy"
      description=""
   >
      <field
         name="company_min_years"
         type="number"
         label="Minimalna liczba lat wynajęcia"
         description="Najmniejsza liczba lat do wyboru na suwaku"
         default="1"
      />
        <field
         name="company_max_years"
         type="number"
         label="Maksymalna liczba lat wynajęcia"
         description="Największa liczba lat do wyboru na suwaku"
         default="10"
      />
   </fieldset>
   <fieldset
      name="private"
      label="Osoby Prywatne"
      description=""
   >
      <field
         name="private_min_years"
         type="number"
         label="Minimalna liczba lat wynajęcia"
         description="Najmniejsza liczba lat do wyboru na suwaku"
         default="1"
      />
        <field
         name="private_max_years"
         type="number"
         label="Maksymalna liczba lat wynajęcia"
         description="Największa liczba lat do wyboru na suwaku"
         default="5"
      />
   </fieldset>
</config>

</extension>
You will have to change "something" to whatever you have named your component, of course.

Oen44
Joomla! Apprentice
Joomla! Apprentice
Posts: 10
Joined: Tue Jan 16, 2018 4:44 pm

Re: How to configure component so every product can read from it?

Post by Oen44 » Thu Jan 18, 2018 11:59 pm

You are talking about main component xml file. Totally different from what I'm talking about. Anyway, I found simple solution for my problem. Those configuration values are stored in #__extensions table under params column as JSON.

Code: Select all

		$query = $db->getQuery(true);
		$query->select('`params`')
				->from('#__extensions')
				->where('element=\'com_aquaoferta\'');
		$db->setQuery((string)$query);

		$tempParams = $db->loadObject();
		$params = new JRegistry;
		$params->loadString($tempParams->params, 'JSON');
		$this->items['params'] = $params;
That's all I had to do, honestly. Now I can finally finish my component. Why haven't I done this before? :eek:

Anyway, thanks for your help.


Locked

Return to “Joomla! 3.x Coding”