0

On a site for certain articles (and not others!!)
I have the following error message *
This worked in j3/php7.4. Perhaps a problem comes from switching to j5 / php8.1
What's strange is that it works on some items but not others (I'm looking for differences but I don't see any!)
What is possible is that my test at the top is not good because if the fields are filled, I never have this problem.

I am told to take the array but how to define it?
I don't understand that it worked under Joomla 3 and that it works half-way on certain Joomla!5 articles.

Here is the code to ~221:

<?php
// Si les champs "champ(pro) ET champ(type pro)" ne sont pas vides 
if ( ($this->item->jcfields[110]->value !== '') AND ($this->item->jcfields[111]->value !== '') AND ($this->item->jcfields[112]->value !== '') ): ; 
//if (!empty($this->item->jcfields[110]->value)): ;  
// initialise la table avant la boucle
$app = Factory::getApplication();
$factory = $app->bootComponent("com_content")->getMVCFactory();
$table = $factory->createTable("Article", "Table");
 
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 : </b>";
    $field_value = json_decode($this->item->jcfields[111]->rawvalue, true);
 
    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"];
 
                $table->load($article_id);
                $item_catid = $table->catid;
 
                $film_types_pro = implode(", ", $field_value["row0"]["field112"]);
 
                echo HTMLHelper::link(
                    Route::_("index.php?option=com_content&view=article&id={$article_id}&catid={$item_catid}"),
                    $titre_art . " ( " . $film_types_pro . " )"
                );
            }
        }
    }
    }
}
endif; 
?>

Stack trace:

    0 implode(): Argument #1 ($array) must be of type array, string given
Call Stack
#    Function    Location
1    ()    JROOT/templates/cassiopeia_alterweb/html/com_content/article/alterweb.info-ressources.php:221
2    implode()    JROOT/templates/cassiopeia_alterweb/html/com_content/article/alterweb.info-ressources.php:221
3    include()    JROOT/libraries/src/MVC/View/HtmlView.php:416
4    Joomla\CMS\MVC\View\HtmlView->loadTemplate()    JROOT/libraries/src/MVC/View/HtmlView.php:204
5    Joomla\CMS\MVC\View\HtmlView->display()    JROOT/components/com_content/src/View/Article/HtmlView.php:258
6    Joomla\Component\Content\Site\View\Article\HtmlView->display()    JROOT/libraries/src/Cache/Controller/ViewController.php:95
7    Joomla\CMS\Cache\Controller\ViewController->get()    JROOT/libraries/src/MVC/Controller/BaseController.php:692
8    Joomla\CMS\MVC\Controller\BaseController->display()    JROOT/components/com_content/src/Controller/DisplayController.php:122
9    Joomla\Component\Content\Site\Controller\DisplayController->display()    JROOT/libraries/src/MVC/Controller/BaseController.php:730
10    Joomla\CMS\MVC\Controller\BaseController->execute()    JROOT/libraries/src/Dispatcher/ComponentDispatcher.php:143
11    Joomla\CMS\Dispatcher\ComponentDispatcher->dispatch()    JROOT/components/com_content/src/Dispatcher/Dispatcher.php:56
12    Joomla\Component\Content\Site\Dispatcher\Dispatcher->dispatch()    JROOT/libraries/src/Component/ComponentHelper.php:361
13    Joomla\CMS\Component\ComponentHelper::renderComponent()    JROOT/libraries/src/Application/SiteApplication.php:218
14    Joomla\CMS\Application\SiteApplication->dispatch()    JROOT/libraries/src/Application/SiteApplication.php:261
15    Joomla\CMS\Application\SiteApplication->doExecute()    JROOT/libraries/src/Application/CMSApplication.php:306
16    Joomla\CMS\Application\CMSApplication->execute()    JROOT/includes/app.php:58
17    require_once()    JROOT/index.php:32​
2
  • How successful are you if you unconditionally cast the field112 value to an array? $film_types_pro = implode(", ", (array) $field_value["row0"]["field112"]); I might rescript your nested condition block this way: 3v4l.org/aZufi Commented May 21 at 23:00
  • Also, that trailing semicolon looks dodgy: AND ($this->item->jcfields[112]->value !== '') ): ; Commented May 21 at 23:02

1 Answer 1

0

Instead of checking if the data is not an empty string like:

if ($this->item->jcfields[110]->value !== '')

I recommend checking if the data is declared and not null:

if (isset($this->item->jcfields[110]->value)) {
4
  • Hello I'm doubling in French because it would actually be easier for me :-) Ok I'm going to change the test but 2 people on the French forum tell me that the implode must test an array. What would be this best variable? As this is a code that someone helped me to make for joomla 3/php 7 Can I show it here or on the fr forum to check that the code (~140 lines) is compatible with Joomla 5?
    – Hdcms
    Commented Apr 17 at 7:03
  • @Hdcms I don't use Joomla 4 (neither J5) . Yes, implode first arg is an array. As my first language was Pascal, I use the same algorithm for that. I don't understand your sentence " Quelle serait cette meilleure variable ? " => QUELLE VARIABLE ?? @++
    – Seb
    Commented Apr 17 at 17:02
  • This answer doesn't make sense. The error states that the data is a string. Therefore we can deduce that it is both "declared" and "not null". Commented May 21 at 22:51
  • Hello Thank you but in the meantime, another developer suggested to me: if (!empty($this->item->jcfields[58]->value)): ; What would be more durable php 9.x...?
    – Hdcms
    Commented May 28 at 13:27

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.