You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Magnolia CMS页面应用自定义YAML导出动作配置及上下文获取咨询

嘿,我来帮你一步步搞定这个Magnolia CMS的导出动作定制问题~

一、配置自定义导出动作类

既然你已经搭好了Maven模块,接下来先创建并配置你的自定义动作类,让它直接导出YAML且跳过选择对话框:

1. 编写自定义动作类

创建一个继承自Magnolia原生ExportAction的类,在构造方法里强制设置导出格式为YAML,并关闭选择对话框:

import info.magnolia.ui.api.action.ActionExecutionException;
import info.magnolia.ui.api.context.UiContext;
import info.magnolia.ui.contentapp.browser.action.ExportAction;
import info.magnolia.ui.contentapp.browser.action.ExportActionDefinition;
import javax.inject.Inject;

public class YamlOnlyExportAction extends ExportAction {

    private final UiContext uiContext;

    @Inject
    public YamlOnlyExportAction(ExportActionDefinition definition, UiContext uiContext) {
        super(definition, uiContext);
        this.uiContext = uiContext;
        // 强制设置导出格式为YAML
        getDefinition().setFormat("yaml");
        // 关闭格式选择对话框
        getDefinition().setShowDialog(false);
    }

    @Override
    public void execute() throws ActionExecutionException {
        // 这里可以添加你需要的自定义逻辑(比如额外处理页面内容)
        super.execute(); // 调用父类的导出逻辑,已经是YAML格式啦
    }
}

2. 注册动作类到模块配置

在你的模块配置文件(比如/your-module/src/main/resources/META-INF/magnolia/your-module/ui-contentapp/page-app/actions.yaml)里,注册这个动作:

actions:
  yamlExport:
    definitionClass: info.magnolia.ui.contentapp.browser.action.ExportActionDefinition
    implementationClass: com.yourcompany.yourmodule.actions.YamlOnlyExportAction
    label: Export as YAML
    icon: icon-export
    availability:
      rules:
        - name: IsPageRule
二、获取当前页面上下文

要拿到选中的页面信息,有两种常用方式,按需选择:

1. 通过SelectedItemsProvider获取选中页面

在动作类里注入SelectedItemsProvider,直接获取当前选中的页面项:

import info.magnolia.ui.api.item.Item;
import info.magnolia.ui.contentapp.browser.SelectedItemsProvider;
import javax.inject.Inject;

// 修改构造方法,注入SelectedItemsProvider
private final SelectedItemsProvider selectedItemsProvider;

@Inject
public YamlOnlyExportAction(ExportActionDefinition definition, UiContext uiContext, SelectedItemsProvider selectedItemsProvider) {
    super(definition, uiContext);
    this.uiContext = uiContext;
    this.selectedItemsProvider = selectedItemsProvider;
    getDefinition().setFormat("yaml");
    getDefinition().setShowDialog(false);
}

@Override
public void execute() throws ActionExecutionException {
    // 获取第一个选中的页面(如果是批量导出可以遍历集合)
    Item selectedPage = selectedItemsProvider.getSelectedItems().stream().findFirst().orElse(null);
    if (selectedPage != null) {
        // 获取页面的JCR路径/UUID
        String pageIdentifier = selectedPage.getItemId().toString();
        // 这里可以根据标识符做进一步处理,比如获取JCR节点
    }
    super.execute();
}

2. 通过JCR会话获取页面节点

如果需要更底层的JCR操作,可以通过UiContext获取website工作区的会话,结合页面路径获取节点:

import info.magnolia.jcr.util.SessionUtil;
import javax.jcr.Node;
import javax.jcr.Session;

// 在execute方法里添加
if (selectedPage != null) {
    String pagePath = selectedPage.getItemId().toString();
    try {
        Session websiteSession = SessionUtil.getSession("website");
        Node pageNode = websiteSession.getNode(pagePath);
        // 在这里操作页面节点,比如读取属性、子节点等
    } catch (Exception e) {
        throw new ActionExecutionException("Failed to get page node", e);
    }
}
三、替换原有页面应用的导出动作

要让页面应用默认使用你的新动作,覆盖原有导出动作的配置。在你的模块的content-apps/page-app.yaml里修改:

contentApps:
  page:
    subApps:
      browser:
        actions:
          export:
            implementationClass: com.yourcompany.yourmodule.actions.YamlOnlyExportAction
            showDialog: false
            format: yaml

最后,别忘了在模块的pom.xml里确保依赖了Magnolia的内容应用模块:

<dependency>
    <groupId>info.magnolia.ui</groupId>
    <artifactId>magnolia-ui-contentapp</artifactId>
    <version>${magnolia.version}</version>
</dependency>

重启Magnolia实例并清除缓存,你的自定义导出动作就会生效——点击导出按钮会直接把选中页面导出为YAML文件,再也不会弹出格式选择对话框啦~

内容的提问来源于stack exchange,提问作者Ana Sustic

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:43:26