I was wondering if there is a way to get a list of all article's HTML already rendered. I want the media image files rendered as <img>
and not as a ^media_display_55aabd2ac89b5e44211ccf86/position:none^.
I've built a controller for this:
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;
};
And it returns the following:
{
"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
}
You need to "render" the media before returning it, otherwise you only get the image id. Have a look at http://pencilblue.github.io/classes/MediaService.html#method_render (or http://pencilblue.github.io/classes/ImageMediaRenderer.html#method_render for PB 0.6.0).