1

我想知道是否有办法获取所有已呈现文章的 HTML 列表。我希望将媒体图像文件呈现为<img>而不是呈现为^media_display_55aabd2ac89b5e44211ccf86/position:none^.

我为此构建了一个控制器:

module.exports = function BookApiControllerModule(pb) {

  //PB dependencies
  var util = pb.util;
  var BaseApiController = pb.BaseApiController;
  var PluginService = pb.PluginService;
  var BaseObjectService = pb.BaseObjectService;
  var BookService = PluginService.getService('BookService', 'articles-api');
  var ArticleService = pb.ArticleService;
  var TYPE = 'article';

  //Creating an inline service.
  function DndService(context) {
    if (!util.isObject(context)) {
      context = {};
    }
    context.type = TYPE;
    DndService.super_.call(this, context);
  }

  util.inherits(DndService, BaseObjectService);

  DndService.getName = function () {
    return "DndService";
  };

  DndService.init = function (cb) {
    pb.log.debug("DndService: Initialized");
    cb(null, true);
  };

  DndService.beforeSave = function (context, cb) {
    cb(null);
  };

  DndService.afterSave = function (context, cb) {
    cb(null);
  };

  DndService.beforeDelete = function (context, cb) {
    cb(null);
  };

  DndService.afterDelete = function (context, cb) {
    cb(null);
  };

  DndService.validate = function (context, cb) {
    cb(null);
  };

  DndService.merge = function (context, cb) {
    cb(null);
  };

  DndService.format = function (context, cb) {
    cb(null);
  };

  DndService.getAll = function (ctx, cb) {
    ctx.data = [{message: "ASDF"}];
    cb(null);
  };

  BaseObjectService.on(TYPE + '.' + BaseObjectService.GET_ALL, DndService.getAll);
  //Done with the inline service

  function BookApiController() {
    this.service = new DndService();
  };
  util.inherits(BookApiController, pb.BaseApiController);

  BookApiController.getRoutes = function (cb) {
    var routes = [
      {
        method: 'get',
        path: "/api/articles",
        handler: "getAll",
        content_type: 'application/json'
      }
    ];
    cb(null, routes);
  };

  //exports
  return BookApiController;
};

它返回以下内容:

{
    "data": [
        {
            "_id": "55c2a7650668d47274859fc8",
            "author": "55aaa75a3bb0fbbd2056c76d",
            "publish_date": "2015-08-05T22:45:00.000Z",
            "meta_keywords": [
                ""
            ],
            "article_media": [
                "55aabd2ac89b5e44211ccf86",
                "55c2a86d0668d47274859fd1"
            ],
            "article_sections": [],
            "article_topics": [],
            "headline": "wefghj",
            "subheading": "khgvhjvgjvgh",
            "url": "blahhhhh",
            "draft": 0,
            "article_layout": "^media_display_55aabd2ac89b5e44211ccf86/position:none^^media_display_55c2a86d0668d47274859fd1/position:none^",
            "object_type": "article",
            "created": "2015-08-06T00:16:37.889Z",
            "last_modified": "2015-08-06T00:22:46.825Z",
            "id": "55c2a7650668d47274859fc8",
            "template": "pencilblue|index"
        }
    ],
    "total": 1,
    "count": 1,
    "offset": 0,
    "limit": 1000
}
4

1 回答 1

0

您需要在返回媒体之前“渲染”媒体,否则您只会获得图像 ID。看看http://pencilblue.github.io/classes/MediaService.html#method_render(或http://pencilblue.github.io/classes/ImageMediaRenderer.html#method_render for PB 0.6.0)。

于 2016-07-09T11:07:48.427 回答