diff --git a/src/ContentElementHelper.php b/src/ContentElementHelper.php index 7932332..1d94ecb 100644 --- a/src/ContentElementHelper.php +++ b/src/ContentElementHelper.php @@ -13,13 +13,6 @@ public static function module($content, $column = 'main') { $contentModuleClass = ContentElement::findClass($content->type); $element = new $contentModuleClass($content, $column); - $moduleModel = ModuleModel::findByPk($element->module); - $moduleClass = Module::findClass($moduleModel->type); - $module = new $moduleClass($moduleModel, $column); - - $_module = Helper::toObj($moduleModel); - $_module->compiledHTML = Helper::replaceHTML(@$module->generate()); - - return $_module; + return ModuleHelper::get($element->module); } } diff --git a/src/FileHelper.php b/src/FileHelper.php index dc6b21b..76ae245 100644 --- a/src/FileHelper.php +++ b/src/FileHelper.php @@ -3,16 +3,33 @@ namespace DieSchittigs\ContaoContentApi; use Contao\FilesModel; +use Contao\ImageSizeModel; +use Contao\ImageSizeItemModel; class FileHelper { - public static function file($uuid) + public static function file($uuid, $size = null) { $model = FilesModel::findOneBy('uuid', $uuid); if(!$model) return null; $result = Helper::toObj($model); unset($result->pid); unset($result->uuid); + $result->path = '/' + $result->path; + if($size && count($size) == 3){ + if(is_numeric($size[2])){ + $result->size = Helper::toObj(ImageSizeModel::findOneById($size[2])); + if($result->size){ + $result->size->subSizes = Helper::toObj(ImageSizeItemModel::findVisibleByPid($result->size->id)); + } + } else { + $result->size = [ + 'width' => $size[0], + 'height' => $size[1], + 'resizeMode' => $size[2] + ]; + } + } return $result; } } diff --git a/src/FrontendApi.php b/src/FrontendApi.php index 980d4e3..2305c46 100644 --- a/src/FrontendApi.php +++ b/src/FrontendApi.php @@ -86,6 +86,11 @@ public function handle(Request $request) ]; } break; + case '/module': + $result = ModuleHelper::get( + $request->query->get('id', null) + ); + break; case '/': $result = new \stdClass; $result->page = PageHelper::getPage( diff --git a/src/Helper.php b/src/Helper.php index 84ab0a1..a879571 100644 --- a/src/Helper.php +++ b/src/Helper.php @@ -27,6 +27,8 @@ public static function toObj($instance, $fields = null) } else { $obj = (object) $instance; } + $imageSize = null; + $images = []; foreach ($obj as $key => &$val) { $originalVal = $val; if ($fields && !in_array($key, $fields)) { @@ -44,16 +46,22 @@ public static function toObj($instance, $fields = null) if ($val == '') { $val = null; } + if($key == 'size'){ + $imageSize = $val; + } if (strpos($key, 'SRC') !== false && $val) { - if (is_array($val)) { - $__val = []; - foreach ($val as $_key => $_val) { - $__val[] = FileHelper::file($_val); - } - $val = $__val; - } else { - $val = FileHelper::file($val); + $images[$key] = $val; + } + } + foreach($images as $key => $image){ + if (is_array($image)) { + $val = []; + foreach ($image as $_key => $_val) { + $val[] = FileHelper::file($_val, $imageSize); } + $obj[$key] = $val; + } else { + $obj[$key] = FileHelper::file($image, $imageSize); } } return (object) $obj; @@ -67,6 +75,9 @@ public static function replaceHTML($html) $html = str_replace('"'.$apiScript, '"', $html); $html = str_replace('src="files', 'src="/files', $html); $html = str_replace('href="files', 'href="/files', $html); + $html = str_replace('src="assets', 'src="/assets', $html); + $html = str_replace('href="assets', 'href="/assets', $html); + $html = str_replace('srcset="assets', 'href="/assets', $html); return $html; }