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
Add ability to register custom page sections per theme (#12682)
Damian Legawiec71b98a8e0633
diff --git a/admin/app/controllers/spree/admin/page_sections_controller.rb b/admin/app/controllers/spree/admin/page_sections_controller.rbindex 355174be60..b35d5158b4 100644--- a/admin/app/controllers/spree/admin/page_sections_controller.rb+++ b/admin/app/controllers/spree/admin/page_sections_controller.rb@@ -7,7 +7,7 @@ module Spreedef createpage_section_type = params.dig(:page_section, :type)- allowed_types = Rails.application.config.spree.page_sections.map(&:to_s)+ allowed_types = available_page_section_types.map(&:to_s)if allowed_types.include?(page_section_type) && page_section_type.safe_constantize.present?@page_section = page_section_type.constantize.new(permitted_resource_params)@@ -52,6 +52,18 @@ module Spreecurrent_store.theme_previews.find(params[:theme_id])endend++ def available_page_section_types+ return [] unless @pageable.present?++ if @pageable.is_a?(Spree::Theme)+ @pageable.available_page_sections+ elsif @pageable.respond_to?(:theme)+ @pageable.theme.available_page_sections+ else+ []+ end+ endendendenddiff --git a/core/app/models/spree/theme.rb b/core/app/models/spree/theme.rbindex 42bc25046d..2bd19ca5fa 100644--- a/core/app/models/spree/theme.rb+++ b/core/app/models/spree/theme.rb@@ -113,6 +113,9 @@ module Spreeendend+ # Creates a new preview for the theme+ #+ # @return [Spree::Theme]def create_previewActiveRecord::Base.connected_to(role: :writing) doApplicationRecord.transaction do@@ -152,16 +155,46 @@ module Spreeendend+ # Returns an array of available layout section classes for the theme+ #+ # @return [Array<Class>]def available_layout_sections- Rails.application.config.spree.theme_layout_sections+ [+ *Rails.application.config.spree.theme_layout_sections,+ *custom_layout_sections+ ]+ end++ # Returns an array of custom layout section classes for the theme+ #+ # @return [Array<Class>]+ def custom_layout_sections+ # you can override this method in your theme to return a list of custom layout sections for your theme+ # [Spree::PageSections::Custom, Spree::PageSections::Custom2]+ []end+ # Returns an array of available page section classes for the theme+ #+ # @return [Array<Class>]def available_page_sectionsreturn @available_page_sections if @available_page_sections- @available_page_sections ||= Rails.application.config.spree.page_sections.find_all do |section_class|- section_class.role == 'content'- end.sort_by(&:name)+ @available_page_sections ||= [+ *Rails.application.config.spree.page_sections.find_all do |section_class|+ section_class.role == 'content'+ end,+ *custom_page_sections+ ].sort_by(&:name)+ end++ # Returns an array of custom page section classes for the theme+ #+ # @return [Array<Class>]+ def custom_page_sections+ # you can override this method in your theme to return a list of custom page sections for your theme+ # [Spree::PageSections::Custom, Spree::PageSections::Custom2]+ []enddef restore_defaults!diff --git a/storefront/lib/generators/spree/storefront/theme/templates/model.rb.tt b/storefront/lib/generators/spree/storefront/theme/templates/model.rb.ttindex af56d79a1c..53a8cd3771 100644--- a/storefront/lib/generators/spree/storefront/theme/templates/model.rb.tt+++ b/storefront/lib/generators/spree/storefront/theme/templates/model.rb.tt@@ -7,6 +7,12 @@ module Spreelicense: 'MIT',}end++ def custom_page_sections+ # you can override this method in your theme to return a list of custom page sections for your theme+ # [Spree::PageSections::Custom, Spree::PageSections::Custom2]+ []+ endendendend