diff --git a/src/Parser/Field/FieldFactory.php b/src/Parser/Field/FieldFactory.php index cb032fa..bb7b07f 100644 --- a/src/Parser/Field/FieldFactory.php +++ b/src/Parser/Field/FieldFactory.php @@ -32,6 +32,7 @@ class FieldFactory * of the same attributes. It loads classes based upon the field type. */ public static function build( + $metadata, ResourceManager $resourceManager, Config $config, $fields, @@ -48,6 +49,11 @@ public static function build( $field = $field->getFieldtype(); } else { $data = $item->get($field); + if (isset($metadata['fields'])) { + if (isset($metadata['fields'][$field])) { + $fieldType = $metadata['fields'][$field]['data']['type']; + } + } } if ($data instanceof Taxonomy) { @@ -57,6 +63,7 @@ public static function build( $repeatingFieldCollection = new RepeatingFieldCollection([]); /** @var RepeatingFieldCollection[] $collection */ $repeatingFieldCollection = self::build( + $metadata, $resourceManager, $config, $fields, @@ -76,8 +83,10 @@ public static function build( } elseif ($data instanceof Carbon) { $type = new Date($field, $data, $config); } elseif (!$data instanceof Relations) { - if (in_array($field, self::$fileTypes)) { + //Check to see if image, imagelist, file, or filelist to handle unique rendering. + if (in_array($fieldType, self::$fileTypes)) { $type = new File($field, $data, $resourceManager, $config); + $type->setFieldType($fieldType); } else { //We need to check if the label is an int. If it isn't then we'll use that for type (repeaters). if (is_int($label)) { diff --git a/src/Parser/Field/Type/File.php b/src/Parser/Field/Type/File.php index 255ca99..08ff5a8 100644 --- a/src/Parser/Field/Type/File.php +++ b/src/Parser/Field/Type/File.php @@ -14,6 +14,8 @@ class File extends AbstractType /** @var Config $config */ protected $config; + protected $fieldType; + /** * File constructor. * @param $type @@ -34,9 +36,14 @@ public function __construct( public function render() { + //Check to see if value is empty to avoid showing an empty link or adding empty values + if (empty($this->getValue())) { + return $this->getValue(); + } + $values = []; - if ($this->getType() == 'imagelist') { + if ($this->getFieldType() == 'imagelist') { foreach ($this->getValue() as &$image) { $image['url'] = $this->makeAbsoluteLinkToResource($image['filename']); $image['thumbnail'] = $this->makeAbsoluteLinkToThumbnail($image['filename']); @@ -45,20 +52,20 @@ public function render() } - if ($this->getType() == 'filelist') { + if ($this->getFieldType() == 'filelist') { foreach ($this->getValue() as &$file) { $file['url'] = $this->makeAbsoluteLinkToResource($file['filename']); $values[] = $file; } } - if ($this->getType() === 'image') { + if ($this->getFieldType() === 'image') { $values = $this->getValue(); $values['url'] = $this->makeAbsoluteLinkToResource($values['file']); $values['thumbnail'] = $this->makeAbsoluteLinkToThumbnail($values['file']); } - if ($this->getType() === 'file') { + if ($this->getFieldType() === 'file') { $values['file'] = $this->getValue(); $values['url'] = $this->makeAbsoluteLinkToResource($values['file']); } @@ -96,4 +103,22 @@ protected function makeAbsoluteLinkToThumbnail($filename = '') $filename ); } + + /** + * @return mixed + */ + public function getFieldType() + { + return $this->fieldType; + } + + /** + * @param mixed $fieldType + * @return File + */ + public function setFieldType($fieldType) + { + $this->fieldType = $fieldType; + return $this; + } } diff --git a/src/Parser/Parser.php b/src/Parser/Parser.php index 3a42278..811e80f 100644 --- a/src/Parser/Parser.php +++ b/src/Parser/Parser.php @@ -8,6 +8,7 @@ use Bolt\Extension\Bolt\JsonApi\Parser\Field\FieldFactory; use Bolt\Extension\Bolt\JsonApi\Parser\Field\FieldCollection; use Bolt\Storage\EntityProxy; +use Bolt\Storage\Mapping\MetadataDriver; class Parser { @@ -18,10 +19,14 @@ class Parser /** @var ResourceManager $resourceManager */ protected $resourceManager; - public function __construct(Config $config, ResourceManager $resourceManager) + /** @var MetadataDriver $metadata */ + protected $metadata; + + public function __construct(Config $config, ResourceManager $resourceManager, MetadataDriver $metadata) { $this->config = $config; $this->resourceManager = $resourceManager; + $this->metadata = $metadata; } public function parseItem($item, $fields = []) @@ -46,8 +51,12 @@ public function parseItem($item, $fields = []) ]; $fields = array_unique($fields); + //Get field type information + $metadata = $this->metadata->getClassMetadata($contentType); + /** @var FieldCollection $fieldCollection */ $fieldCollection = FieldFactory::build( + $metadata, $this->resourceManager, $this->config, $fields, diff --git a/src/Provider/APIProvider.php b/src/Provider/APIProvider.php index af5d601..5dfe651 100644 --- a/src/Provider/APIProvider.php +++ b/src/Provider/APIProvider.php @@ -75,7 +75,8 @@ function ($app) { function ($app) { return new Parser( $app['jsonapi.config'], - $app['resources'] + $app['resources'], + $app['storage.metadata'] ); } );