Skip to content

Instantly share code, notes, and snippets.

@CodeCurosity
Last active April 4, 2020 06:41
Show Gist options
  • Save CodeCurosity/490f9e8eeeb500ff82bbe5ddc3e6a970 to your computer and use it in GitHub Desktop.
Save CodeCurosity/490f9e8eeeb500ff82bbe5ddc3e6a970 to your computer and use it in GitHub Desktop.
Element Api
<?php
use craft\elements\Entry;
use craft\helpers\UrlHelper;
return [
'endpoints' => [
'news.json' => function() {
return [
'elementType' => Entry::class,
'criteria' => ['section' => 'news'],
'transformer' => function(Entry $entry) {
$photos = [];
foreach ($entry->mainImage as $photo) {
$photos[] = $photo->getUrl();
}
$siteId = null;
$htmlcontent = $entry->htmlContent;
$SuperTableRows = [];
foreach ($entry->repeater as $row){
$SuperTableRows[] = [
'lastName' => $row->lastName,
'email' => $row->email
];
}
$content = Craft::$app->getElements()->parseRefs((string)$htmlcontent, $siteId);
return [
'title' => $entry->title,
'dateCreated' => $entry->dateCreated,
'content' => $content,
'url' => $photos,
'repeater' => $SuperTableRows,
];
},
];
},
]
];
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment