XF 2.2 PHP 8.0.3 Template error: [E_WARNING] Array to string conversion

Masetrix

Well-known member
Whenever I activate PHP 8.0.3 I get the following error:

PHP:
    ErrorException: Template error: [E_WARNING] Array to string conversion src/XF/Str/Formatter.php:51

    Generiert von: Unbekanntes Konto 11 März 2021 um 19:28

Stack-Trace

#0 [internal function]: XF\Template\Templater->handleTemplateError(2, '[E_WARNING] Arr...', '/var/www/vhosts...', 51)
#1 src/XF/Str/Formatter.php(51): preg_replace(Array, Array, Array)
#2 src/XF/Template/Templater.php(4646): XF\Str\Formatter->censorText(Array, NULL)
#3 [internal function]: XF\Template\Templater->filterCensor(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, false)
#4 src/XF/Template/Templater.php(1056): call_user_func_array(Array, Array)
#5 internal_data/code_cache/templates/l2/s1/public/andy_calendar_events.php(30): XF\Template\Templater->filter(Array, Array, false)
#6 src/XF/Template/Templater.php(1626): XF\Template\Templater->{closure}(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, NULL)
#7 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(52): XF\Template\Templater->renderTemplate('andy_calendar_e...', Array, true, NULL)
#8 src/XF/Widget/WidgetRenderer.php(62): MaZ\AMP\XF\Template\XF22\Templater->renderTemplate('public:andy_cal...', Array)
#9 src/XF/Widget/WidgetRenderer.php(67): XF\Widget\WidgetRenderer->render()
#10 src/XF/Template/Templater.php(1901): XF\Widget\WidgetRenderer->__toString()
#11 internal_data/code_cache/templates/l2/s1/public/whats_new.php(16): XF\Template\Templater->widgetPosition('whats_new_overv...', Array)
#12 src/XF/Template/Templater.php(1626): XF\Template\Templater->{closure}(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, NULL)
#13 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(52): XF\Template\Templater->renderTemplate('whats_new', Array, true, NULL)
#14 src/XF/Template/Template.php(24): MaZ\AMP\XF\Template\XF22\Templater->renderTemplate('public:whats_ne...', Array)
#15 src/XF/Mvc/Renderer/Html.php(48): XF\Template\Template->render()
#16 src/XF/Mvc/Dispatcher.php(458): XF\Mvc\Renderer\Html->renderView('XF:WhatsNew\\Ove...', 'public:whats_ne...', Array)
#17 src/XF/Mvc/Dispatcher.php(440): XF\Mvc\Dispatcher->renderView(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#18 src/XF/Mvc/Dispatcher.php(400): XF\Mvc\Dispatcher->renderReply(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#19 src/XF/Mvc/Dispatcher.php(58): XF\Mvc\Dispatcher->render(Object(XF\Mvc\Reply\View), 'html')
#20 src/XF/App.php(2326): XF\Mvc\Dispatcher->run()
#21 src/XF.php(488): XF\App->run()
#22 index.php(20): XF::runApp('XF\\Pub\\App')
#23 {main}

However, it is not clear to me whether this is a bug in XF 2.2.4 or one (or all) of the add-ons involved here, or just a cache error.

Any hints?
 
Solution
Thanks.

Yeah that's pretty much what I translated the compiled template back to in my head. This is the problematic line:

Code:
<a href="{{ link('threads/', $thread|censor) }}">

I'm not totally clear what the author expects to be happening here but |censor doesn't work on an array ($thread).

That would be a fairly clear bug for the author to fix. Honestly I don't think we'd be making any changes here necessarily. PHP 8.0 now exposes this kind of issue as an E_WARNING rather than an E_NOTICE. We suppress notices but not warnings. This is a genuine bug in the add-on code and PHP exposing it with a higher severity may even be a benefit to developers if they are using/testing with PHP 8.0.

But should be an easy fix, anyway.
PHP 8 is okay to use now with the latest release - I use it on my local.

Third party add-ons may not work - that would have to be confirmed with the authors.
 
I didnt think we were supposed to be using php8 yet on current release?
We're not really recommending it or not recommending it. It should be fine but there have been a few bugs recently. Once we've had a release cycle or two without any PHP 8.0 bugs we may start to recommend it.

Whenever I activate PHP 8.0.3 I get the following error:

PHP:
    ErrorException: Template error: [E_WARNING] Array to string conversion src/XF/Str/Formatter.php:51

    Generiert von: Unbekanntes Konto 11 März 2021 um 19:28

Stack-Trace

#0 [internal function]: XF\Template\Templater->handleTemplateError(2, '[E_WARNING] Arr...', '/var/www/vhosts...', 51)
#1 src/XF/Str/Formatter.php(51): preg_replace(Array, Array, Array)
#2 src/XF/Template/Templater.php(4646): XF\Str\Formatter->censorText(Array, NULL)
#3 [internal function]: XF\Template\Templater->filterCensor(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, false)
#4 src/XF/Template/Templater.php(1056): call_user_func_array(Array, Array)
#5 internal_data/code_cache/templates/l2/s1/public/andy_calendar_events.php(30): XF\Template\Templater->filter(Array, Array, false)
#6 src/XF/Template/Templater.php(1626): XF\Template\Templater->{closure}(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, NULL)
#7 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(52): XF\Template\Templater->renderTemplate('andy_calendar_e...', Array, true, NULL)
#8 src/XF/Widget/WidgetRenderer.php(62): MaZ\AMP\XF\Template\XF22\Templater->renderTemplate('public:andy_cal...', Array)
#9 src/XF/Widget/WidgetRenderer.php(67): XF\Widget\WidgetRenderer->render()
#10 src/XF/Template/Templater.php(1901): XF\Widget\WidgetRenderer->__toString()
#11 internal_data/code_cache/templates/l2/s1/public/whats_new.php(16): XF\Template\Templater->widgetPosition('whats_new_overv...', Array)
#12 src/XF/Template/Templater.php(1626): XF\Template\Templater->{closure}(Object(MaZ\AMP\XF\Template\XF22\Templater), Array, NULL)
#13 src/addons/MaZ/AMP/XF/Template/XF22/Templater.php(52): XF\Template\Templater->renderTemplate('whats_new', Array, true, NULL)
#14 src/XF/Template/Template.php(24): MaZ\AMP\XF\Template\XF22\Templater->renderTemplate('public:whats_ne...', Array)
#15 src/XF/Mvc/Renderer/Html.php(48): XF\Template\Template->render()
#16 src/XF/Mvc/Dispatcher.php(458): XF\Mvc\Renderer\Html->renderView('XF:WhatsNew\\Ove...', 'public:whats_ne...', Array)
#17 src/XF/Mvc/Dispatcher.php(440): XF\Mvc\Dispatcher->renderView(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#18 src/XF/Mvc/Dispatcher.php(400): XF\Mvc\Dispatcher->renderReply(Object(XF\Mvc\Renderer\Html), Object(XF\Mvc\Reply\View))
#19 src/XF/Mvc/Dispatcher.php(58): XF\Mvc\Dispatcher->render(Object(XF\Mvc\Reply\View), 'html')
#20 src/XF/App.php(2326): XF\Mvc\Dispatcher->run()
#21 src/XF.php(488): XF\App->run()
#22 index.php(20): XF::runApp('XF\\Pub\\App')
#23 {main}

However, it is not clear to me whether this is a bug in XF 2.2.4 or one (or all) of the add-ons involved here, or just a cache error.

Any hints?

This seems to be related to a specific add-on template: XF\Template\Templater->renderTemplate('andy_calendar_e...', Array, true, NULL)

It may be interesting to understand exactly what the contents of the compiled template are if you're able to share the contents of the file: internal_data/code_cache/templates/l2/s1/public/andy_calendar_events.php but in all likelihood it's probably a bug in that template and may not even by PHP 8.0 specific.
 
Ok, here it is:
internal_data/code_cache/templates/l2/s1/public/andy_calendar_events.php

PHP:
<?php
// FROM HASH: 6cc83e2d388f1f286d8d3d1f5cd4953b
return array(
'code' => function($__templater, array $__vars, $__extensions = null)
{
    $__finalCompiled = '';
    if (!$__templater->test($__vars['calendarEvents'], 'empty', array())) {
        $__finalCompiled .= '
    
    ';
        $__templater->includeCss('andy_calendar_events.less');
        $__finalCompiled .= '
    
    <div class="block" ' . $__templater->func('widget_data', array($__vars['widget'], ), true) . '>
        <div class="block-container">
            <h3 class="block-minorHeader"><a href="' . $__templater->func('link', array('calendar', ), true) . '">' . '📅 Anstehende Termine...' . '</a></h3>
            <ul class="block-body">
                ';
        if ($__templater->isTraversable($__vars['calendarEvents'])) {
            foreach ($__vars['calendarEvents'] AS $__vars['thread']) {
                $__finalCompiled .= '
                    <li class="block-row">
                        <div class="contentRow">
                            <div class="contentRow-figure calendarevents-date-container">
                                <div class=\'calendarevents-date-container-month\'>' . $__templater->escape($__vars['thread']['monthShort']) . '</div>
                                <div class=\'calendarevents-date-container-day\'>' . $__templater->escape($__vars['thread']['dayShort']) . '</div>
                            </div>
                            <div class="contentRow-main contentRow-main--close">
                                <span class="calendarevents-thread-title">
                                    <a href="' . $__templater->func('link', array('threads/', $__templater->filter($__vars['thread'], array(array('censor', array()),), false), ), true) . '">
                                        ' . $__templater->func('prefix', array('thread', $__vars['thread']['prefix_id'], ), true) . $__templater->filter($__vars['thread']['title'], array(array('censor', array()),), true) . '
                                    </a>
                                </span>
                                <div class="contentRow-minor contentRow-minor--hideLinks">
                                    <span class="calendarevents-forum-title">
                                        <a href="' . $__templater->func('link', array('forums/', $__vars['thread']['forum'], ), true) . '">' . $__templater->escape($__vars['thread']['forum']['title']) . '</a>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </li>
                ';
            }
        }
        $__finalCompiled .= '
            </ul>
        </div>
    </div>

';
    }
    return $__finalCompiled;
}
);
 
This may require changes in the add-on but it does reflect a change in PHP 8.0 that we may also need to workaround.

I think I can see what has caused the error, but I'd be interested in seeing the andy_calendar_events template from the Admin CP to verify.
 
Here we are :)

PHP:
<xf:if is="$calendarEvents is not empty">
    
    <xf:css src="andy_calendar_events.less" />
    
    <div class="block" {{ widget_data($widget) }}>
        <div class="block-container">
            <h3 class="block-minorHeader"><a href="{{ link('calendar') }}">{{ phrase('calendar_calendar_events') }}</a></h3>
            <ul class="block-body">
                <xf:foreach loop="$calendarEvents" value="$thread">
                    <li class="block-row">
                        <div class="contentRow">
                            <div class="contentRow-figure calendarevents-date-container">
                                <div class='calendarevents-date-container-month'>{$thread.monthShort}</div>
                                <div class='calendarevents-date-container-day'>{$thread.dayShort}</div>
                            </div>
                            <div class="contentRow-main contentRow-main--close">
                                <span class="calendarevents-thread-title">
                                    <a href="{{ link('threads/', $thread|censor) }}">
                                        {{ prefix('thread', $thread.prefix_id) }}{$thread.title|censor}
                                    </a>
                                </span>
                                <div class="contentRow-minor contentRow-minor--hideLinks">
                                    <span class="calendarevents-forum-title">
                                        <a href="{{ link('forums/', $thread.forum) }}">{$thread.forum.title}</a>
                                    </span>
                                </div>
                            </div>
                        </div>
                    </li>
                </xf:foreach>
            </ul>
        </div>
    </div>

</xf:if>

and also the .less template

PHP:
// calendar events

.calendarevents-date-container {
    text-align: center;
    font-size: @xf-fontSizeSmallest;
    min-width: 30px;
}

.calendarevents-date-container-month {
    color: contrast(@xf-textColorFeature);
    background: @xf-textColorFeature;
    padding: 2px 5px 2px 5px;
    border-top-left-radius: @xf-borderRadiusSmall;
    border-top-right-radius: @xf-borderRadiusSmall;
}

.calendarevents-date-container-day {
    color: @xf-textColorFeature;
    background: @xf-contentHighlightBg;
    padding: 3px 5px 3px 5px;
    border: 1px solid @xf-borderColorHighlight;
    border-top-width: 0px;
    border-bottom-width: 2px;
    border-bottom-left-radius: @xf-borderRadiusSmall;
    border-bottom-right-radius: @xf-borderRadiusSmall;
}
 
Thanks.

Yeah that's pretty much what I translated the compiled template back to in my head. This is the problematic line:

Code:
<a href="{{ link('threads/', $thread|censor) }}">

I'm not totally clear what the author expects to be happening here but |censor doesn't work on an array ($thread).

That would be a fairly clear bug for the author to fix. Honestly I don't think we'd be making any changes here necessarily. PHP 8.0 now exposes this kind of issue as an E_WARNING rather than an E_NOTICE. We suppress notices but not warnings. This is a genuine bug in the add-on code and PHP exposing it with a higher severity may even be a benefit to developers if they are using/testing with PHP 8.0.

But should be an easy fix, anyway.
 
Solution
Top Bottom