Skip to content

Commit

Permalink
Merge pull request #1057 from bolt/feature/localize-endpoints
Browse files Browse the repository at this point in the history
Feature/localize endpoints
  • Loading branch information
bobdenotter committed Feb 12, 2020
2 parents ed4032f + 9445c9c commit a2eda1b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/Entity/Content.php
Expand Up @@ -359,7 +359,7 @@ public function getFieldValues(bool $parsed = true): array
{
$fieldValues = [];
foreach ($this->getFields() as $field) {
$fieldValues[$field->getName()] = $parsed ? $field->getParsedValue() : $field->getValue();
$fieldValues[$field->getName()] = $field->getApiValue();
}

return $fieldValues;
Expand Down
23 changes: 23 additions & 0 deletions src/Entity/Field.php
Expand Up @@ -4,13 +4,16 @@

namespace Bolt\Entity;

use ApiPlatform\Core\Annotation\ApiFilter;
use ApiPlatform\Core\Annotation\ApiResource;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Filter\SearchFilter;
use Bolt\Configuration\Content\FieldType;
use Bolt\Utils\Sanitiser;
use Doctrine\ORM\Mapping as ORM;
use Knp\DoctrineBehaviors\Contract\Entity\TranslatableInterface;
use Knp\DoctrineBehaviors\Model\Translatable\TranslatableTrait;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Annotation\SerializedName;
use Tightenco\Collect\Support\Collection as LaravelCollection;
use Twig\Markup;

Expand All @@ -21,6 +24,7 @@
* "normalization_context"={"groups"={"get_field"}}
* }
* })
* @ApiFilter(SearchFilter::class)
* @ORM\Entity(repositoryClass="Bolt\Repository\FieldRepository")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn(name="type", type="string", length=191)
Expand Down Expand Up @@ -146,7 +150,26 @@ public function get($key)

/**
* @Groups("get_field")
* @SerializedName("value")
*/
public function getApiValue()
{
if (! $this->isTranslatable()) {
return $this->getParsedValue();
}

$result = [];

foreach ($this->getTranslations() as $translation) {
$locale = $translation->getLocale();
$this->setCurrentLocale($locale);
$value = $this->getParsedValue();
$result[$locale] = $value;
}

return $result;
}

public function getValue(): ?array
{
return $this->translate($this->getCurrentLocale(), ! $this->isTranslatable())->getValue();
Expand Down
21 changes: 16 additions & 5 deletions tests/api/get_content.feature
Expand Up @@ -16,7 +16,9 @@ Feature: Get content with API
"publishedAt": "@string@.isDateTime()",
"authorName": @string@,
"fieldValues": {
"title": @string@,
"title": {
"en": @string@
},
"slug": @string@,
"image": {
"filename": @string@,
Expand Down Expand Up @@ -58,7 +60,9 @@ Feature: Get content with API
"publishedAt": "@string@.isDateTime()",
"authorName": @string@,
"fieldValues": {
"title": @string@,
"title": {
"en": @string@
},
"slug": @string@,
"image": {
"filename": @string@,
Expand Down Expand Up @@ -105,7 +109,9 @@ Feature: Get content with API
"publishedAt": "@string@.isDateTime()",
"authorName": @string@,
"fieldValues": {
"title": @string@,
"title": {
"en": @string@
},
"slug": @string@,
"image": {
"filename": @string@,
Expand Down Expand Up @@ -165,7 +171,9 @@ Feature: Get content with API
"publishedAt": "@string@.isDateTime()",
"authorName": @string@,
"fieldValues": {
"title": @string@,
"title": {
"en": @string@
},
"slug": @string@,
"image": {
"filename": @string@,
Expand Down Expand Up @@ -216,12 +224,15 @@ Feature: Get content with API
"@type": "Content",
"id": 1,
"contentType": @string@,
"status": @string@,
"createdAt": "@string@.isDateTime()",
"modifiedAt": "@string@.isDateTime()",
"publishedAt": "@string@.isDateTime()",
"authorName": @string@,
"fieldValues": {
"title": @string@,
"title": {
"en": @string@
},
"slug": @string@,
"image": {
"filename": @string@,
Expand Down
8 changes: 5 additions & 3 deletions tests/api/get_content_fields.feature
Expand Up @@ -7,8 +7,9 @@ Feature: Get content fields with API
And the response should be in JSON
And the response should contain json:
"""
"@array@.repeat({\"name\": \"@string@\", \"type\": \"@string@\", \"value\": \"@array@\"})"
"@array@.repeat({\"name\": \"@string@\", \"type\": \"@string@\", \"value\": \"@*@\"})"
"""

@api
Scenario: As a user I fetch fields of content in JSON+LD format
When I send a GET request to "/api/contents/1/fields.jsonld"
Expand All @@ -20,7 +21,8 @@ Feature: Get content fields with API
"@context": "/api/contexts/Field",
"@id": "/api/contents/1/fields",
"@type": "hydra:Collection",
"hydra:member": "@array@.repeat({\"@id\": \"@string@\", \"@type\": \"@string@\", \"name\": \"@string@\", \"type\": \"@string@\", \"value\": \"@array@\"})",
"hydra:totalItems": @integer@
"hydra:member": "@array@.repeat({\"@id\": \"@string@\", \"@type\": \"@string@\", \"name\": \"@string@\", \"type\": \"@string@\", \"value\": \"@*@\"})",
"hydra:totalItems": @integer@,
"@*@": "@*@"
}
"""

0 comments on commit a2eda1b

Please sign in to comment.