There is an error when embedding a Media file into a text area:
Notice: Array to string conversion in Drupal\Core\Template\AttributeArray->__toString() (line 73 of core/lib/Drupal/Core/Template/AttributeArray.php).
I have debugged the $variables['attributes'] in template_preprocess_entity_embed_container() and there seems to be an array for data-entity-embed-display-settings item:

array
(
    [data-embed-button] => file_media_browser
    [data-entity-embed-display] => entity_reference:entity_reference_file_media_decs
    [data-entity-embed-display-settings] => array
    (
        [entity] => 
        [attributes] => array
            (
                [data-entity-type] => media
                [data-entity-uuid] => 61c33e8a-c950-44f0-aee3-3f1415cf51d5
                [data-langcode] => en
                [data-entity-embed-display] => entity_reference:entity_reference_file_media_decs
                [data-embed-button] => file_media_browser
            )

        [use_description_as_link_text] => 1
        [description] => 
    )

    [data-entity-type] => media
    [data-entity-uuid] => 61c33e8a-c950-44f0-aee3-3f1415cf51d5
    [data-langcode] => en
    [class] => array
    (
        [0] => embedded-entity
    )
)

The same values seem to be in the root level of the array already, so it seems they are not needed at all. In HTML the attribute is rendered as data-entity-embed-display-settings="Array 1".

This doesn't seem to happen for example for Media image, where the element is an empty array which also suggests that they are not needed:

array
(
    [data-align] => left
    [data-embed-button] => image_media_browser
    [data-entity-embed-display] => view_mode:media.embedded_media
    [data-entity-embed-display-settings] => array
        (
        )

    [data-entity-type] => media
    [data-entity-uuid] => 94627856-7a20-4f01-b4f7-bb7d2885b6e3
    [data-langcode] => en
    [class] => array
        (
            [0] => embedded-entity
        )
)
Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

mErilainen created an issue. See original summary.

merilainen’s picture

Issue summary: View changes
merilainen’s picture

There is a patch at https://www.drupal.org/project/entity_embed/issues/3069448#comment-13234764 which uses json_encode for the display settings array which fixes the error, but I don't think it's the right approach. Also breaks tests.

drewble’s picture

StatusFileSize
new1.05 KB

I was having the same issue. Determined that it was related to any values in the $variables['attributes'] array that was a non-JSON-encoded array. This patch ensures that all array children of $variables['attributes'] are JSON encoded.

stewest’s picture

dalinian’s picture

Patch #4 causes embedded image styling to fail. It produces:

<div class='["align-right","embedded-entity"]

instead of

<div class="align-right embedded-entity"

dalinian’s picture

StatusFileSize
new1.06 KB

Here's a work around for the problem of the class attribute conversion failing on embedded images. In this case, I'm just not applying json_encoding to any attribute key named 'class'. This works for the specific case of the class attribute. However, I suspect there are other attributes that get formatted incorrectly due to json encoding.

wjackson’s picture

Status: Active » Reviewed & tested by the community

Patch #7 resolve the issue on our end. Thank you!

Status: Reviewed & tested by the community » Needs work

The last submitted patch, 7: array-to-string-3106808-7.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

jaydee1818’s picture

Patch #7 also squashed the notification for me.

bramdriesen’s picture

Is this still relevant?

agiraud’s picture

I still have the PHP notice with the current module version. The patch in #7 fixed it.

et.cetera’s picture

+1

I have this notice because the svg_image module adds width and height as array in formatter settings

https://git.drupalcode.org/project/svg_image/-/blob/8.x-1.x/src/Plugin/F...

when using embed. This is also described here:

https://www.drupal.org/project/svg_image/issues/3116124

Patch #7 fixes the notice.

jmcintyre’s picture

I'm seeing this issue upon updating a site from Drupal 9.3.19 to 9.4.5. The patch in #7 resolved it in my local environment but I haven't deployed it upstream yet—is there a reason other than coding standards this hasn't been rolled into a release?

bramdriesen’s picture

The fact 4 tests are failing?

jmcintyre’s picture

Uh, sorry @BramDriesen... I obviously didn't read the actual test result.

bramdriesen’s picture

No problem :-) happy to merge this once the tests are passing again. (no idea why I said that, guess I mixed up issue queues 😅 I'm not even a maintainer of the entity_embed module)

dzinkevich’s picture

StatusFileSize
new1.26 KB

Re-roll of #7 to work with entity_embed v 8.1.2

nelo_drup’s picture

When using the patch everything works fine but the images uploaded previously are damaged but only when viewing them with the module
https://www.drupal.org/project/image_browser
and here you can see how the thumbnails are damaged
View error

arthur.baghdasar made their first commit to this issue’s fork.

arthur.baghdasar’s picture

StatusFileSize
new1.07 KB
arthur.baghdasar’s picture

StatusFileSize
new988 bytes
arthur.baghdasar’s picture

This is still failing some tests. Though the patch fixes the issue on our end.

nelo_drup’s picture

#23 The patch array-to-string-3106808-23.patch works fine you don't see that error anymore, from the comment #19

ctlopez made their first commit to this issue’s fork.

ctlopez’s picture

Status: Needs work » Needs review

I am marking this ready for review as the patch provided works and applies cleanly, and the tests now pass.

pflora’s picture

Status: Needs review » Reviewed & tested by the community

I was able to replicate the error posted in the IS, and after applying the changes made by the MR no errors were shown after embedding a Media file. I've also ran the tests and nothing seems to be broken. No new CS errors were added with the changes, so i think this is good to be moved to RTBC.

bramdriesen’s picture

Had a look through the code changes, looks good. RTBC +1

Dave Reid made their first commit to this issue’s fork.

dave reid’s picture

dave reid’s picture

Status: Reviewed & tested by the community » Fixed

Merged to 8.x-1.x and will release today. Thank you everyone!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.