Skip to content

Instantly share code, notes, and snippets.

@johanb
Created September 8, 2015 08:41
Show Gist options
  • Save johanb/f2b08b56703e44d7eb72 to your computer and use it in GitHub Desktop.
Save johanb/f2b08b56703e44d7eb72 to your computer and use it in GitHub Desktop.
protected function transformAttribute($attribute)
{
if ($attribute instanceof DateTime) {
// Convert dates to ISO-8601
return $attribute->format(\DateTime::ISO8601);
} elseif ($attribute instanceof RichTextData) {
// RichTextData
return $attribute->getRawContent();
} elseif ($attribute instanceof ElementCriteriaModel) {
// ElementCriteriaModel
if ($attribute->elementType instanceof AssetElementType) {
$asset = $attribute->first();
return $asset ? UrlHelper::getSiteUrl($asset->getUrl()) : null;
} elseif ($attribute->elementType instanceof MatrixBlockElementType) {
$arrBlocks = [];
foreach ($attribute->find() as $block) {
$arrBlocks[] = $this->transformAttribute($block->getContent());
}
return $arrBlocks;
}
} else {
return ModelHelper::packageAttributeValue($attribute);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment