fields
forms
Image
Odoo 19 fields — Image (views)
Live preview
Interactive
Source excerpt
web/static/src/views/fields/image/image_field.js
import { isMobileOS } from "@web/core/browser/feature_detection";
import { _t } from "@web/core/l10n/translation";
import { registry } from "@web/core/registry";
import { useService } from "@web/core/utils/hooks";
import { imageUrl } from "@web/core/utils/urls";
import { isBinarySize } from "@web/core/utils/binary";
import { FileUploader } from "../file_handler";
import { standardFieldProps } from "../standard_field_props";
import { Component, useState } from "@odoo/owl";
export const fileTypeMagicWordMap = {
"/": "jpg",
R: "gif",
i: "png",
P: "svg+xml",
U: "webp",
};
const placeholder = "/web/static/img/placeholder.png";
export class ImageField extends Component {
static template = "web.ImageField";
static components = {
FileUploader,
};
static props = {
...standardFieldProps,
alt: { type: String, optional: true },
enableZoom: { type: Boolean, optional: true },
imgClass: { type: String, optional: true },
zoomDelay: { type: Number, optional: true },
previewImage: { type: String, optional: true },
acceptedFileExtensions: { type: String, optional: true },
width: { type: Number, optional: true },
height: { type: Number, optional: true },
reload: { type: Boolean, optional: true },
convertToWebp: { type: Boolean, optional: true },
};
static defaultProps = {
acceptedFileExtensions: "image/*",
alt: _t("Binary file"),
imgClass: "",
reload: true,
};
setup() {
this.notification = useService("notification");
this.orm = useService("orm");
this.isMobile = isMobileOS();
this.state = useState({
isValid: true,
});
this.lastURL = undefined;
if (this.fieldType === "many2one" && !this.props.previewImage) {
throw new Error(
"ImageField: previewImage must be provided when set on a many2one field"
);
}
const field = this.props.record.fields[this.props.name];
this.isImageOnAnotherRecord = field.related?.includes(".") || this.fieldType === "many2one";
}
get imgAlt() {
if (this.fieldType === "many2one" && this.props.record.data[this.props.name]) {
return this.props.record.data[this.props.name].display_name;
}
return this.props.alt;
}
get imgClass() {
return ["img", "img-fluid"].concat(this.props.imgClass.split(" ")).join(" ");
}
get fieldType() {
return this.props.record.fields[this.props.name].type;
}
get rawCacheKey() {
if (this.isImageOnAnotherRecord) {
return null;
}
return this.props.record.data.write_date;
}
get sizeStyle() {
let style = "";
if (this.props.width) {
style += `max-width: ${this.props.width}px;`;
if (!this.props.height) {
style += `height: auto; max-height: 100%;`;
}
}
if (this.props.height) {
style += `max-height: ${this.props.height}px;`;
if (!this.props.width) {
style += `width: auto; max-width: 100%;`;
}
}
return style;
}
get hasTooltip() {
return this.props.enableZoom && this.props.record.data[this.props.name];
}
get tooltipAttributes() {
const fieldName = this.fieldType === "many2one" ? this.props.previewImage : this.props.name;
return {
template: "web.ImageZoomTooltip",
info: JSON.stringify({ url: this.getUrl(fieldName) }),
};
}
getUrl(imageFieldName) {
if (!this.props.reload && this.lastURL) {
return this.lastURL;
}
if (!this.props.record.data[this.props.name] || !this.state.isValid) {
return placeholder;
}
if (this.fieldType === "many2one") {
Registry / API
- Registry name
image- Category
fields- Module
web- Slug
image- Nav group
forms