‪TYPO3CMS  ‪main
TYPO3\CMS\Extbase\Mvc\View\JsonView Class Reference
Inheritance diagram for TYPO3\CMS\Extbase\Mvc\View\JsonView:

Public Member Functions

 injectPersistenceManager (PersistenceManagerInterface $persistenceManager)
self assign ($key, $value)
self assignMultiple (array $values)
 setVariablesToRender (array $variablesToRender)
 setConfiguration (array $configuration)
 renderSection ($sectionName, array $variables=[], $ignoreUnknown=false)
 renderPartial ($partialName, $sectionName, array $variables, $ignoreUnknown=false)
string render ()

Public Attributes


Protected Member Functions

mixed renderArray ()
mixed transformValue ($value, array $configuration, $firstLevel=false)
array string transformObject (object $object, array $configuration)

Protected Attributes

string[] $variablesToRender = array( 'value' )
string $currentVariable = ''
array $configuration = array( )
PersistenceManagerInterface $persistenceManager
array $variables = array( )

Detailed Description

A JSON view

Definition at line 28 of file JsonView.php.

Member Function Documentation

◆ assign()

self TYPO3\CMS\Extbase\Mvc\View\JsonView::assign (   $key,

Add a variable to $this->viewData. Can be chained, so $this->view->assign(..., ...)->assign(..., ...); is possible

string$key‪Key of variable
mixed$value‪Value of object
‪self an instance of $this, to enable chaining

Definition at line 182 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\assignMultiple().

◆ assignMultiple()

self TYPO3\CMS\Extbase\Mvc\View\JsonView::assignMultiple ( array  $values)

Add multiple variables to $this->viewData.

array$values‪array in the format array(key1 => value1, key2 => value2).
‪self an instance of $this, to enable chaining

Definition at line 194 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\assign().

◆ injectPersistenceManager()

TYPO3\CMS\Extbase\Mvc\View\JsonView::injectPersistenceManager ( PersistenceManagerInterface  $persistenceManager)

◆ render()

string TYPO3\CMS\Extbase\Mvc\View\JsonView::render ( )

Transforms the value view variable to a serializable array representation using a YAML view configuration and JSON encodes the result.

‪string The JSON encoded variables

Definition at line 238 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray().

◆ renderArray()

mixed TYPO3\CMS\Extbase\Mvc\View\JsonView::renderArray ( )

Loads the configuration and transforms the value to a serializable array.


Definition at line 250 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\render().

◆ renderPartial()

TYPO3\CMS\Extbase\Mvc\View\JsonView::renderPartial (   $partialName,
array  $variables,
  $ignoreUnknown = false 

Definition at line 225 of file JsonView.php.

◆ renderSection()

TYPO3\CMS\Extbase\Mvc\View\JsonView::renderSection (   $sectionName,
array  $variables = [],
  $ignoreUnknown = false 

Definition at line 219 of file JsonView.php.

◆ setConfiguration()

TYPO3\CMS\Extbase\Mvc\View\JsonView::setConfiguration ( array  $configuration)
array$configuration‪The rendering configuration for this JSON view

Definition at line 214 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration.

◆ setVariablesToRender()

TYPO3\CMS\Extbase\Mvc\View\JsonView::setVariablesToRender ( array  $variablesToRender)

Specifies which variables this JsonView should render By default only the variable 'value' will be rendered

Definition at line 206 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$variablesToRender.

◆ transformObject()

array string TYPO3\CMS\Extbase\Mvc\View\JsonView::transformObject ( object  $object,
array  $configuration 

Traverses the given object structure in order to transform it into an array structure.

object$object‪Object to traverse
array$configurationConfiguration for transforming the given object or NULL
‪array|string Object structure as an array or as a rendered string (for a DateTime instance)

Definition at line 318 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, TYPO3\CMS\Extbase\Reflection\ObjectAccess\getGettablePropertyNames(), TYPO3\CMS\Extbase\Reflection\ObjectAccess\getProperty(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

◆ transformValue()

mixed TYPO3\CMS\Extbase\Mvc\View\JsonView::transformValue (   $value,
array  $configuration,
  $firstLevel = false 

Transforms a value depending on type recursively using the supplied configuration.

mixed$value‪The value to transform
array$configurationConfiguration for transforming the value
‪mixed The transformed value

Definition at line 278 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$configuration, and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject().

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject().

Member Data Documentation

◆ $configuration

array TYPO3\CMS\Extbase\Mvc\View\JsonView::$configuration = array( )

The rendering configuration for this JSON view which determines which properties of each variable to render.

The configuration array must have the following structure:

Example 1:

[ 'variable1' => [ '_only' => ['property1', 'property2', ...] ], 'variable2' => [ '_exclude' => ['property3', 'property4, ...] ], 'variable3' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ '_only' => ['firstName', 'lastName'] ] ] ], 'somearrayvalue' => [ '_descendAll' => [ '_only' => ['property1'] ] ] ]

Of variable1 only property1 and property2 will be included. Of variable2 all properties except property3 and property4 are used. Of variable3 all properties except secretTitle are included.

If a property value is an array or object, it is not included by default. If, however, such a property is listed in a "_descend" section, the renderer will descend into this sub structure and include all its properties (of the next level).

The configuration of each property in "_descend" has the same syntax like at the top level. Therefore - theoretically - infinitely nested structures can be configured.

To export indexed arrays the "_descendAll" section can be used to include all array keys for the output. The configuration inside a "_descendAll" will be applied to each array element.

Example 2: exposing object identifier

[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being a persisted entity '_only' => ['firstName'], '_exposeObjectIdentifier' => TRUE, '_exposedObjectIdentifierKey' => 'guid' ] ] ] ]

Note for entity objects you are able to expose the object's identifier also, just add an "_exposeObjectIdentifier" directive set to TRUE and an additional property '__identity' will appear keeping the persistence identifier. Renaming that property name instead of '__identity' is also possible with the directive "_exposedObjectIdentifierKey". Example 2 above would output (summarized): {"customer":{"firstName":"John","guid":"892693e4-b570-46fe-af71-1ad32918fb64"}}

Example 3: exposing object's class name

[ 'variableFoo' => [ '_exclude' => ['secretTitle'], '_descend' => [ 'customer' => [ // consider 'customer' being an object '_only' => ['firstName'], '_exposeClassName' => \TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_FULLY_QUALIFIED ] ] ] ]

The _exposeClassName is similar to the objectIdentifier one, but the class name is added to the JSON object output, for example (summarized): {"customer":{"firstName":"John","__class":"Acme\Foo\Domain\Model\Customer"}}

The other option is EXPOSE_CLASSNAME_UNQUALIFIED which only will give the last part of the class without the namespace, for example (summarized): {"customer":{"firstName":"John","__class":"Customer"}} This might be of interest to not provide information about the package or domain structure behind.

Definition at line 153 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\renderArray(), TYPO3\CMS\Extbase\Mvc\View\JsonView\setConfiguration(), TYPO3\CMS\Extbase\Mvc\View\JsonView\transformObject(), and TYPO3\CMS\Extbase\Mvc\View\JsonView\transformValue().

◆ $currentVariable

string TYPO3\CMS\Extbase\Mvc\View\JsonView::$currentVariable = ''

Definition at line 54 of file JsonView.php.

◆ $persistenceManager

PersistenceManagerInterface TYPO3\CMS\Extbase\Mvc\View\JsonView::$persistenceManager

◆ $variables

array TYPO3\CMS\Extbase\Mvc\View\JsonView::$variables = array( )

View variables and their values

See also

Definition at line 164 of file JsonView.php.

◆ $variablesToRender

string [] TYPO3\CMS\Extbase\Mvc\View\JsonView::$variablesToRender = array( 'value' )

Only variables whose name is contained in this array will be rendered

Definition at line 50 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Mvc\View\JsonView\setVariablesToRender().



Definition for the class name exposure configuration, that is, if the class name of an object should also be part of the output JSON, if configured.

Setting this value, the object's class name is fully put out, including the namespace.

Definition at line 38 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\JsonViewTest\exposeClassNameSettingsAndResults().



Puts out only the actual class name without namespace. See EXPOSE_CLASSNAME_FULL for the meaning of the constant at all.

Definition at line 44 of file JsonView.php.

Referenced by TYPO3\CMS\Extbase\Tests\Unit\Mvc\View\JsonViewTest\exposeClassNameSettingsAndResults().