ソースを参照

seo: add AfterSave hook

xuxin 1 年間 前
コミット
f0e0e8db8d
2 ファイル変更16 行追加4 行削除
  1. 5 0
      seo/admin.go
  2. 11 4
      seo/collection.go

+ 5 - 0
seo/admin.go

@@ -387,5 +387,10 @@ func (collection *Collection) save(ctx *web.EventContext) (r web.EventResponse,
 		return
 	}
 	r.VarsScript = fmt.Sprintf(`vars.seoSnackbarShow = true;vars.%s = false;`, ctx.R.FormValue("loadingName"))
+	if collection.afterSave != nil {
+		if err = collection.afterSave(ctx.R.Context(), name, locale); err != nil {
+			return
+		}
+	}
 	return
 }

+ 11 - 4
seo/collection.go

@@ -49,10 +49,11 @@ func NewCollection() *Collection {
 // @snippet_begin(SeoCollectionDefinition)
 type Collection struct {
 	registeredSEO []*SEO
-	globalName    string      //default name is GlobalSEO
-	inherited     bool        //default is true. the order is model seo setting, system seo setting, global seo setting
-	dbContextKey  interface{} // get db from context
-	settingModel  interface{} // db model
+	globalName    string                                                             //default name is GlobalSEO
+	inherited     bool                                                               //default is true. the order is model seo setting, system seo setting, global seo setting
+	dbContextKey  interface{}                                                        // get db from context
+	settingModel  interface{}                                                        // db model
+	afterSave     func(ctx context.Context, settingName string, locale string) error // hook called after saving
 }
 
 // @snippet_end
@@ -203,6 +204,12 @@ func (collection *Collection) GetSEOByModel(model interface{}) *SEO {
 	return nil
 }
 
+// AfterSave set the hook called after saving
+func (collection *Collection) AfterSave(v func(ctx context.Context, settingName string, locale string) error) *Collection {
+	collection.afterSave = v
+	return collection
+}
+
 // RenderGlobal render global seo
 func (collection Collection) RenderGlobal(req *http.Request) h.HTMLComponent {
 	return collection.Render(collection.globalName, req)