Skip to content

Commit

Permalink
Merge pull request #27 from Raistlfiren/fieldType-hotfix
Browse files Browse the repository at this point in the history
Field type hotfix
  • Loading branch information
Raistlfiren authored Sep 8, 2016
2 parents 39b33d8 + 8783883 commit 92fa8c2
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
11 changes: 10 additions & 1 deletion src/Parser/Field/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -57,6 +63,7 @@ public static function build(
$repeatingFieldCollection = new RepeatingFieldCollection([]);
/** @var RepeatingFieldCollection[] $collection */
$repeatingFieldCollection = self::build(
$metadata,
$resourceManager,
$config,
$fields,
Expand All @@ -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)) {
Expand Down
33 changes: 29 additions & 4 deletions src/Parser/Field/Type/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class File extends AbstractType
/** @var Config $config */
protected $config;

protected $fieldType;

/**
* File constructor.
* @param $type
Expand All @@ -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']);
Expand All @@ -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']);
}
Expand Down Expand Up @@ -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;
}
}
11 changes: 10 additions & 1 deletion src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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 = [])
Expand All @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/Provider/APIProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ function ($app) {
function ($app) {
return new Parser(
$app['jsonapi.config'],
$app['resources']
$app['resources'],
$app['storage.metadata']
);
}
);
Expand Down

0 comments on commit 92fa8c2

Please sign in to comment.