madeyourday/contao-rocksolid-custom-elements

2.4.7 Empty headline is still output in the template

Closed this issue · 4 comments

Contao 4.13.37 PHP8.2

Since 2.4.7 empty headlines are displayed in the frontend despite a query in the template.

2024-02-27_12h24_53
2024-02-27_12h23_27

I was debugging this for the past few minutes and this is because $this->headline is now of type Stringable and not empty in if-clauses anymore.

image

This is being caused in the last change:

$this->Template->headline = new class($this->Template->headline, $this->Template->hl) implements \Stringable
{
public ?string $text;
public ?string $tag_name;
public function __construct(?string $text, ?string $tag_name)
{
$this->text = $text;
$this->tag_name = $tag_name;
}
public function __toString(): string
{
return $this->text ?? '';
}

The question is if this should be fixed from your side @ausi or if every person using custom-elements would need to update their templates after updating to version 2.4.7.

We'd all either need to use <?php if ((string) $this->headline): ?> or there should be a workaround.

I consider this critical since updates would lead to something that may output empty headlines like here:

<div class="ce_rsce_text col-m-6 text block">
	<div class="inside">
		<div class="c_headline">
			<h1></h1>
		</div>
		<div class="c_text">…</div>
	</div>
</div>

A possible solution would be using the ParseTemplateListener as it only renders html templates

namespace App\EventListener;

use Contao\CoreBundle\DependencyInjection\Attribute\AsHook;
use Contao\Template;

#[AsHook('parseTemplate')]
class ParseTemplateListener
{
    public function __invoke(Template $template): void
    {
        // Cast stringable to string (See #166)
        $template->headline = (string) $template->headline;
    }
}
ausi commented

@Ainschy can you please try if the latest dev-master version fixes your issue?

Thanks it's look fine for me

ausi commented

Released as version 2.4.8

@Ainschy @zoglo thank you for the quick reporting/debugging/testing!