bolt: [BUG] Content->getImage() method breaks for images with alt and no path
The function Content->getImage() takes the first field of type image and the returns either the image path if we have a file key or the whole thing if not (as fallback for bolt 1.5.0).
If a user now edits the alt title of an image but does not provide a image path, this check fails and returns the array [alt: „the alt“]. Since every part of bolt (including especially the twig template of the backend) assume it’s a string and not an array, this throws an exception, making some views of the backend unusable.
The method in question - ContentValuesTrait.php Line 503
// Grab the first field of type 'image', and return that.
foreach ($this->contenttype['fields'] as $key => $field) {
if ($field['type'] == 'image') {
// After v1.5.1 we store image data as an array
if (is_array($this->values[$key]) && isset($this->values[$key]['file'])) {
return $this->values[$key]['file'];
}
return $this->values[$key];
}
}
Example of a breaking template with a “Array to String Conversion”: __base/listing.twig
{% set thumb_title = __('Image') ~ ': ' ~ content.getImage %}
In my case a widget that uses the @bolt/_base/listing.twig block creating the thumbnails breaks, making the dashboard fail critically (Remember: we can’t catch exceptions in twig).
Proposed Fix - ContentValuesTrait.php Line 503
// Grab the first field of type 'image', and return that.
foreach ($this->contenttype['fields'] as $key => $field) {
if ($field['type'] == 'image') {
// After v1.5.1 we store image data as an array
if (is_array($this->values[$key])) {
return isset($this->values[$key]['file']) ? $this->values[$key]['file'] : "";
}
return $this->values[$key];
}
}
Details
- Relevant Bolt Version: 3.0.12
- Install type: Composer install
- PHP version: 5.5
- Used web server: Apache
- For UX/UI issues: Chrome
Reproduction
- Create a content type with a field of type image with alt attribute
- Enter an alt value and save
- Use the $record->getImage() method somewhere and observe returned file path
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 15 (15 by maintainers)
Commits related to this issue
- fix for images with alt attribute but no path - see #5900 — committed to ToBe998/bolt by ToBe998 8 years ago
- fix for images with alt attribute but no path - see #5900 — committed to bolt/bolt by ToBe998 8 years ago
- Merge branch 'release/3.2' into release/3.3 * release/3.2: Fix middleware parsing for arrays, strings not starting with '::', and for class names that have __invoke method. Re-add translation cac... — committed to CarsonF/bolt by CarsonF 8 years ago
- Merge branch 'release/3.3' into master * release/3.3: Fix middleware parsing for arrays, strings not starting with '::', and for class names that have __invoke method. Re-add translation cache ... — committed to bolt/bolt by CarsonF 8 years ago
Fixed in #5919