Skip to content

armgit / spree

Public Imported from GitHub
0

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.

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.rb
index 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 Spree
def create
page_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 Spree
current_store.theme_previews.find(params[:theme_id])
end
end
+
+ 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
+ end
end
end
end
diff --git a/core/app/models/spree/theme.rb b/core/app/models/spree/theme.rb
index 42bc25046d..2bd19ca5fa 100644
--- a/core/app/models/spree/theme.rb
+++ b/core/app/models/spree/theme.rb
@@ -113,6 +113,9 @@ module Spree
end
end
+ # Creates a new preview for the theme
+ #
+ # @return [Spree::Theme]
def create_preview
ActiveRecord::Base.connected_to(role: :writing) do
ApplicationRecord.transaction do
@@ -152,16 +155,46 @@ module Spree
end
end
+ # 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_sections
return @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]
+ []
end
def 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.tt
index 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 Spree
license: '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]
+ []
+ end
end
end
end