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
)
)
| Comment | File | Size | Author |
|---|---|---|---|
| #23 | array-to-string-3106808-23.patch | 988 bytes | arthur.baghdasar |
| #18 | array-to-string-3106808-18.patch | 1.26 KB | dzinkevich |
| #7 | array-to-string-3106808-7.patch | 1.06 KB | dalinian |
| #4 | array-to-string-3106808-4.patch | 1.05 KB | drewble |
Issue fork entity_embed-3106808
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
Comment #2
merilainen commentedComment #3
merilainen commentedThere 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.
Comment #4
drewble commentedI 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.Comment #5
stewestI testes Patch 4 https://www.drupal.org/project/entity_embed/issues/3106808#comment-13611520 and it works.
Comment #6
dalinian commentedPatch #4 causes embedded image styling to fail. It produces:
<div class='["align-right","embedded-entity"]instead of
<div class="align-right embedded-entity"Comment #7
dalinian commentedHere'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.
Comment #8
wjackson commentedPatch #7 resolve the issue on our end. Thank you!
Comment #10
jaydee1818 commentedPatch #7 also squashed the notification for me.
Comment #11
bramdriesenIs this still relevant?
Comment #12
agiraud commentedI still have the PHP notice with the current module version. The patch in #7 fixed it.
Comment #13
et.cetera commented+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.
Comment #14
jmcintyre commentedI'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?
Comment #15
bramdriesenThe fact 4 tests are failing?
Comment #16
jmcintyre commentedUh, sorry @BramDriesen... I obviously didn't read the actual test result.
Comment #17
bramdriesenNo 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)Comment #18
dzinkevich commentedRe-roll of #7 to work with entity_embed v 8.1.2
Comment #19
nelo_drup commentedWhen 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
Comment #22
arthur.baghdasar commentedComment #23
arthur.baghdasar commentedComment #24
arthur.baghdasar commentedThis is still failing some tests. Though the patch fixes the issue on our end.
Comment #25
nelo_drup commented#23 The patch array-to-string-3106808-23.patch works fine you don't see that error anymore, from the comment #19
Comment #27
ctlopez commentedI am marking this ready for review as the patch provided works and applies cleanly, and the tests now pass.
Comment #28
pflora commentedI 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.
Comment #29
bramdriesenHad a look through the code changes, looks good. RTBC +1
Comment #31
dave reidComment #33
dave reidMerged to 8.x-1.x and will release today. Thank you everyone!