nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
162 stars 68 forks source link

Aggregated data from matrix-field returns strange values #1415

Closed stefanfriedrich closed 3 months ago

stefanfriedrich commented 4 months ago

When we aggregate data from a matrix-field (richtext, redactor), the content that is shown in the frontend is not from the matrix-field, it seems to be craft-internal data from the entry. If no matrix-field is chosen for the content, everything works fine. If no matrix-field with text exists, only other matrix-fields that show images, etc. everything works fine. The broken content touches als "og:description" and "twitter:description" - all field that try to get the content from the same matrix-fields.

Screenshots

Bildschirmfoto 2024-02-13 um 18 01 37

Bildschirmfoto 2024-02-13 um 19 17 42 Bildschirmfoto 2024-02-13 um 19 11 51

Versions

khalwat commented 4 months ago

In our discussions, you mentioned that you were unable to replicate this with a different project -- which is making me think this is something specific to your project.

Do you have any steps to reproduce that you can give me so I can attempt to replicate the bug, and fix it, if it is one?

stefanfriedrich commented 4 months ago

Still checking if there is another project that's aggregating it's data in the same way. I'll come back to you as soon as I know. Maybe important: worked on craft 3, error showed up after update to 4.

khalwat commented 3 months ago

I've tried replicating this by setting up a Matrix block with a plain text field and a rich text field in it.

Then I set SEOmatic to pull the description from that Matrix field, and everything worked as expected. It extracted the text from the Matrix fields, and used them for the description.

Can you help me out here with what could possibly be different with your setup for this site?

Are you seeing this behavior on any other sites you're using SEOmatic on?

khalwat commented 3 months ago

So I think I'm going to need a composer.json and db dump if I'm to attempt to diagnose this.

That or we do a video conf call:

https://savvycal.com/nystudio107/chat

khalwat commented 3 months ago

okay yeah so likely something you're doing in your templates is affecting it, because I get the correct result using a generic index.twig:

<title>🚧 Home | Rotax Aircraft Engines</title><meta name="generator" content="SEOmatic">
<meta name="keywords" content="technology, create, finding, continue, pushes, aviation, innovation, we’re, bursting, ever-better, built, boundaries, aircraft, engine, performance">
<meta name="description" content="We have built – and continue to create – technology that pushes the boundaries of aircraft engine performance to take the piloting experience to the next…">
<meta name="referrer" content="no-referrer-when-downgrade">

So if you could send me the templates/ directory on Discord, I could be able to figure it out pretty easily.

khalwat commented 3 months ago

Okay so it ends up being that the issue is that in this case, the Matrix blocks were being eager loaded in the templates via something like this:

{% do craft.app.elements.eagerLoadElements(
    className(entry),
    [entry],
    ['groupedEntryIntro']
) %}

...and then in the helper/Text.php class, it failed the is_array() test that was there because as of Craft 4, eager loaded elements return a Collection object.

khalwat commented 3 months ago

Fixed in 4.0.45 -> https://github.com/nystudio107/craft-seomatic/releases/tag/4.0.45

martinhellwagner commented 2 months ago

Just out of curiosity, is this also an issue in SEOmatic 3.x.x, or just on 4.x.x and 5.x.x?

khalwat commented 2 months ago

Just 4.x and 5.x, it had to do with collections

martinhellwagner commented 2 months ago

Got it, thanks!