0

I get the value checkboxes:[film_rea], but I can't get the corresponding text [realisation].

<?php foreach ($this->item->jcfields as $field) {
 
    if ('films-pros' === $field->name && 'subform' === $field->type) { 
    echo "<br /><b><span><i class='fa fa-link fa-1x' aria-hidden='true'></i></span> Pro(s) en lien avec ce film : </b>"; 
    $field_value = json_decode($this->item->jcfields[111]->rawvalue, true);
    //echo '<pre>', var_dump($field_value), '</pre>'; // ok 
    $app = Factory::getApplication();
    $factory = $app->bootComponent('com_content')->getMVCFactory();    
    $table = $factory->createTable('Article', 'Table'); 
    $table->load($article_id);
    $item_catid = $table->catid;
    foreach ($field->subform_rows as $subform_row) {
        foreach ($subform_row as $subField) {    
            if ('film-pro-lie' === $subField->fieldname) 
        {
                    $titre_art = $subField->value;
            $article_id = $field_value['row0']['field110']; 
            //OK echo '<pre>', var_dump($article_id), '</pre>';$field_value['row0']['field112']
            
            //$film_type_pro = $field_value['row0']['field112'][0];
            // OKecho '<pre>', var_dump($film_type_pro), '</pre>';       
                    echo HTMLHelper::link(
                    Route::link("site", "index.php?option=com_content&view=article&id={$article_id}&catid={$item_catid}"),
                    $titre_art  ." ");
                    


                    foreach ($field->fieldparams->get('options') as $option)   {
            // check if the selected value(s) has the current option
            // if rawvalue is an array, there is 2 or more options - look inside the rawvalue as an array
            // if rawvalue is a string, there's only one option - comparitor is as a string

            if ((is_array($field->rawvalue) && in_array($option->value, $field->rawvalue)) || $field->rawvalue == $option->value)   {
                $selected[] = array(
                'name'  => $option->name,
                'value' => $option->value
                );
            }
                }
                // update the value with the selected items
                $item->jcfieldsnames[ $field->name ]['value'] = $selected;
                echo '<pre>', var_dump($item->jcfieldsnames[ $field->name ]['value']), '</pre>';                


                    
            }
 
        }
        
        
      
        
    }
 
    }
 
} 
?>

DEBUG

array(1) {
  ["row0"]=>
  array(2) {
    ["field110"]=>
    string(3) "119"
    ["field112"]=>
    array(1) {
      [0]=>
      string(8) "film_rea"
    }
  }
}
string(8) "film_rea"

enter image description here

1
  • Hi Well, I don't know if this is the right way to do it, but I've modified my initial code. I don't see any value appearing [echo '<pre>', var_dump($item->jcfieldsnames[ $field->name ]['value']), '</pre>'; ] I'm not sure where to put the code, nor the syntax? Can you help me again, as I'm a bit lost in loops and I'm not a developer. I've spent days making progress and I'm just looking for a clean finish.
    – Hdcms
    Jun 21, 2023 at 13:35

2 Answers 2

2

The Labels for the subforms are stored in the fieldsparams column of the #__fields table and therefore you need to do extra processing to extract the labels for each sub field and match them up with your value.

This extract of code taken from Marty's handy little plugin for working with custom fields shows the processing that needs to happen to get the label(s). The full article can be found here, https://www.martyfriedel.com/blog/how-to-easily-access-to-custom-fields-in-joomla

if ($field->type == 'checkboxes')

{

    // create an array for our selected items

    $selected = [];

 

    // loop through all of the options

    foreach ($field->fieldparams->get('options') as $option)

    {

        // check if the selected value(s) has the current option

        // if rawvalue is an array, there is 2 or more options - look inside the rawvalue as an array

        // if rawvalue is a string, there's only one option - comparitor is as a string

        if ((is_array($field->rawvalue) && in_array($option->value, $field->rawvalue)) || $field->rawvalue == $option->value)

        {

            $selected[] = array(

                'name'  => $option->name,

                'value' => $option->value

            );

        }

    }

    // update the value with the selected items

    $item->jcfieldsnames[ $field->name ]['value'] = $selected;

} 

You will still need to adapt the above to suit your needs that works within your processing or just use the lines that the get the property(ies) that you need.

Updated: 24/06/20223

to address the comments made in the original question and this answer I would say you have done okay for a non-developer in working your way through the code and the questions you have been asking on JSE to get to stage you are at.

JSE is about helping you help yourself so I wouldn't normally say that you might consider engaging a local developer to finish your solution off for you, particularly when you have got this far, but some times you have consider the cost of paying someone else vs the time you have spent and wasted so far.

If you do want to continue then my advice would be to use the link above to the plugin Marty has written and install/enable that plugin on your Joomla environment where it will do all the work that you have been trying to do to extract the all the properties and values from the Custom Fields and present them to your template in an easier to use form.

Or to put it another way the Plugin already does what you are trying do in your code by extracting the custom fields and putting them in the $item object ready for you to use. As far as getting the values you are try to reinvent the wheel when the plugin has done this for you.

Your code should than be no more than filtering the properties and presenting the links as you desire.

2
  • See my change above. I also have a doubt about the method of the article which dates from 2019. It seems to me that this has changed for subforms with joomla 4
    – Hdcms
    Jun 22, 2023 at 7:38
  • I have updated my answer.
    – Irata
    Jun 23, 2023 at 23:21
1

This isn't an answer to your question but an improvement to your code that will make it more efficient.

Move the following lines to the start of your file before you begin to your Foreach loops as you only need to get the app and create the Table object once in your processing.

...
$app = Factory::getApplication();
$factory = $app->bootComponent('com_content')->getMVCFactory(); 
$table = $factory->createTable('Article', 'Table'); 

foreach ($this->item->jcfields as $field) {
....
2
  • Ok done; Thanks
    – Hdcms
    Jun 21, 2023 at 13:36
  • Hi Desperate appeal I did not understand the proposed solution :-( I spent more than a day to get to the code almost finished (for joomla 4) and that I can understand. I am not a developer and it is for a project of a non-profit association (so without money). Can someone help me finalize ? Regards
    – Hdcms
    Jun 26, 2023 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.