Skip to content

Commit

Permalink
Early if for JSON (#626)
Browse files Browse the repository at this point in the history
* [FEATURE] Provide early if for JSON cObject

* Adjust example

* Add some documentation for headless cObjects

* Add docs for JSON_CONTENT

* Add two simple tests for JSON.if
  • Loading branch information
kitzberger authored Aug 30, 2023
1 parent 62c7700 commit 538ff97
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 7 deletions.
17 changes: 11 additions & 6 deletions Classes/ContentObject/JsonContentContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@
use const JSON_FORCE_OBJECT;

/**
* CONTENT_JSON Content object behaves & has the same options as standard TYPO3' Content
* main difference is content is grouped by colPol field & encoded into JSON by default.
* This cObject basically behaves like TYPO3's CONTENT,
* the main difference is that content elements are
* grouped by colPol & encoded into JSON by default.
*
* CONTENT_JSON has the same options as CONTENT, also adds two new options for edge cases in json context
* CONTENT_JSON has the same options as CONTENT but also
* offers two new options for edge cases in json context.
*
* ** merge ** option
* New option allows to generate another CONTENT_JSON call in one definition & then merge both results into one dataset
* (useful for handling slide feature of CONTENT cObject)
* This option allows to generate another CONTENT_JSON call
* in one definition & then merge both results into one
* dataset (useful for handling slide feature of CONTENT cObject).
*
* for example:
*
Expand All @@ -57,7 +60,8 @@
* }
*
* ** doNotGroupByColPos = 0(default)|1 **
* Option allows return of flat array (without grouping by colPos) encoded into JSON
* This option allows to return a flat array (without grouping
* by colPos) but still encoded into JSON.
*
* lib.content = CONTENT_JSON
* lib.content {
Expand All @@ -67,6 +71,7 @@
* where = {#colPos} != 1
* }
* doNotGroupByColPos = 1
* }
*/
class JsonContentContentObject extends ContentContentObject
{
Expand Down
4 changes: 4 additions & 0 deletions Classes/ContentObject/JsonContentObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public function __construct(ContentDataProcessor $contentDataProcessor = null)
*/
public function render($conf = []): string
{
if (!empty($conf['if.']) && !$this->cObj->checkIf($conf['if.'])) {
return '';
}

$data = [];

if (!is_array($conf)) {
Expand Down
107 changes: 106 additions & 1 deletion Documentation/Developer/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,111 @@ This chapter will explain different usecases for developer working with `headles

.. _developer-plugin-extbase:

New cObjects
============

EXT:headless comes with a bunch of new cObjects to be used via TypoScript:

* BOOL
* FLOAT
* INT
* JSON
* JSON_CONTENT

`BOOL`, `FLOAT` and `INT` are basically like `TEXT` (with `value` and `stdWrap` properties!) but make sure their result is being cast to bool, float or int.

JSON
----

To build and render a JSON object into your page output.

.. code-block:: typoscript
lib.meta = JSON
lib.meta {
if.isTrue = 1
fields {
title = TEXT
title {
field = seo_title
stdWrap.ifEmpty.cObject = TEXT
stdWrap.ifEmpty.cObject {
field = title
}
}
robots {
fields {
noIndex = BOOL
noIndex.field = no_index
}
}
ogImage = TEXT
ogImage {
dataProcessing {
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10 {
as = media
references.fieldName = og_image
processingConfiguration {
returnFlattenObject = 1
}
}
}
}
}
dataProcessing {
}
stdWrap {
}
}
JSON_CONTENT
------------

This cObject basically behaves like TYPO3's `CONTENT`, the main difference is that content elements are grouped by `colPol` & encoded into JSON by default.

`CONTENT_JSON` has the same options as `CONTENT` but also offers two new options for edge cases in json context.

**merge**

This option allows to generate another `CONTENT_JSON` call in one definition & then merge both results into one dataset
(useful for handling slide feature of CONTENT cObject).

.. code-block:: typoscript
lib.content = CONTENT_JSON
lib.content {
table = tt_content
select {
orderBy = sorting
where = {#colPos} != 1
}
merge {
table = tt_content
select {
orderBy = sorting
where = {#colPos} = 1
}
slide = -1
}
}
**doNotGroupByColPos = 0(default)|1**

This option allows to return a flat array (without grouping by colPos) but still encoded into JSON.

.. code-block:: typoscript
lib.content = CONTENT_JSON
lib.content {
table = tt_content
select {
orderBy = sorting
where = {#colPos} != 1
}
doNotGroupByColPos = 1
}
Internal Extbase plugins
========================

Expand Down Expand Up @@ -274,7 +379,7 @@ Here's an example of how to override the meta object by data from a DB record:
lib.meta.stdWrap.override.cObject = JSON
lib.meta.stdWrap.override.cObject {
stdWrap.if.isTrue.data = GP:tx_news_pi1|news
if.isTrue.data = GP:tx_news_pi1|news
dataProcessing.10 = FriendsOfTYPO3\Headless\DataProcessing\DatabaseQueryProcessor
dataProcessing.10 {
table = tx_news_domain_model_news
Expand Down
2 changes: 2 additions & 0 deletions Tests/Unit/ContentObject/JsonContentObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ public function dataProvider(): array
return [
[[], '[]'],
[null, '[]'],
[['if.' => ['isTrue' => 0], 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], ''],
[['if.' => ['isTrue' => 1], 'fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], json_encode(['test' => '1'])],
[['stdWrap.' => ['wrap' => '{"wrapped":|}']], json_encode(['wrapped' => []])],
[['dataProcessing.' => ['10' => 'FriendsOfTYPO3\Headless\Tests\Unit\ContentObject\DataProcessingExample', '10.' => ['as' => 'sites']]], json_encode(['SomeCustomProcessing'])],
[['fields.' => ['test' => 'TEXT', 'test.' => ['value' => '1']]], json_encode(['test' => '1'])],
Expand Down

0 comments on commit 538ff97

Please sign in to comment.