J4 - How do I get child categories field values??

26 views
Skip to first unread message

darrenf...@gmail.com

unread,
Oct 22, 2021, 4:01:50 AM10/22/21
to Joomla! General Development
I've got a template where I'm overriding the com_content template.

I want to be able to get the field values for the children.

I've read numerous things on the internet on how to get the values but none of them seem to be working...

firstly one suggestion I saw was that it was stored in jcfields - however jcfields is not set in the children.

I then found some old joomla 3 code that used jloader to load the fields.php helper - oddly enough jloader still worked and still actually loaded the fields helper - but I've changed that now to 

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

so that it's using the proper Joomla 4 format.  I then looked at how some of the other code is getting the field values - seems simple enough 

$fields = FieldsHelper::getFields('com_content.categories', $child->id, true);

I looked through github to see if I could find the definition of getFields (I also searched the joomla installation using visual studio code too) - the only reference I could find to getFields and it's code is in administrator/components/com_fields/helper/field.php - which just mention it's deprecated and to use joomla\component\fields\administrator\helper\fieldshelper - I also looked through the joomla api to find out what getFields actually does - again absolutely nothing in the api (I did find joomla\cms\fields in there but that wasn't much help - I take it as it's a component the code is stored in a different github?

Anyway I gathered that the variables needed for it are "type" (I gathered type for the categories is "com_component.categories" - I did initially try with category as I only want one category but that returned nothing - but categories seems to return something, "id" and true - not too sure what true/false does - I tried changed true to false and used print_r to see the results - both results looked identical so not too sure what the true/false bit does at the end - the only thing I have noticed is in all the code I've found that uses getFields - it's always set to true.

Anyway to test it fully I created a category in my site as a test category - it was given the category 8 and I added a text field to it called "test" - and put in it "this is a test" - so I'd certainly see if it returned the data.  I used

$fields = FieldsHelper::getFields('com_content.categories', 8, true);

(I replaced $child-Id with 8 to ensure I was only going to get the fields from category number 8 as a start to see what happened and I got an array of stdClass objects back - one of which was called "test") and this is what was returned

stdClass Object
(
    [id] => 4
    [title] => test
    [name] => test
    [checked_out] => 
    [checked_out_time] => 
    [note] => 
    [state] => 1
    [access] => 1
    [created_time] => 2021-10-22 06:51:16
    [created_user_id] => 239
    [ordering] => 0
    [language] => *
    [fieldparams] => Joomla\Registry\Registry Object
        (
            [data:protected] => stdClass Object
                (
                    [filter] => 
                    [maxlength] => 
                )

            [initialized:protected] => 1
            [separator] => .
        )

    [params] => Joomla\Registry\Registry Object
        (
            [data:protected] => stdClass Object
                (
                    [hint] => 
                    [class] => 
                    [label_class] => 
                    [show_on] => 
                    [render_class] => 
                    [showlabel] => 1
                    [label_render_class] => 
                    [display] => 2
                    [prefix] => 
                    [suffix] => 
                    [layout] => 
                    [display_readonly] => 2
                )

            [initialized:protected] => 1
            [separator] => .
        )

    [type] => text
    [default_value] => 
    [context] => com_content.categories
    [group_id] => 0
    [label] => test
    [description] => 
    [required] => 0
    [only_use_in_subform] => 0
    [language_title] => 
    [language_image] => 
    [editor] => 
    [access_level] => Public
    [author_name] => Darren
    [group_title] => 
    [group_access] => 
    [group_state] => 
    [group_note] => 
)

Ok so I feel I am kind of getting somewhere... however if I tried to get the value of this field stdClass object I got an error that value doesn't exist (which yeah of course looking at the print_r output I can see there is no "value" set in the stdClass object so yeah not surprising.  It seems to be getting me all the information about the field - except the value - what on earth am I doing wrong???  I'm giving it the type of field I'm wanting to get ("com_content.categories"), I'm giving it the ID of the category that I'm trying to get the fields for (8) - and yet I'm getting no values returned - just information about the field which is useless to me as I want to use the values!!!

Is there a different function I should be using to get the actual values??  Everything I've seen seems to suggest that just call getFields and it will return an array with a "value" and "rawvalue" element but this is just not doing this.

darrenf...@gmail.com

unread,
Oct 22, 2021, 5:29:07 AM10/22/21
to Joomla! General Development
ah after much frustration and playing around - I finally found where the function is hiding (for some reason GitHub doesn't seem to look in the src folder for the code.

Anyway I've figured out that I need to pass the id as and array -

$fields = FieldsHelper::getFields('com_content.categories', array ( 'id' => 8 ), true);

Yay!!!  Getting closer and closer to making a fully working J4 template!!
Reply all
Reply to author
Forward
0 new messages