Skip to content
Open
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
20 changes: 10 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contenttypes:
allowed-fields: [ title, slug, teaser, image ]
where-clause:
status: 'published'
order: title,-datepublish
order: title,-datepublish
pages:
# use 'default' settings
```
Expand Down Expand Up @@ -129,14 +129,14 @@ parameters appended to the URL):
Besides the basic JSON API features, below are some additional Bolt specific
queries that you may find useful:

| URL | Description |
|-------------------------|-------------------------------------------------------------|
|`/{ct}/search?q={query}` | Searches for `{query}` in a specific `contenttype`. |
|`/search?q={query}` | Searches for `{query}` in all contenttypes. |
|`/menu` | Returns a list of all menus defined in `menu.yml`. |
|`/menu?q={name}` | Returns the menu with the specified name. |
|`/taxonomy` | Returns a list of all taxonomies defined in `taxonomy.yml`. |
|`/taxonomy?q={name}` | Returns the taxonomy with the specified name. |
| URL | Description |
|---------------------------------------|-------------------------------------------------------------|
|`/{ct}/search?q={query}` | Searches for `{query}` in a specific `contenttype`. |
|`/search?q={query}` | Searches for `{query}` in all contenttypes. |
|`/menu` | Returns a list of all menus defined in `menu.yml`. |
|`/menu?q={name}` or `/menu/{name}` | Returns the menu with the specified name. |
|`/taxonomy` | Returns a list of all taxonomies defined in `taxonomy.yml`. |
|`/taxonomy?q={name}` or `/menu/{name}` | Returns the taxonomy with the specified name. |

### Road Map

Expand All @@ -151,4 +151,4 @@ queries that you may find useful:
* Handle select-contenttype fields.
* Add hooks for handling specific fieldtypes.
* Add i18n for `detail` field in error messages.
* Add integration/hooks with other extensions, such as [`RelatedContentByTags`](https://github.com/xiaohutai/bolt-relatedcontentbytags).
* Add integration/hooks with other extensions.
85 changes: 73 additions & 12 deletions src/Action/MenuAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Bolt\Extension\Bolt\JsonApi\Action;

use Bolt\Config;
use Bolt\Config as BoltConfig;
use Bolt\Extension\Bolt\JsonApi\Config\Config;
use Bolt\Extension\Bolt\JsonApi\Exception\ApiNotFoundException;
use Bolt\Extension\Bolt\JsonApi\Helpers\DataLinks;
use Bolt\Extension\Bolt\JsonApi\Response\ApiResponse;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -14,22 +16,27 @@
*/
class MenuAction
{
/** @var Config $config */
/** @var BoltConfig $config */
protected $boltConfig;

/** @var \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig */
/** @var Config $extensionConfig */
protected $extensionConfig;

/** @var DataLinks $dataLinks */
protected $dataLinks;

/**
* MenuAction constructor.
*
* @param Config $boltConfig
* @param \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig
* @param BoltConfig $boltConfig
* @param Config $extensionConfig
* @param DataLinks $dataLinks
*/
public function __construct(Config $boltConfig, \Bolt\Extension\Bolt\JsonApi\Config\Config $extensionConfig)
public function __construct(BoltConfig $boltConfig, Config $extensionConfig, DataLinks $dataLinks)
{
$this->boltConfig = $boltConfig;
$this->extensionConfig = $extensionConfig;
$this->dataLinks = $dataLinks;
}

/**
Expand All @@ -39,20 +46,74 @@ public function __construct(Config $boltConfig, \Bolt\Extension\Bolt\JsonApi\Con
*/
public function handle(Request $request)
{
$singleMenu = '';
if ($name = $request->get('q', '')) {
$name = "/$name";
$data = $this->singleMenu($name);
$singleMenu = "/$name";
} else {
$data = $this->allMenus();
}

$menu = $this->boltConfig->get('menu' . $name, false);

return new ApiResponse([
'links' => [
'self' => $this->extensionConfig->getBasePath() .
"/menu" .
$singleMenu .
$this->dataLinks->makeQueryParameters($request->query->all())
],
'data' => $data,
], $this->extensionConfig);
}

/**
* @param string $name
*
* @return array
*/
private function singleMenu($name)
{
$menu = $this->boltConfig->get('menu/' . $name, false);

if (! $menu) {
throw new ApiNotFoundException(
"Menu with name [$name] not found."
);
}

return new ApiResponse([
'data' => $menu,
], $this->extensionConfig);
return $this->parseMenu($name, $menu);
}

/**
* @return array
*/
private function allMenus()
{
$menus = $this->boltConfig->get('menu', false);

return array_map(
[$this, 'parseMenu'],
array_keys($menus),
array_values($menus)
);
}

/**
* @param string $name
* @param array $menu
*
* @return array
*/
private function parseMenu($name, $menu)
{
return [
'id' => $name,
'type' => 'menu',
'attributes' => [
'items' => $menu,
],
'links' => [
'self' => $this->extensionConfig->getBasePath() . "/menu/" . $name
]
];
}
}
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;
}
}
3 changes: 3 additions & 0 deletions src/Controllers/ContentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ public function connect(Application $app)
$ctr->get('/menu', [$this, 'menu'])
->bind('jsonapi.menu');

$ctr->get('/menu/{q}', [$this, 'menu'])
->bind('jsonapi.singleMenu');

$ctr->get('/taxonomy', [$this, 'taxonomy'])
->bind('jsonapi.taxonomy');

Expand Down
16 changes: 15 additions & 1 deletion 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,11 +22,15 @@ class Parser
/** @var MetadataDriver $metadata */
protected $metadata;

public function __construct(Config $config, ResourceManager $resourceManager, MetadataDriver $metadata)
/** @var Users $users */
protected $users;

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

public function parseItem($item, $fields = [])
Expand Down Expand Up @@ -90,6 +95,15 @@ public function parseItem($item, $fields = [])
$values['attributes'] = $attributes;
}

if ($this->config->isEnableDisplayNames() && 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
6 changes: 4 additions & 2 deletions src/Provider/APIProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ function ($app) {
return new Parser(
$app['jsonapi.config'],
$app['resources'],
$app['storage.metadata']
$app['storage.metadata'],
$app['users']
);
}
);
Expand Down Expand Up @@ -140,7 +141,8 @@ function ($app) {
function ($app) {
return new MenuAction(
$app['config'],
$app['jsonapi.config']
$app['jsonapi.config'],
$app['jsonapi.datalinks']
);
}
);
Expand Down