Skip to content

A platform for community discussion. Free, open, simple.

UX: add modifier to customize invite template (#35458)

marstall1b90310eea2f
diff --git a/app/controllers/admin/email_templates_controller.rb b/app/controllers/admin/email_templates_controller.rb
index b6dbf0f3685..5e16dd60e2c 100644
--- a/app/controllers/admin/email_templates_controller.rb
+++ b/app/controllers/admin/email_templates_controller.rb
@@ -122,6 +122,7 @@ class Admin::EmailTemplatesController < Admin::AdminController
user_notifications.user_watching_category_or_tag
user_notifications.user_watching_first_post
]
+ DiscoursePluginRegistry.apply_modifier(:email_template_keys, @email_keys)
end
def show
diff --git a/app/mailers/invite_mailer.rb b/app/mailers/invite_mailer.rb
index f11883761ae..85e98df4730 100644
--- a/app/mailers/invite_mailer.rb
+++ b/app/mailers/invite_mailer.rb
@@ -49,9 +49,15 @@ class InviteMailer < ActionMailer::Base
user_custom_message: sanitized_message,
)
else
+ template =
+ DiscoursePluginRegistry.apply_modifier(
+ :invite_forum_mailer_template,
+ "invite_forum_mailer",
+ invite,
+ )
build_email(
invite.email,
- template: sanitized_message ? "custom_invite_forum_mailer" : "invite_forum_mailer",
+ template: sanitized_message ? "custom_invite_forum_mailer" : template,
inviter_name: inviter_name,
site_domain_name: Discourse.current_hostname,
invite_link: invite.link(with_email_token: true),
diff --git a/spec/mailers/invite_mailer_spec.rb b/spec/mailers/invite_mailer_spec.rb
index 7b456d42bdc..d5ed6fa6ab5 100644
--- a/spec/mailers/invite_mailer_spec.rb
+++ b/spec/mailers/invite_mailer_spec.rb
@@ -75,6 +75,41 @@ RSpec.describe InviteMailer do
end
end
end
+
+ context "with template modifier" do
+ fab!(:invite)
+ let(:plugin) { Plugin::Instance.new }
+ let(:custom_template) { "plugin_custom_invite_template" }
+ I18n.backend.store_translations(
+ :en,
+ {
+ plugin_custom_invite_template: {
+ subject_template: "[%{site_name}] Custom Invite from %{inviter_name}",
+ text_body_template:
+ "Custom invite body: %{invite_link}\n\nFrom: %{inviter_name}\nSite: %{site_domain_name}",
+ },
+ },
+ )
+ it "allows plugins to customize the invite template" do
+ plugin_instance = Plugin::Instance.new
+ @modifier_block = Proc.new { |template, passed_invite| custom_template }
+
+ DiscoursePluginRegistry.register_modifier(
+ plugin_instance,
+ :invite_forum_mailer_template,
+ &@modifier_block
+ )
+
+ mail = InviteMailer.send_invite(invite)
+ expect(mail.subject).to eq("[Discourse] Custom Invite from Bruce Wayne (bruce0)")
+
+ DiscoursePluginRegistry.unregister_modifier(
+ plugin_instance,
+ :invite_forum_mailer_template,
+ &@modifier_block
+ )
+ end
+ end
end
context "when inviting to topic" do
diff --git a/spec/requests/admin/email_templates_controller_spec.rb b/spec/requests/admin/email_templates_controller_spec.rb
index 339772ade35..6f743bd2e7f 100644
--- a/spec/requests/admin/email_templates_controller_spec.rb
+++ b/spec/requests/admin/email_templates_controller_spec.rb
@@ -56,6 +56,26 @@ RSpec.describe Admin::EmailTemplatesController do
template = templates.find { |t| t["id"] == "user_notifications.admin_login" }
expect(template["can_revert"]).to eq(false)
end
+
+ it "includes custom email template keys added via modifier" do
+ custom_keys = %w[custom.email_template_one custom.email_template_two]
+
+ # Register a modifier to add custom email template keys
+ block = Proc.new { |keys| keys + custom_keys }
+ plugin_instance = Plugin::Instance.new
+ plugin_instance.register_modifier(:email_template_keys, &block)
+
+ # Get the modified email keys
+ modified_keys = Admin::EmailTemplatesController.email_keys
+
+ # Verify custom keys are included
+ expect(modified_keys).to include(*custom_keys)
+
+ # Verify original keys are still present
+ expect(modified_keys).to include("user_notifications.admin_login")
+
+ DiscoursePluginRegistry.unregister_modifier(plugin_instance, :email_template_keys, &block)
+ end
end
shared_examples "email templates inaccessible" do