Problem/Motivation
When viewing the SDC Component Library preview page, a PHP warning is triggered: "Array to string conversion" in component-preview.html.twig at line 237. This occurs when components have array values in their prop examples. The warning appears every time the component library page loads, creating a poor developer experience.
Steps to reproduce
1. Install sdc_component_library module
2. Create a component with props that have array examples (e.g., checklist_items: [array])
3. Create a .story.twig file for the component
4. Navigate to /sdc-component-library
5. Observe the "Array to string conversion" warning in the browser console/logs
Proposed resolution
Modify the code block generation in component-preview.html.twig to check if example values are iterable (arrays) before attempting string conversion. Use the json_encode filter for array values instead of direct string concatenation.
Change the props_string generation logic to:
- Check if value.examples[0] is iterable
- If iterable, use json_encode filter
- If not, use the value directly
Remaining tasks
- Review and test the patch
- Ensure it handles all data types correctly
- Verify no regressions in code preview display
User interface changes
None. The code preview display will remain the same, but array examples will be properly JSON-encoded in the generated code snippet.
API changes
None.
Data model changes
None.
Issue fork sdc_component_library-3589910
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 #3
d.fisher commentedI've just followed the steps to reproduce this and I'm not seeing any errors. Are you able to provide the component you are seeing the error with so I can investigate further?
Comment #4
d.fisher commentedComment #5
d.fisher commentedCan you also provide which version of Drupal core you are working with? Thank you.
Comment #6
jaydee1818 commentedSo this is an example of a component that triggers the issue:
Note that the prop "checklist_items" is of "type: array"
This happens on both 10.6 and 11.3.
Notification triggered:
The patch I've supplied resolves the issue.
Comment #7
d.fisher commentedThank you. I'll take a look using your component example. That looks similar to what I set up just now and didn't see any errors at all.
Comment #8
jaydee1818 commentedOk - it's actually the array structure of the examples that cause the issue:
Comment #9
d.fisher commentedI've never used the examples key so I'm not familiar with that the structure should be.
This is the latest metadata schema json:
https://git.drupalcode.org/project/drupal/-/raw/11.3.x/core/assets/schem...
It does appear that your array structures are correct.
So the issue still stands - but we now know it specifically relates to the examples key? Does that sound correct?
Comment #10
jaydee1818 commentedYes - if I remove the examples, we don’t see the warning. The patch I’ve submitted specifically targets the “examples” key.
Comment #11
d.fisher commentedExcellent. Ok thanks. Now I should be able to recreate the error and test the patch. Thanks for your patience. Just wanted to verify that I can see the same issue that you're having and that the patch fixes it. Leave it with me. I'll endeavour to pick this up Tuesday morning (we're on a long weekend here in the UK).