Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I need to filter out some fields from all resources returned by a given query. Which fields are to be filtered out is a dynamic information defined in the TYPO3 BE using a metadata table that defines properties of other tables (like which field of which table should be filtered out, depending on FE user rights).

Since it is something dynamic, I can't rely on properties annotations in the Extbase model (not sure if they exist anyway).

What would be possible ways to achieve this?

You must be logged in to vote

Hi. Your question is related to serialization (normalization) process.

First of all I'm leaving here link to documentation. Unfortunately we didn't have yet time to fill the section about serialization but maybe if someone read that topic in the future it will be already filled: https://docs.typo3.org/p/sourcebroker/t3api/master/en-us/Serialization/Index.html

And now regarding your question - I think that there are few ways to go:

  1. First one is about using normalizationContext and groups. In configuration for the endpoint you can define groups which determines if property should be included in. If any group is defined in normalizationContext then only properties with this group are includ…

Replies: 1 comment · 1 reply

Comment options

Hi. Your question is related to serialization (normalization) process.

First of all I'm leaving here link to documentation. Unfortunately we didn't have yet time to fill the section about serialization but maybe if someone read that topic in the future it will be already filled: https://docs.typo3.org/p/sourcebroker/t3api/master/en-us/Serialization/Index.html

And now regarding your question - I think that there are few ways to go:

  1. First one is about using normalizationContext and groups. In configuration for the endpoint you can define groups which determines if property should be included in. If any group is defined in normalizationContext then only properties with this group are included in response. According to example below:
use SourceBroker\T3api\Annotation as T3api;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

/**
 * @T3api\ApiResource (
 *     collectionOperations={
 *          "get"={
 *              "path"="/entities",
 *              "normalizationContext"={
 *                  "groups"={"api_entity_get_collection"}
 *              },
 *          },
 *     },
 * )
 */
class MyEntity extends AbstractEntity
{
    /**
     * @var string
     */
    protected $property1;

    /**
     * @var string
     *
     * @T3api\Serializer\Groups({
     *     "api_entity_get_collection",
     * })
     */
    protected $property2;

We added normalization groupapi_entity_get_collection and now in the endpoint /_api/entities only property2 will be included (property1 will simply not occur in the response and its getter won't be called at all).

1a. Additionally to first point - there might be situation when you would like to add dynamic groups. To achieve that you could use signal \SourceBroker\T3api\Serializer\ContextBuilder\ContextBuilderInterface::SIGNAL_CUSTOMIZE_SERIALIZER_CONTEXT_ATTRIBUTES (https://docs.typo3.org/p/sourcebroker/t3api/master/en-us/Serialization/ContextGroups/Index.html#customization). One of the properties in this signal is attributes. You can enrich it with your custom groups and return. Then simply you can use this dynamic group name in the T3api\Serializer\Groups annotation for property/getter.

  1. This solution is probably the one most matching to your case. You can use special ExcludeIf annotation for serializtion to exclude property if condition matches. For example:
use SourceBroker\T3api\Annotation as T3api;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

class MyEntity extends AbstractEntity
{
    /**
     * @var string
     */
    protected $property1;

    /**
     * @var string
     *
     * @T3api\Serializer\Groups({
     *     "api_entity_get_collection",
     * })
     * @T3api\Serializer\Exclude(if="object.getProperty1() > 2")
     */
    protected $property2;
}

Now property2 will be included in response only if Entity::getProperty1() returns value higher than 2. Conditions inside if supports standard Symfony/TYPO3 expression language. You can even use there your custom expressions which depends e.g. on services with TYPO3's dependency injection just by registering your own expression language functions in standard TYPO3 way. Inside expression language for Exclude annotation there is available keyword object which refers to current entity.

Example usage with custom function provider:

Expression Function Provider:

namespace Vendor\Extension\ExpressionLanguage;

use Symfony\Component\ExpressionLanguage\ExpressionFunction;
use Symfony\Component\ExpressionLanguage\ExpressionFunctionProviderInterface;

class ExampleFunctionsProvider implements ExpressionFunctionProviderInterface
{
    /**
     * @return ExpressionFunction[]
     */
    public function getFunctions(): array
    {
        return [
            $this->getCurrentTimestamp(),
        ];
    }

    protected function getCurrentTimestamp(): ExpressionFunction
    {
        return new ExpressionFunction(
            'current_timestamp',
            static function () {
            },
            static function (): int {
                return time();
            }
        );
    }
}

Entity:

namespace Vendor\Extension\Domain\Model;

use SourceBroker\T3api\Annotation as T3api;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;

/**
 * @T3api\ApiResource (
 *     collectionOperations={
 *          "get"={
 *              "path"="/entities",
 *          },
 *     },
 * )
 */
class MyEntity extends AbstractEntity
{
    /**
     * @var string
     *
     * @T3api\Serializer\Exclude(if="current_timestamp() > 1634205655")
     */
    protected $property2;
}
You must be logged in to vote
1 reply
@fsuter
Comment options

Thanks for the detailed answer. I'll dig into that.

Answer selected by klamparski
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.