Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEATURE] New DataProcessor: ExtractPropertyProcessor #544

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Classes/DataProcessing/ExtractPropertyProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/*
* This file is part of the "headless" Extension for TYPO3 CMS.
*
* For the full copyright and license information, please read the
* LICENSE.md file that was distributed with this source code.
*/

declare(strict_types=1);

namespace FriendsOfTYPO3\Headless\DataProcessing;

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
use TYPO3\CMS\Frontend\ContentObject\DataProcessorInterface;

/**
* Extract a single (maybe nested) property from a given array
*
* Example:
* lib.meta.fields.ogImage = TEXT
* lib.meta.fields.ogImage {
* dataProcessing {
* 10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
* ...
*
* 20 = FriendsOfTYPO3\Headless\DataProcessing\ExtractPropertyProcessor
* 20.key = media.publicUrl
* 20.as = media
* }
* }
*/
class ExtractPropertyProcessor implements DataProcessorInterface
{
/**
* Extract a single (maybe nested) property from a given array
*
* @param ContentObjectRenderer $cObj The content object renderer, which contains data of the content element
* @param array $contentObjectConfiguration The configuration of Content Object
* @param array $processorConfiguration The configuration of this processor
* @param array $processedData Key/value store of processed data (e.g. to be passed to a Fluid View)
* @return array the processed data as key/value store
*/
public function process(
ContentObjectRenderer $cObj,
array $contentObjectConfiguration,
array $processorConfiguration,
array $processedData
) {
if (empty($processorConfiguration['as'])) {
throw new \Exception('Please specify property \'as\'');
}

if (empty($processorConfiguration['key'])) {
throw new \Exception('Please specify property \'key\'');
}

$targetFieldName = (string)$cObj->stdWrapValue(
'as',
$processorConfiguration
);

$key = GeneralUtility::trimExplode('.', $processorConfiguration['key'], true);

// Extract (nested) property
do {
$processedData = $processedData[array_shift($key)] ?? null;
} while (count($key));

return [
$targetFieldName => $processedData
];
}
}
28 changes: 28 additions & 0 deletions Documentation/Developer/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,34 @@ Here's an example of how to override the meta object by data from a DB record:
}
}

.. _developer-dataprocessors:

TypoScript DataProcessors
=========================

This extension provides a couple of handy DataProcessors. Have a look into the default TypoScript to see them in action.

Here's an example demonstrating their usage.

.. code-block:: typoscript

lib.meta.fields.ogImage = TEXT
lib.meta.fields.ogImage {
dataProcessing {
# Use the column 'og_image' to render an array with all relevant
# information (such as the publicUrl)
10 = FriendsOfTYPO3\Headless\DataProcessing\FilesProcessor
10.as = media
10.references.fieldName = og_image
10.processingConfiguration.returnFlattenObject = 1

# Extract only property 'publicUrl' from the above created array
20 = FriendsOfTYPO3\Headless\DataProcessing\ExtractPropertyProcessor
20.key = media.publicUrl
20.as = media
}
}

.. _developer-ext-form:

EXT:form & form output decorators
Expand Down