Implementing Gryphon-style JSON Feeds
A few clients may rely on Gryphon-style JSON feeds because of connected apps. While the new CEO feeds are much cleaner, there are any number of reasons a client may need to keep the old style around.
After enabling the client module, create a new SectionInterceptor
, or update the existing one.
You'll be altering the beforeRenderJson
method. This will allow the site.tld/section/foo.json
feed to emulate Gryphon.
use Ceo\Http\Response;
use Ceo\Helpers\Time;
class SectionInterceptor extends \Ceo\Compat\Interceptors\SectionInterceptor
{
public function beforeRenderJson($params = [])
{
// allow users to append "disable_compat=1" to the URL to get the
// normal CEO feed
if ($this->request->hasQuery('disable_compat')) {
$params = parent::beforeRenderJson($params);
return $params;
}
// build a "Gryphon Compatible" JSON export
$jsonData = [
'section' => [
'uid' => $params['section']->id,
'slug' => $params['section']->slug,
'name' => rawurlencode($params['section']->title),
'name_formatted' => rawurlencode($params['section']->title),
'content' => rawurlencode($params['section']->description),
'content_formatted' => rawurlencode($params['section']->description),
'sidebar' => '',
'sidebar_formatted' => '',
'template' => $params['section']->template,
'article_limit' => 20,
'created' => $params['section']->created_at ? Time::fromSqlTime($params['section']->created_at)->format('U') : time(),
'modified' => $params['section']->modified_at ? Time::fromSqlTime($params['section']->modified_at)->format('U') : time(),
'tags' => []
],
'articles' => []
];
$tags = [];
foreach ($params['section']->tags as $tag) {
$tags[] = [
'uid' => $tag->id,
'name' => $tag->name,
'name_normalized' => $tag->slug,
'status' => 1
];
}
$jsonData['section']['tags'] = $tags;
$articles = [];
foreach ($params['articles'] as $article) {
$out = [
'uid' => $article->id,
'slug' => $article->slug,
'headline' => rawurlencode($article->headline),
'headline_formatted'=> rawurlencode($article->headline),
'subhead' => rawurlencode($article->subhead),
'subhead_formatted' => rawurlencode($article->subhead),
'althead' => '',
'althead_formatted' => '',
'abstract' => rawurlencode($article->abstract),
'abstract_formatted'=> rawurlencode($article->abstract),
'infobox' => rawurlencode($article->infobox),
'infobox_formatted' => rawurlencode($article->infobox),
'copy' => rawurlencode($article->content),
'copy_formatted' => rawurlencode($article->content),
'template' => $article->template,
'status' => $article->published_at ? 1 : 0,
'created' => Time::fromSqlTime($article->created_at)->format('U'),
'modified' => Time::fromSqlTime($article->modified_at)->format('U'),
'weight' => $article->weight,
'published' => $article->published_at ? Time::fromSqlTime($article->published_at)->format('U') : '',
'url_slug' => $article->slug,
'tags' => [],
'media' => [],
'authors' => []
];
$tags = [];
foreach ($article->tags as $tag) {
$tags[] = [
'uid' => $tag->id,
'name' => $tag->name,
'name_normalized' => $tag->slug,
'status' => 1
];
}
$out['tags'] = $tags;
$authors = [];
foreach ($article->authors as $author) {
$authors[] = [
'uid' => $author->id,
'name' => $author->name,
'name_normalized' => $author->slug,
'email' => $author->email,
'tagline' => '',
'status' => 1
];
}
$out['authors'] = $authors;
$media = [];
if ($article->dominantMedia) {
$media = [
'uid' => $article->dominantMedia->id,
'base_name' => $article->dominantMedia->attachment_uuid,
'extension' => $article->dominantMedia->extension,
'caption' => rawurlencode($article->dominantMedia->content),
'caption_formatted' => rawurlencode($article->dominantMedia->content),
'source' => rawurlencode($article->dominantMedia->source),
'status' => $article->dominantMedia->published_at ? 1 : 0,
'type' => $article->dominantMedia->type,
'created' => Time::fromSqlTime($article->dominantMedia->created_at)->format('U'),
'modified' => Time::fromSqlTime($article->dominantMedia->modified_at)->format('U'),
'weight' => $article->dominantMedia->weight,
'link' => $article->dominantMedia->click_through,
'preview_ext' => $article->dominantMedia->preview_extension,
'thumbnail_ext' => $article->dominantMedia->preview_extension,
'full_ext' => $article->dominantMedia->preview_extension,
'urlOriginal' => rawurlencode($article->dominantMedia->getOriginalUrl()),
'urlPreview' => rawurlencode($article->dominantMedia->getCustomUrl(['w' => 1000])),
'urlThumbnail' => rawurlencode($article->dominantMedia->getCustomUrl(['w' => 600])),
'url' => rawurlencode($article->dominantMedia->getFriendlyUrl()),
'authors' => []
];
$authors = [];
foreach ($article->dominantMedia->authors as $author) {
$authors[] = [
'uid' => $author->id,
'name' => $author->name,
'name_normalized' => $author->slug,
'email' => $author->email,
'tagline' => '',
'status' => 1
];
}
$media['authors'] = $authors;
}
$out['media'] = [$media];
$articles[] = $out;
}
$jsonData['articles'] = $articles;
$resp = new Response;
$resp->asJson();
$resp->setContent(json_encode([$jsonData], JSON_PRETTY_PRINT));
return $resp;
}
}