Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions config/config.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ headers:
replacements:
'src="//': 'src="http://'
'href="//': 'href="http://'

# The base url where the API is accessible.
base: '/json'

Expand All @@ -52,4 +52,7 @@ date-iso-8601: true
jsonoptions: 0

# Set this to true to disable frontend completely and display an empty page
disablefrontend: false
disablefrontend: false

# Adds a `ownerdisplayname` which has the record's ownerid's display name
enabledisplaynames: false
32 changes: 32 additions & 0 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ class Config
*/
private $disableFrontend;

/**
* @var boolean
*/
private $enableDisplayNames;

/**
* @param array $config
* @param Application $app
*/
public function __construct($config, Application $app)
{
if (isset($config['base'])) {
Expand All @@ -85,6 +94,9 @@ public function __construct($config, Application $app)

$disablefrontend = isset($config['disablefrontend']) ? $config['disablefrontend'] : false;
$this->setDisableFrontend($disablefrontend);

$enableDisplayNames = isset($config['enabledisplaynames']) ? $config['enabledisplaynames'] : false;
$this->setEnableDisplayNames($enableDisplayNames);
}

/**
Expand Down Expand Up @@ -350,4 +362,24 @@ public function setDisableFrontend($disableFrontend)

return $this;
}

/**
* @return bool
*/
public function isEnableDisplayNames()
{
return $this->enableDisplayNames;
}

/**
* @param bool $enableDisplayNames
*
* @return Config
*/
public function setEnableDisplayNames($enableDisplayNames)
{
$this->enableDisplayNames = $enableDisplayNames;

return $this;
}
}
26 changes: 26 additions & 0 deletions src/Parser/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Bolt\Extension\Bolt\JsonApi\Parser\Field\FieldFactory;
use Bolt\Storage\Entity\Relations;
use Bolt\Storage\Mapping\MetadataDriver;
use Bolt\Users;

class Parser
{
Expand All @@ -21,13 +22,27 @@ class Parser
/** @var MetadataDriver $metadata */
protected $metadata;

/** @var Users $users */
protected $users;

public function __construct(Config $config, ResourceManager $resourceManager, MetadataDriver $metadata)
{
$this->config = $config;
$this->resourceManager = $resourceManager;
$this->metadata = $metadata;
}

/**
* @param Users $users
*
* @deprecated Probably to be removed in next major version.
* Only use is in `\Bolt\Extension\Bolt\JsonApi\Provider`.
*/
public function setUsers($users)
{
$this->users = $users;
}

public function parseItem($item, $fields = [])
{
$contentType = (string) $item->getContenttype();
Expand Down Expand Up @@ -90,6 +105,17 @@ public function parseItem($item, $fields = [])
$values['attributes'] = $attributes;
}

// Use $this->config->isEnableDisplayNames() instead of isset($this->users),
// if `$users` is mandatory via constructor.
if (isset($this->users) && isset($values['attributes']['ownerid'])) {
$ownerid = $values['attributes']['ownerid'];
$owner = $this->users->getUser($ownerid);

if ($owner) {
$values['attributes']['ownerdisplayname'] = $owner['displayname'];
}
}

$values['links'] = [
'self' => sprintf('%s/%s/%s', $this->config->getBasePath(), $contentType, $id),
];
Expand Down
13 changes: 11 additions & 2 deletions src/Provider/APIProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,20 @@ function ($app) {
*/
$app['jsonapi.parser'] = $app->share(
function ($app) {
return new Parser(
$app['jsonapi.config'],
/** @var \Bolt\Extension\Bolt\JsonApi\Config\Config $jsonApiConfig */
$jsonApiConfig = $app['jsonapi.config'];

$parser = new Parser(
$jsonApiConfig,
$app['resources'],
$app['storage.metadata']
);

if ($jsonApiConfig->isEnableDisplayNames()) {
$parser->setUsers($app['users']);
}

return $parser;
}
);

Expand Down