How do I display the custom field value in the Site Modules?

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

Moderators: ooffick, 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.
Windows Defender SmartScreen Issues <-- please read this if using Windows 10.
Post Reply
learnice
Joomla! Apprentice
Joomla! Apprentice
Posts: 7
Joined: Fri Apr 22, 2022 3:41 am

How do I display the custom field value in the Site Modules?

Post by learnice » Fri Sep 22, 2023 3:03 am

Hello,I have a problem, and need help.Thank you.

I haved create a custom category field called "CatIcon", and how do I display the custom field value of "CatIcon" when retrieving the article category list on the website homepage?

I want to output the values of the custom field "CatIcon" in the attachment of the 4 screenshot. The PHP file is: default-20230922-023726. php. Who can help me write the code for the field ivalue in the Site Modules.Thanks.
4.png
You do not have the required permissions to view the files attached to this post.
Last edited by toivo on Fri Sep 22, 2023 3:11 am, edited 1 time in total.
Reason: mod note: moved from 4.x General Questions

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1414
Joined: Tue Jun 30, 2020 12:17 pm

Re: How do I display the custom field value in the Site Modules?

Post by Pavel-ww » Tue Sep 26, 2023 3:32 pm

learnice wrote:
Fri Sep 22, 2023 3:03 am

I haved create a custom category field called "CatIcon", and how do I display the custom field value of "CatIcon" when retrieving the article category list on the website homepage?
Hi. The code on your screenshot does not look like the right override of Categories list Module [mod_articles_categories].

You always need to use two files.

default.php - This is the shell calling the list of categories.
1.jpg
--
default_items.php - This is a list of categories that are called in the shell.
2.jpg
--
For the output of category custom fields, you should edit default_items.php, by adding the following parts of the code to it:
3.jpg
--

Code: Select all

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;


<?php $customFields = FieldsHelper::getFields('com_content.categories', $item, true);
            foreach ($customFields as $customField) {
                $customFields[$customField->name] = $customField;
            } ?>
       <span><?php echo $customFields['my-field']->value ?></span>
You can replace <span>...</span> with a tag you need and place this line where you need it in layout.

If you want to rename the module php template you should rename both files (for example to custom.php and custom_items.php ) and also you should to change it in the code, in both files.
4.jpg
5.jpg
You do not have the required permissions to view the files attached to this post.
Last edited by Pavel-ww on Tue Sep 26, 2023 3:58 pm, edited 1 time in total.

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1414
Joined: Tue Jun 30, 2020 12:17 pm

Re: How do I display the custom field value in the Site Modules?

Post by Pavel-ww » Tue Sep 26, 2023 3:50 pm

Here is the result output
6.jpg
7.jpg
You do not have the required permissions to view the files attached to this post.

erick-b
Joomla! Explorer
Joomla! Explorer
Posts: 285
Joined: Tue Feb 17, 2015 10:25 am

Re: How do I display the custom field value in the Site Modules?

Post by erick-b » Wed Sep 27, 2023 8:30 am

Code: Select all

$getFields = FieldsHelper::getFields('com_content.categories', $this->category, true);
$fields = ArrayHelper::pivot($getFields , 'name');
echo $fields['CatIcon’']->rawvalue
;

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1414
Joined: Tue Jun 30, 2020 12:17 pm

Re: How do I display the custom field value in the Site Modules?

Post by Pavel-ww » Wed Sep 27, 2023 2:54 pm

erick-b wrote:
Wed Sep 27, 2023 8:30 am

Code: Select all

$getFields = FieldsHelper::getFields('com_content.categories', $this->category, true);
$fields = ArrayHelper::pivot($getFields , 'name');
echo $fields['CatIcon’']->rawvalue
;
Hi @erick-b. I think if you provide a solution, then it must be a worker. Otherwise, the forum will turn into a dump of garbage. Your solution does not work for two reasons.

1) It is necessary to connect ArrayHelper

2) $this->category does not work in the module. Instead should be used $item

We are talking about the Categories List Module here [mod_articles_categories]

erick-b
Joomla! Explorer
Joomla! Explorer
Posts: 285
Joined: Tue Feb 17, 2015 10:25 am

Re: How do I display the custom field value in the Site Modules?

Post by erick-b » Wed Sep 27, 2023 6:26 pm

Code: Select all

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Utilities\ArrayHelper;
Fields = FieldsHelper::getFields('com_content.categories', $this->category, true);
$fields = ArrayHelper::pivot($getFields , 'name');
echo $fields['CatIcon’']->rawvalue
no needs for that stupid foreach when you want only one field, and with pivot you get the fields by name

instead of $this->category use $item , $this->item ... or whatever is already present in your code

erick-b
Joomla! Explorer
Joomla! Explorer
Posts: 285
Joined: Tue Feb 17, 2015 10:25 am

Re: How do I display the custom field value in the Site Modules?

Post by erick-b » Wed Sep 27, 2023 6:42 pm

Pavel-ww wrote:
Wed Sep 27, 2023 2:54 pm
Hi @erick-b. I think if you provide a solution, then it must be a worker
I am not his mother , it's up to each one to think and search with a little help

User avatar
Pavel-ww
Joomla! Ace
Joomla! Ace
Posts: 1414
Joined: Tue Jun 30, 2020 12:17 pm

Re: How do I display the custom field value in the Site Modules?

Post by Pavel-ww » Thu Sep 28, 2023 7:27 am

erick-b wrote:
Wed Sep 27, 2023 6:26 pm

Code: Select all

use Joomla\Component\Fields\Administrator\Helper\FieldsHelper;
use Joomla\Utilities\ArrayHelper;
Fields = FieldsHelper::getFields('com_content.categories', $this->category, true);
$fields = ArrayHelper::pivot($getFields , 'name');
echo $fields['CatIcon’']->rawvalue
It is funny that if someone tries this code and is convinced that it does not work, you will lose a reputation, instead of self-affirmation (which is apparently your goal). ;)

That "stupid foreach" is an universal solution for all cases, if you have more than one field for example.

Did you intend to make typos so that this code does not work for a non -professional? Here people ask for help actually, and not measured skills. If you are not ready to be "his mother", then this forum is not for you (IMO).

erick-b
Joomla! Explorer
Joomla! Explorer
Posts: 285
Joined: Tue Feb 17, 2015 10:25 am

Re: How do I display the custom field value in the Site Modules?

Post by erick-b » Mon Oct 02, 2023 3:56 pm

Pavel-ww wrote:
Thu Sep 28, 2023 7:27 am
[ If you are not ready to be "his mother", then this forum is not for you (IMO).
you are funny :-)


Post Reply

Return to “Joomla! 4.x Coding”