Skip to content

Commit b0bebca

Browse files
author
Bertrand Dunogier
committed
Added an 'html' property to field values, that renders the field value to HTML
Implemented HTML parameters for Image, MapLocation
1 parent c4e4f52 commit b0bebca

5 files changed

Lines changed: 133 additions & 2 deletions

File tree

GraphQL/Resolver/ContentResolver.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
use eZ\Publish\API\Repository\ContentService;
1010
use eZ\Publish\API\Repository\ContentTypeService;
1111
use eZ\Publish\API\Repository\SearchService;
12-
use eZ\Publish\API\Repository\Values\Content\Content;
1312
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
1413
use eZ\Publish\API\Repository\Values\Content\Field;
1514
use eZ\Publish\API\Repository\Values\Content\Query;
16-
use eZ\Publish\API\Repository\Values\Content\Relation;
1715
use eZ\Publish\API\Repository\Values\Content\Search\SearchHit;
1816
use Overblog\GraphQLBundle\Resolver\TypeResolver;
1917

@@ -161,6 +159,7 @@ function(Field $field) use ($content) {
161159
'contentTypeId' => $content->contentInfo->contentTypeId,
162160
'fieldDefIdentifier' => $field->fieldDefIdentifier,
163161
'value' => $field->value,
162+
'content' => $content,
164163
]
165164
),
166165
'fieldDefIdentifier' => $field->fieldDefIdentifier,
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: bdunogier
5+
* Date: 14/09/2018
6+
* Time: 12:55
7+
*/
8+
9+
namespace BD\EzPlatformGraphQLBundle\GraphQL\Resolver;
10+
11+
use BD\EzPlatformGraphQLBundle\GraphQL\Value\ContentFieldValue;
12+
use eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FieldRenderingExtension;
13+
use Overblog\GraphQLBundle\Definition\Argument;
14+
15+
class FieldValueHtmlResolver
16+
{
17+
/**
18+
* @var FieldRenderingExtension
19+
*/
20+
private $fieldRenderer;
21+
22+
public function __construct(FieldRenderingExtension $fieldRenderer)
23+
{
24+
$this->fieldRenderer = $fieldRenderer;
25+
}
26+
27+
public function resolveFieldValueToHtml(ContentFieldValue $value, Argument $args)
28+
{
29+
return $this->fieldRenderer->renderField(
30+
$value->content,
31+
$value->fieldDefIdentifier,
32+
$args->getRawArguments());
33+
}
34+
}

GraphQL/Value/ContentFieldValue.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace BD\EzPlatformGraphQLBundle\GraphQL\Value;
77

8+
use eZ\Publish\API\Repository\Values\Content\Content;
89
use eZ\Publish\API\Repository\Values\ValueObject;
910

1011
/**
@@ -28,6 +29,11 @@ class ContentFieldValue extends ValueObject
2829
*/
2930
protected $contentTypeId;
3031

32+
/**
33+
* @var \eZ\Publish\API\Repository\Values\Content\Content
34+
*/
35+
protected $content;
36+
3137
/**
3238
* @var \eZ\Publish\Core\FieldType\Value
3339
*/

Resources/config/graphql/Field.types.yml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,39 @@ FieldValue:
2323
text:
2424
type: "String"
2525
description: "String representation of the value"
26+
html:
27+
type: "String"
28+
description: "HTML representation of the value"
29+
args:
30+
template:
31+
type: "String"
32+
description: "Custom field template to use for field rendering"
33+
attributes:
34+
type: "String"
35+
description: "hash of HTML attributes you want to add to the inner markup"
36+
lang:
37+
type: "String"
38+
description: "Override the current language. Must be a valid locale with xxx-YY format"
2639
resolveType: "@=resolver('FieldValueType', [value])"
2740

41+
HTMLFieldValue:
42+
type: object
43+
config:
44+
fields:
45+
html:
46+
type: "String"
47+
description: "HTML representation of the value"
48+
resolve: "@=resolver('FieldValueToHtml', [value, args])"
49+
args:
50+
template:
51+
type: "String"
52+
description: "Custom field template to use for field rendering"
53+
attributes:
54+
type: "String"
55+
description: "hash of HTML attributes you want to add to the inner markup"
56+
lang:
57+
type: "String"
58+
description: "Override the current language. Must be a valid locale with xxx-YY format"
2859

2960
GenericFieldValue:
3061
type: object
@@ -35,6 +66,7 @@ GenericFieldValue:
3566
description: "String representation of the value"
3667
resolve: "@=value"
3768
interfaces: [FieldValue]
69+
inherits: [HTMLFieldValue]
3870

3971
TextLineFieldValue:
4072
type: object
@@ -45,6 +77,7 @@ TextLineFieldValue:
4577
description: "String representation of the value"
4678
resolve: "@=value.text"
4779
interfaces: [FieldValue]
80+
inherits: [HTMLFieldValue]
4881

4982
ImageFieldValue:
5083
type: object
@@ -82,7 +115,22 @@ ImageFieldValue:
82115
type: "[String]"
83116
description: "One or more variation identifiers."
84117
resolve: "@=resolver('ImageVariation', [value.value, args])"
118+
html:
119+
type: "String"
120+
args:
121+
parameters:
122+
type: "ImageFieldValueHtmlParameters"
85123
interfaces: [FieldValue]
124+
inherits: [HTMLFieldValue]
125+
126+
ImageFieldValueHtmlParameters:
127+
type: "input-object"
128+
config:
129+
fields:
130+
alias: {type: "String"}
131+
width: {type: "String"}
132+
height: {type: "String"}
133+
class: {type: "String"}
86134

87135
ImageVariation:
88136
type: object
@@ -113,6 +161,8 @@ AuthorFieldValue:
113161
authors:
114162
type: "[Author]"
115163
resolve: "@=value.authors"
164+
inherits: [HTMLFieldValue]
165+
116166
Author:
117167
type: object
118168
config:
@@ -133,6 +183,7 @@ BinaryFileFieldValue:
133183
type: "String"
134184
description: "String representation of the value"
135185
resolve: "@=value"
186+
inherits: [HTMLFieldValue]
136187

137188
CheckboxFieldValue:
138189
type: object
@@ -143,6 +194,7 @@ CheckboxFieldValue:
143194
type: "String"
144195
description: "String representation of the value"
145196
resolve: "@=value"
197+
inherits: [HTMLFieldValue]
146198

147199
CountryFieldValue:
148200
type: object
@@ -153,6 +205,7 @@ CountryFieldValue:
153205
type: "String"
154206
description: "String representation of the value"
155207
resolve: "@=value"
208+
inherits: [HTMLFieldValue]
156209

157210
DateFieldValue:
158211
type: object
@@ -172,12 +225,14 @@ DateFieldValue:
172225
format:
173226
type: String
174227
description: "A format string compatible with PHP's date() function"
228+
inherits: [HTMLFieldValue]
175229

176230
DateAndTimeFieldValue:
177231
type: object
178232
inherits: ["DateFieldValue"]
179233
config:
180234
interfaces: [FieldValue]
235+
inherits: [HTMLFieldValue]
181236

182237
EmailAddressFieldValue:
183238
type: object
@@ -188,6 +243,7 @@ EmailAddressFieldValue:
188243
type: "String"
189244
description: "String representation of the value"
190245
resolve: "@=value.email"
246+
inherits: [HTMLFieldValue]
191247

192248
FloatFieldValue:
193249
type: object
@@ -198,6 +254,7 @@ FloatFieldValue:
198254
type: "String"
199255
description: "String representation of the value"
200256
resolve: "@=value.value"
257+
inherits: [HTMLFieldValue]
201258

202259
IntegerFieldValue:
203260
type: object
@@ -208,6 +265,7 @@ IntegerFieldValue:
208265
type: "String"
209266
description: "String representation of the value"
210267
resolve: "@=value.value"
268+
inherits: [HTMLFieldValue]
211269

212270
KeywordFieldValue:
213271
type: object
@@ -219,6 +277,7 @@ KeywordFieldValue:
219277
description: "String representation of the value"
220278
# @todo will fail, array to string conversion
221279
resolve: "@=value"
280+
inherits: [HTMLFieldValue]
222281

223282
MapLocationFieldValue:
224283
type: object
@@ -238,6 +297,26 @@ MapLocationFieldValue:
238297
address:
239298
type: "String"
240299
resolve: "@=value.address"
300+
html:
301+
type: "String"
302+
args:
303+
params:
304+
type: "MapLocationHtmlParameters"
305+
306+
inherits: [HTMLFieldValue]
307+
308+
MapLocationHtmlParameters:
309+
type: "input-object"
310+
config:
311+
fields:
312+
draggable: { type: "Boolean" }
313+
height: { type: "String", description: "The height of the rendered map with its unit (for example \"200px\" or \"20em\")." }
314+
width: { type: "String", description: "The width of the rendered map with its unit (for example \"500px\" or \"50em\")." }
315+
mapType: { type: "String", description: "One of the GMap types of map (https://developers.google.com/maps/documentation/javascript/maptypes#BasicMapTypes)" }
316+
scrollWheel: { type: "Boolean", description: "Allows you to disable scroll wheel starting to zoom when mouse comes over the map as user scrolls down a page." }
317+
showInfo: { type: "Boolean", description: "Whether to show a latitude, longitude and the address outside of the map" }
318+
showMap: { type: "Boolean", description: "Whether to show a Map" }
319+
zoom: { type: "Int", description: "The initial zoom level. Default: 13" }
241320

242321
MediaFieldValue:
243322
type: object
@@ -268,6 +347,7 @@ MediaFieldValue:
268347
type: "Int"
269348
description: "Width of the media."
270349
resolve: "@=value.widht"
350+
inherits: [HTMLFieldValue]
271351

272352
PriceFieldValue:
273353
type: object
@@ -278,6 +358,7 @@ PriceFieldValue:
278358
type: "String"
279359
description: "String representation of the value"
280360
resolve: "@=value"
361+
inherits: [HTMLFieldValue]
281362

282363
RatingFieldValue:
283364
type: object
@@ -288,6 +369,7 @@ RatingFieldValue:
288369
type: "String"
289370
description: "String representation of the value"
290371
resolve: "@=value"
372+
inherits: [HTMLFieldValue]
291373

292374
RelationFieldValue:
293375
type: object
@@ -302,6 +384,7 @@ RelationFieldValue:
302384
type: "Content"
303385
description: "The related Content item"
304386
resolve: "@=resolver('ContentById', [value.destinationContentId])"
387+
inherits: [HTMLFieldValue]
305388

306389
RelationListFieldValue:
307390
type: object
@@ -316,6 +399,7 @@ RelationListFieldValue:
316399
type: "[Content]"
317400
description: "The related content items"
318401
resolve: "@=resolver('ContentByIdList', [value.destinationContentIds])"
402+
inherits: [HTMLFieldValue]
319403

320404
RichTextFieldValue:
321405
type: object
@@ -338,6 +422,7 @@ RichTextFieldValue:
338422
type: "String"
339423
description: "Editable HTML5 representation."
340424
resolve: "@=resolver('RichTextXmlToHtml5Edit', [value.xml])"
425+
inherits: [HTMLFieldValue]
341426

342427
SelectionFieldValue:
343428
type: object
@@ -348,6 +433,7 @@ SelectionFieldValue:
348433
type: "String"
349434
description: "String representation of the value"
350435
resolve: "@=value"
436+
inherits: [HTMLFieldValue]
351437

352438
TextBlockFieldValue:
353439
type: object
@@ -358,3 +444,4 @@ TextBlockFieldValue:
358444
type: "String"
359445
description: "String representation of the value"
360446
resolve: "@=value"
447+
inherits: [HTMLFieldValue]

Resources/config/services.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ services:
2828
- { name: overblog_graphql.resolver, alias: "ContentRelations", method: "findContentRelations" }
2929
- { name: overblog_graphql.resolver, alias: "ContentReverseRelations", method: "findContentReverseRelations" }
3030
- { name: overblog_graphql.resolver, alias: "FieldValueType", method: "resolveFieldValueType" }
31+
BD\EzPlatformGraphQLBundle\GraphQL\Resolver\FieldValueHtmlResolver:
32+
arguments:
33+
- "@ezpublish.twig.extension.field_rendering"
34+
tags:
35+
- { name: overblog_graphql.resolver, alias: "FieldValueToHtml", method: "resolveFieldValueToHtml" }
3136

3237
bd_ezplatform_graphql.graph.resolver.user:
3338
class: BD\EzPlatformGraphQLBundle\GraphQL\Resolver\UserResolver

0 commit comments

Comments
 (0)