‪TYPO3CMS  11.5
TYPO3\CMS\Extbase\Mvc\View\JsonView Class Reference
Inheritance diagram for TYPO3\CMS\Extbase\Mvc\View\JsonView:
TYPO3\CMS\Extbase\Mvc\View\AbstractView TYPO3\CMS\Extbase\Mvc\View\ViewInterface TYPO3\CMS\Extbase\Mvc\View\ViewInterface

Public Member Functions

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

Public Attributes

const EXPOSE_CLASSNAME_FULLY_QUALIFIED = 1
 
const EXPOSE_CLASSNAME_UNQUALIFIED = 2
 

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
 
ControllerContext $controllerContext
 
array $variables = array( )
 
- ‪Protected Attributes inherited from ‪TYPO3\CMS\Extbase\Mvc\View\AbstractView
TYPO3 CMS Extbase Mvc Controller ControllerContext $controllerContext
 
array $variables = array( )
 

Detailed Description

A JSON view

Todo:
‪v12: Drop 'implements ViewInterface' together with removal of extbase ViewInterface

Definition at line 30 of file JsonView.php.

Member Function Documentation

◆ assign()

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

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

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

Reimplemented from TYPO3\CMS\Extbase\Mvc\View\AbstractView.

Definition at line 201 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.

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

Reimplemented from TYPO3\CMS\Extbase\Mvc\View\AbstractView.

Definition at line 213 of file JsonView.php.

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

◆ canRender()

bool TYPO3\CMS\Extbase\Mvc\View\JsonView::canRender ( )

Tells if the view implementation can render the view for the given context.

By default we assume that the view implementation can handle all kinds of contexts. Override this method if that is not the case.

Returns
‪bool TRUE if the view has something useful to display, otherwise FALSE
Deprecated:
‪since TYPO3 v11, will be removed in v12. Legacy method, not part of ViewInterface anymore.

Reimplemented from TYPO3\CMS\Extbase\Mvc\View\AbstractView.

Definition at line 230 of file JsonView.php.

◆ initializeView()

TYPO3\CMS\Extbase\Mvc\View\JsonView::initializeView ( )

Initializes this view.

Override this method for initializing your concrete view implementation.

Deprecated:
‪since v11, will be removed with v12. Drop together with removal of extbase ViewInterface.

Reimplemented from TYPO3\CMS\Extbase\Mvc\View\AbstractView.

Definition at line 242 of file JsonView.php.

◆ injectPersistenceManager()

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

Definition at line 177 of file JsonView.php.

References TYPO3\CMS\Extbase\Mvc\View\JsonView\$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.

Returns
‪string The JSON encoded variables

Implements TYPO3\CMS\Extbase\Mvc\View\ViewInterface.

Definition at line 288 of file JsonView.php.

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

◆ renderArray()

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

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

Returns
‪mixed

Definition at line 300 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,
  $sectionName,
array  $variables,
  $ignoreUnknown = false 
)

Definition at line 275 of file JsonView.php.

◆ renderSection()

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

Definition at line 266 of file JsonView.php.

◆ setConfiguration()

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

Definition at line 258 of file JsonView.php.

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

◆ setControllerContext()

TYPO3\CMS\Extbase\Mvc\View\JsonView::setControllerContext ( ControllerContext  $controllerContext)

Sets the current controller context

Parameters
ControllerContext$controllerContext
Deprecated:
‪since v11, will be removed with v12.

Reimplemented from TYPO3\CMS\Extbase\Mvc\View\AbstractView.

Definition at line 188 of file JsonView.php.

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

◆ 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

Parameters
array$variablesToRender

Definition at line 250 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 
)
protected

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

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

Definition at line 364 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 
)
protected

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

Parameters
mixed$value‪The value to transform
array$configurationConfiguration for transforming the value
bool$firstLevel
Returns
‪mixed The transformed value

Definition at line 328 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( )
protected

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 155 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().

◆ $controllerContext

ControllerContext TYPO3\CMS\Extbase\Mvc\View\JsonView::$controllerContext
protected
Deprecated:
‪since v11, will be removed with v12.

Definition at line 164 of file JsonView.php.

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

◆ $currentVariable

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

Definition at line 56 of file JsonView.php.

◆ $persistenceManager

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

◆ $variables

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

View variables and their values

See also
assign()

Definition at line 171 of file JsonView.php.

◆ $variablesToRender

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

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

Definition at line 52 of file JsonView.php.

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

◆ EXPOSE_CLASSNAME_FULLY_QUALIFIED

const TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_FULLY_QUALIFIED = 1

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 40 of file JsonView.php.

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

◆ EXPOSE_CLASSNAME_UNQUALIFIED

const TYPO3\CMS\Extbase\Mvc\View\JsonView::EXPOSE_CLASSNAME_UNQUALIFIED = 2

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

Definition at line 46 of file JsonView.php.

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