Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Feat/images alt (#13056)
Filip Cichorek2a5d5e1b51a0
diff --git a/admin/app/views/spree/admin/page_blocks/forms/_image.html.erb b/admin/app/views/spree/admin/page_blocks/forms/_image.html.erbindex 2e9b43dd25..ac4d4ad587 100644--- a/admin/app/views/spree/admin/page_blocks/forms/_image.html.erb+++ b/admin/app/views/spree/admin/page_blocks/forms/_image.html.erb@@ -1,5 +1,8 @@<%= render 'active_storage/upload_form', form: f, field_name: :asset, width: 512, height: 512, auto_submit: true %>-+<div class="mt-2 form-group">+ <%= f.label :preferred_image_alt, Spree.t(:alt_text) %>+ <%= f.text_field :preferred_image_alt, data: { action: 'auto-submit#submit' }, class: 'form-control' %>+</div><% content_for(:design_tab) do %><%= render 'spree/admin/page_builder/labeled_range_input',f: f, field: :preferred_height, min: 1, max: 1024, unit: 'px', _label: 'Max image height on desktop'diff --git a/admin/app/views/spree/admin/page_sections/forms/_image_with_text.html.erb b/admin/app/views/spree/admin/page_sections/forms/_image_with_text.html.erbindex 64da5531f4..eaed91d66a 100644--- a/admin/app/views/spree/admin/page_sections/forms/_image_with_text.html.erb+++ b/admin/app/views/spree/admin/page_sections/forms/_image_with_text.html.erb@@ -1,5 +1,4 @@<%= render 'spree/admin/shared/page_section_image', f: f %>-<div class="py-2"><%= f.fields_for :link do |lf| %><div class="form-group">@@ -9,15 +8,13 @@<%= render 'spree/admin/page_links/linkable_type_dropdown', page_link: lf.object, form_name: 'page_section[link_attributes]' %></div></div>-- <div class="custom-control custom-checkbox pb-2">+ <div class="pb-2 custom-control custom-checkbox"><%= lf.check_box :open_in_new_tab, class: 'custom-control-input', data: { action: 'auto-submit#submit' } %><%= lf.label :open_in_new_tab, class: 'custom-control-label' %>- <span class="text-muted form-text mt-2">Clicking on the image will send the user to the selected page/URL</span>+ <span class="mt-2 text-muted form-text"><%= Spree.t('admin.page_builder.image_click_hint') %></span></div><% end %></div>-<% content_for(:design_tab) do %><div class="form-group"><%= f.label :preferred_desktop_image_alignment, Spree.t('admin.page_builder.desktop_image_alignment') %>diff --git a/admin/app/views/spree/admin/shared/_page_section_image.html.erb b/admin/app/views/spree/admin/shared/_page_section_image.html.erbindex 93207b333b..233f59a8c4 100644--- a/admin/app/views/spree/admin/shared/_page_section_image.html.erb+++ b/admin/app/views/spree/admin/shared/_page_section_image.html.erb@@ -1 +1,7 @@<%= render 'active_storage/upload_form', form: f, field_name: :asset, width: 400, height: 128, auto_submit: true %>+<% if f.object.has_preference?(:image_alt) %>+ <div class="form-group">+ <%= f.label :preferred_image_alt, Spree.t(:alt_text) %>+ <%= f.text_field :preferred_image_alt, data: { action: 'auto-submit#submit' }, class: 'form-control' %>+ </div>+<% end %>diff --git a/admin/config/locales/en.yml b/admin/config/locales/en.ymlindex edb80667cf..6a27061ee6 100644--- a/admin/config/locales/en.yml+++ b/admin/config/locales/en.yml@@ -149,6 +149,7 @@ en:heading_alignment: Heading alignmentheading_size: Heading sizeheight: Height+ image_click_hint: Clicking on the image will send the user to the selected page/URLleft_aligned: Left alignedlogo_centered: Logo centeredmax_video_width_on_desktop: Max video width on desktopdiff --git a/core/app/models/concerns/spree/has_image_alt_text.rb b/core/app/models/concerns/spree/has_image_alt_text.rbnew file mode 100644index 0000000000..aea16ff020--- /dev/null+++ b/core/app/models/concerns/spree/has_image_alt_text.rb@@ -0,0 +1,18 @@+module Spree+ module HasImageAltText+ extend ActiveSupport::Concern++ # @return [String] Alt text resolved from preference, asset filename, or i18n fallback.+ def image_alt+ return preferred_image_alt if respond_to?(:preferred_image_alt) && preferred_image_alt.present?++ if respond_to?(:asset) && asset&.filename.present?+ filename = asset.filename.to_s+ base = File.basename(filename, File.extname(filename))+ return base.tr('-_.', ' ').squeeze(' ').strip+ end++ Spree.t(:image, default: 'Image')+ end+ end+end\ No newline at end of filediff --git a/core/app/models/spree/page_blocks/image.rb b/core/app/models/spree/page_blocks/image.rbindex 3b134fa7a8..354b691464 100644--- a/core/app/models/spree/page_blocks/image.rb+++ b/core/app/models/spree/page_blocks/image.rb@@ -1,6 +1,8 @@module Spreemodule PageBlocksclass Image < Spree::PageBlock+ include Spree::HasImageAltText+alias image assetTOP_PADDING_DEFAULT = 20@@ -8,6 +10,7 @@ module Spreepreference :height, :integer, default: 64preference :mobile_height, :integer, default: 32+ preference :image_alt, :stringdef icon_name'photo'diff --git a/core/app/models/spree/page_sections/image_banner.rb b/core/app/models/spree/page_sections/image_banner.rbindex d8c703a1f3..09ce769e55 100644--- a/core/app/models/spree/page_sections/image_banner.rb+++ b/core/app/models/spree/page_sections/image_banner.rb@@ -1,12 +1,15 @@module Spreemodule PageSectionsclass ImageBanner < Spree::PageSection+ include Spree::HasImageAltText+TOP_PADDING_DEFAULT = 0BOTTOM_PADDING_DEFAULT = 0preference :overlay_transparency, :integer, default: 40preference :height, :integer, default: 384preference :vertical_alignment, :string, default: :middle+ preference :image_alt, :stringdef default_blocks[@@ -45,6 +48,8 @@ module Spreedef icon_name'slideshow'end++endend-end+end\ No newline at end of filediff --git a/core/app/models/spree/page_sections/image_with_text.rb b/core/app/models/spree/page_sections/image_with_text.rbindex 4ef61dfe0b..ce91a9049d 100644--- a/core/app/models/spree/page_sections/image_with_text.rb+++ b/core/app/models/spree/page_sections/image_with_text.rb@@ -1,11 +1,14 @@module Spreemodule PageSectionsclass ImageWithText < Spree::PageSection+ include Spree::HasImageAltText+TOP_PADDING_DEFAULT = 16BOTTOM_PADDING_DEFAULT = 16preference :desktop_image_alignment, :string, default: 'right'preference :vertical_alignment, :string, default: :middle+ preference :image_alt, :stringhas_one :link, ->(ps) { ps.links }, class_name: 'Spree::PageLink', as: :parent, dependent: :destroy, inverse_of: :parentaccepts_nested_attributes_for :link@@ -55,6 +58,8 @@ module Spreedef icon_name'photo'end++endendenddiff --git a/core/app/models/spree/page_sections/newsletter.rb b/core/app/models/spree/page_sections/newsletter.rbindex 85c9398836..48215bc47a 100644--- a/core/app/models/spree/page_sections/newsletter.rb+++ b/core/app/models/spree/page_sections/newsletter.rb@@ -1,7 +1,10 @@module Spreemodule PageSectionsclass Newsletter < Spree::PageSection+ include Spree::HasImageAltText+preference :overlay_transparency, :integer, default: 40+ preference :image_alt, :stringdef default_blocks@default_blocks.presence || [diff --git a/core/spec/models/concerns/spree/has_image_alt_text_spec.rb b/core/spec/models/concerns/spree/has_image_alt_text_spec.rbnew file mode 100644index 0000000000..ed67ba4dfb--- /dev/null+++ b/core/spec/models/concerns/spree/has_image_alt_text_spec.rb@@ -0,0 +1,87 @@+require 'spec_helper'++RSpec.describe Spree::HasImageAltText, type: :concern do+ # Create a dummy class to test the concern+ let(:dummy_class) do+ Class.new do+ include Spree::HasImageAltText++ attr_accessor :preferred_image_alt, :asset++ def initialize+ @preferred_image_alt = nil+ @asset = nil+ end+ end+ end++ let(:instance) { dummy_class.new }++ describe 'included behavior' do+ it 'creates the image_alt method' do+ expect(instance).to respond_to(:image_alt)+ end++ it 'returns preference value when present' do+ instance.preferred_image_alt = 'Custom alt text'+ expect(instance.image_alt).to eq('Custom alt text')+ end++ it 'falls back when preference is whitespace-only' do+ instance.preferred_image_alt = ' '+ expect(instance.image_alt).to eq('Image')+ end++ it 'returns filename-based alt when preference is blank but asset has filename' do+ instance.preferred_image_alt = ''++ # Mock asset with filename+ asset_double = double('asset')+ allow(asset_double).to receive(:filename).and_return('test-image_file.jpg')+ instance.asset = asset_double++ expect(instance.image_alt).to eq('test image file')+ end++ it 'returns "Image" as fallback when no preference and no asset' do+ instance.preferred_image_alt = nil+ instance.asset = nil++ expect(instance.image_alt).to eq('Image')+ end++ context 'when asset has no filename' do+ it 'returns "Image" fallback' do+ instance.preferred_image_alt = nil++ # Mock asset without filename+ asset_double = double('asset')+ allow(asset_double).to receive(:filename).and_return(nil)+ instance.asset = asset_double++ expect(instance.image_alt).to eq('Image')+ end+ end++ context 'filename processing' do+ it 'correctly converts various filename formats' do+ instance.preferred_image_alt = nil++ test_cases = [+ ['hero-banner_image.jpg', 'hero banner image'],+ ['product_photo-main.png', 'product photo main'],+ ['logo.svg', 'logo'],+ ['My-File_Name.webp', 'My File Name']+ ]++ test_cases.each do |filename, expected_alt|+ asset_double = double('asset')+ allow(asset_double).to receive(:filename).and_return(filename)+ instance.asset = asset_double++ expect(instance.image_alt).to eq(expected_alt)+ end+ end+ end+ end+enddiff --git a/storefront/app/views/themes/default/spree/page_sections/_image_banner.html.erb b/storefront/app/views/themes/default/spree/page_sections/_image_banner.html.erbindex 1c32933c3c..b591e6d2b3 100644--- a/storefront/app/views/themes/default/spree/page_sections/_image_banner.html.erb+++ b/storefront/app/views/themes/default/spree/page_sections/_image_banner.html.erb@@ -1,3 +1,4 @@+<% cache_unless page_builder_enabled?, spree_storefront_base_cache_scope.call(section) do %><div class="w-full relative image-banner" style="<%= section_styles(section) %>;height: <%= section.preferred_height %>px"><div class="w-full absolute left-0 image-banner--image-wrapper" style="height: <%= section.preferred_height %>px; opacity: <%= section.preferred_overlay_transparency %>%;">@@ -9,7 +10,7 @@<source media="(max-width: 1080px)" srcset="<%= spree_image_url(section.image, width: 540, height: nil) %>"><source media="(max-width: 1200px)" srcset="<%= spree_image_url(section.image, width: 620, height: nil) %>"><source media="(min-width: 1201px)" srcset="<%= spree_image_url(section.image, width: 1000, height: nil) %>">- <%= spree_image_tag(section.image, width: 1000, class: "w-full h-full object-cover", loading: "lazy") %>+ <%= spree_image_tag(section.image, width: 1000, class: "w-full h-full object-cover", loading: "lazy", alt: section.image_alt) %></picture><% end %></div>@@ -23,7 +24,7 @@'justify-center'end%>- <div class="flex h-full w-full items-center justify-center">+ <div class="flex justify-center items-center w-full h-full"><div class="flex flex-col <%= vertical_alignment_class %> p-5 z-10 w-full gap-y-2 h-full image-banner--block-wrapper"><% section.blocks.includes(:rich_text_text, :links).each do |block| %><% case block.type %>@@ -38,7 +39,7 @@<% when 'Spree::PageBlocks::Buttons' %><div <%= block_attributes(block) %> class="w-full text-center image-banner--buttons"><div class="inline-block">- <div class="flex flex-col lg:flex-row gap-4">+ <div class="flex flex-col gap-4 lg:flex-row"><% if block.link.present? %><% if block.link.linkable_url.present? %><%= page_builder_link_to block.link, label: block.link.label, class: "btn-#{block.preferred_button_style_1}", target: (block.link.open_in_new_tab ? "_blank" : nil) %>diff --git a/storefront/app/views/themes/default/spree/page_sections/_image_with_text.html.erb b/storefront/app/views/themes/default/spree/page_sections/_image_with_text.html.erbindex fde718c7be..2a166ec5c4 100644--- a/storefront/app/views/themes/default/spree/page_sections/_image_with_text.html.erb+++ b/storefront/app/views/themes/default/spree/page_sections/_image_with_text.html.erb@@ -8,6 +8,7 @@<%= image_tag spree_image_url(section.image, width: 1000, height: nil),class: "w-full h-full object-cover",loading: :lazy,+ alt: section.image_alt,sizes: "(max-width: 1024px) 100vw, 50vw",srcset: [640, 750, 828, 1080, 1200, 2000].map { |width|"#{spree_image_url(section.image, width: width / 2, height: nil)} #{width}w"@@ -46,7 +47,7 @@<% when 'Spree::PageBlocks::Buttons' %><div class="w-full image-with-text--buttons" <%= block_attributes(block) %>><div class="inline-block">- <div class="flex flex-col lg:flex-row gap-4">+ <div class="flex flex-col gap-4 lg:flex-row"><% if block.link %><%= page_builder_link_to block.link,label: block.link.label,diff --git a/storefront/app/views/themes/default/spree/page_sections/_newsletter.html.erb b/storefront/app/views/themes/default/spree/page_sections/_newsletter.html.erbindex 4f6ae1641a..339eea0738 100644--- a/storefront/app/views/themes/default/spree/page_sections/_newsletter.html.erb+++ b/storefront/app/views/themes/default/spree/page_sections/_newsletter.html.erb@@ -2,10 +2,11 @@<% if loaded %><% img = section.image %><% if img.attached? && img.variable? %>- <img- class="w-full h-full absolute top-0 object-cover z-0"- style="opacity: <%= section.preferred_overlay_transparency %>%"- src="<%= spree_image_url(img, width: 2000, height: nil) %>" />+ <img+ class="object-cover absolute top-0 z-0 w-full h-full"+ style="opacity: <%= section.preferred_overlay_transparency %>%"+ alt="<%= section.image_alt %>"+ src="<%= spree_image_url(img, width: 2000, height: nil) %>" /><% end %><div class="w-full page-container flex flex-col text-center py-5 px-5 z-10"><% section.blocks.includes(:rich_text_text).each do |block| %>@@ -38,7 +39,7 @@<div <%= block_attributes(block) %> class="flex justify-center w-full"><% if block.image.attached? && block.image.variable? %><% image_style = "aspect-ratio: #{spree_asset_aspect_ratio(block.image)}; --desktop-height: #{block.preferred_height}px; --mobile-height: #{block.preferred_mobile_height}px;" %>- <%= image_tag spree_image_url(block.image, width: 1000, height: 1000), height: block.preferred_height, class: "custom-desktop-height custom-mobile-height", style: image_style %>+ <%= image_tag spree_image_url(block.image, width: 1000, height: 1000), alt: block.image_alt, height: block.preferred_height, class: "custom-desktop-height custom-mobile-height", style: image_style %><% end %></div><% end %>diff --git a/storefront/app/views/themes/default/spree/products/_featured_image.html.erb b/storefront/app/views/themes/default/spree/products/_featured_image.html.erbindex 4844019087..1759714143 100644--- a/storefront/app/views/themes/default/spree/products/_featured_image.html.erb+++ b/storefront/app/views/themes/default/spree/products/_featured_image.html.erb@@ -1,27 +1,21 @@<% with_cover_image ||= true %><% object ||= product %>-<% if object.default_image.present? && object.default_image.attached? %><% height ||= theme_setting('product_listing_image_height') %><% width ||= theme_setting('product_listing_image_width') %>-<figure class="group <%= 'aspect-1' if height == width %> card-product-featured-images-container bg-transparent h-full"><% if with_cover_image %><% object_class = 'object-cover object-center' %><% else %><% object_class = 'object-contain object-top' %><% end %>-<% image_hover_class = object.secondary_image.present? && object.secondary_image.attached? ?"w-full group-hover:opacity-0 transition-opacity duration-500 z-10 relative h-full bg-background product-card !max-h-full #{object_class}" :"w-full h-full !max-h-full #{object_class}" %>-- <%= spree_image_tag(object.default_image, width: width, height: height, loading: :lazy, alt: object.default_image.alt, class: image_hover_class) %>-+ <%= spree_image_tag(object.default_image, width: width, height: height, loading: :lazy, alt: "#{object.name} #{Spree.t('storefront.products.primary_image', default: 'primary image')}", class: image_hover_class) %><% if object.secondary_image.present? && object.secondary_image.attached? %><% secondary_image_class = "w-full absolute top-0 left-0 opacity-100 h-full !max-h-full #{object_class}" %>-- <%= spree_image_tag(object.secondary_image, width: width, height: height, loading: :lazy, alt: object.secondary_image.alt, class: secondary_image_class) %>+ <%= spree_image_tag(object.secondary_image, width: width, height: height, loading: :lazy, alt: "#{object.name} #{Spree.t('storefront.products.secondary_image', default: 'secondary image')}", class: secondary_image_class) %><% end %></figure><% else %>diff --git a/storefront/app/views/themes/default/spree/shared/_logo.html.erb b/storefront/app/views/themes/default/spree/shared/_logo.html.erbindex ff33b7810e..b33f50cfd7 100644--- a/storefront/app/views/themes/default/spree/shared/_logo.html.erb+++ b/storefront/app/views/themes/default/spree/shared/_logo.html.erb@@ -1,13 +1,10 @@<% logo ||= current_store.logo %><% height ||= 64 %><% logo_css ||= '' %>-<%= link_to spree.root_path, id: 'site-logo' do %>- <span class='sr-only'><%= current_store.name %></span><% if logo.present? && logo.attached? && logo.variable? %><% aspect_ratio = spree_asset_aspect_ratio(logo) %><% logo_style = "aspect-ratio: #{aspect_ratio};" %>-<% if aspect_ratio.to_f > 10 %><% mobile_class = "h-[16px]" %><% desktop_class = "md:h-[32px]" %>@@ -20,7 +17,6 @@<% else %><% mobile_class = "h-[32px]" %><% end %>-<% if defined?(height) && height > 0 %><% logo_style += "--desktop-height: #{height}px;" %><% cdn_height = height %>@@ -30,13 +26,10 @@<% else %><% cdn_height = 32 %><% end %>-<% img_class = "w-auto #{mobile_class} #{desktop_class} #{logo_css}" %>-<% variant_options = spree_image_variant_options(resize_to_fit: [nil, cdn_height * 3]) %>-- <%= image_tag main_app.cdn_image_url(logo.variant(variant_options)), class: img_class, alt: current_store.name, style: logo_style, height: cdn_height %>+ <%= image_tag main_app.cdn_image_url(logo.variant(variant_options)), class: img_class, alt: current_store.name + " logo", style: logo_style, height: cdn_height %><% else %>- <h1 class='text-lg uppercase text-center'><%= current_store.name %></h1>+ <h1 class='text-lg text-center uppercase'><%= current_store.name %></h1><% end %><% end %>diff --git a/storefront/config/locales/en.yml b/storefront/config/locales/en.ymlindex 7188976b4d..35cfeb47a9 100644--- a/storefront/config/locales/en.yml+++ b/storefront/config/locales/en.yml@@ -57,6 +57,8 @@ en:status: You are currently %{status} to the newsletters.products:pinch_to_zoom_html: Pinch to<br>zoom+ primary_image: primary image+ secondary_image: secondary imagerefund_action_not_required_message:order_canceled:fully: This order has been canceled. No further action is required. You will receive a refund within couple business days.