Wide banner showcasing playful tech illustrations, including a quirky spider-like character representing API Platform, over a gentle blue and white background.

API Platform: How to Implement Custom Collection Data Providers with Filters?

03/03/2023

In most cases, when you are trying to filter data from your endpoint, default ApiPlatform filters are more than enough to cover all your needs. Sometimes though, additional actions are needed before your collection can be returned to the world.

How to Implement Custom Collection Data Providers with Filters?

This article explains how to implement a custom collection data provider with filters in API Platform version 2.7.4 and is a part of our series of articles on API Platform which you can find on our blog.

Imagine the following scenario: Let's say we have a collection named transactions. We already created endpoints to fetch data GET /api/transactions:

collectionOperations={
    "get"={
        "normalization_context"={"groups"={"transaction:read"}}
    },
}

We also have some filters; for example, employee exact.

>@ApiFilter(SearchFilter::class, properties={"employee"="exact"})

By default when you will call your endpoint with an additional param in the query string project=1, your list will be filtered by only these transactions that are related to a specific project - in this example id 1. The problem starts when you want to do something extra in the DataProvider in this collection. For example, let's filter collection in a data provider based on logged users. If a user has a specific role, then they see all transactions, if they’re a "normal" user then they can only see transactions related to his user.

public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
{
    $queryBuilder = $this->managerRegistry->getRepository(Transaction::class)->createQueryBuilder('t');

    if (!$this->security->isGranted('ROLE_MANAGER')) {
        $queryBuilder
            ->andWhere('ewh.employee = :employee')
            ->setParameter('employee', $this->security->getUser())
        ;
    }

    return $queryBuilder->getQuery()->getResult();
}

However, what’s important to know is that this will remove default filters. Hence, you will not be able to filter by project anymore. What’s the solution?

To force DataProvider to use default filters. In order to do that, we need to inject extensions collection from the api platform.

public function __construct(
    private object $collectionExtensions,
    private ManagerRegistry $managerRegistry,
    private Security $security,
)
{}

Next, we need to inject a specific collection into our service.

App\DataProvider\TransactionsDataProvider:
    tags:
        - { name: 'api_platform.collection_data_provider', priority: 3 }
    autoconfigure: false
    arguments:
        $collectionExtensions: !tagged api_platform.doctrine.orm.query_extension.collection

From this moment forward, we will have all extensions available in our service. Some of them are filters, other ones could be paginations. Now, we can use them to filter our Query by additional criteria - by project in our example. To do that let's use the following code where we are applying each extension.

$queryNameGenerator = new QueryNameGenerator();

/** @var QueryCollectionExtensionInterface $extension */
foreach ($this->collectionExtensions as $extension) {
    $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);

    if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
        return $extension->getResult($queryBuilder);
    }
}

Now, we can finally call our endpoint to check if everything works well. All class including all previous code snippets is available here:

<?php

declare(strict_types=1);

namespace App\DataProvider;

use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryResultCollectionExtensionInterface;
use ApiPlatform\Core\Bridge\Doctrine\Orm\Util\QueryNameGenerator;
use ApiPlatform\Core\DataProvider\CollectionDataProviderInterface;
use ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface;
use App\Entity\Transaction;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\Security;

final class TransactionsDataProvider implements CollectionDataProviderInterface, RestrictedDataProviderInterface
{
    private ManagerRegistry $managerRegistry;

    private Security $security;

    private object $collectionExtensions;

    public function __construct(
        private object $collectionExtensions,
        private ManagerRegistry $managerRegistry,
        private Security $security,
    )
    {}

    public function supports(string $resourceClass, string $operationName = null, array $context = []): bool
    {
        return EmployeeWorkingHour::class === $resourceClass;
    }

    public function getCollection(string $resourceClass, string $operationName = null, array $context = []): iterable
    {
        $queryBuilder = $this->managerRegistry->getRepository(Transaction::class)->createQueryBuilder('t');

        if (!$this->security->isGranted('ROLE_MANAGER')) {
            $queryBuilder
                ->andWhere('t.employee = :employee')
                ->setParameter('employee', $this->security->getUser())
            ;
        }

        $queryNameGenerator = new QueryNameGenerator();

        /** @var QueryCollectionExtensionInterface $extension */
        foreach ($this->collectionExtensions as $extension) {
            $extension->applyToCollection($queryBuilder, $queryNameGenerator, $resourceClass, $operationName, $context);

            if ($extension instanceof QueryResultCollectionExtensionInterface && $extension->supportsResult($resourceClass, $operationName)) {
                return $extension->getResult($queryBuilder);
            }
        }

        return $queryBuilder->getQuery()->getResult();
    }
}

Hope this helps!

Ma
Image of Marek Krokwa, confidently posing with arms crossed. He's CTO at Primotly and contributor to our insightful technology articles. Marek is wearing a dark collared shirt and glasses, presenting a smart and approachable look, set against a clean white background
Marek Krokwa
CEO

Latest articles

Illustration that symbolizes the importance of governance in ESG

Business | 15/11/2024

Governance in ESG: Why Strong Leadership is Key to Sustainability?

Łukasz Kopaczewski

While all three pillars—environmental, social, and governance—are essential, governance often plays the most foundational role. Governance, which includes ethical leadership, transparency, and accountability, ensures that ESG efforts are not just statements on paper but integrated into a company’s everyday decisions. Notably, a recent study found that 39% of companies feel they perform adequately on governance, indicating significant room for improvement​.

Read more
Illustration to the article on Making Data-Driven Decision and AI

Business | 08/11/2024

Making Data-Driven Decisions: How Artificial Intelligence Can Help You Avoid Common Pitfalls

Agata Pater

Relying solely on intuition in business can often mean missing out on big opportunities. Consider Netflix as a prime example. By analyzing over 30 million daily 'plays' and countless subscriber ratings and searches, Netflix pinpointed exactly what viewers wanted, leading to the creation of hit series like House of Cards. This data-driven approach didn’t just increase viewer engagement; it revolutionized the entertainment industry’s approach to content creation. Embracing Data-Driven Decision Making can similarly empower companies to craft more precise strategies, avoid common pitfalls, and make choices that resonate with actual market demand.

Read more
Illustration to the article about importance of ESG factors in construction sector, and how AI can help in reporting sustainability efforts.

Business | 31/10/2024

The Growing Importance of ESG in Construction Industry

Bernhard Huber

The construction industry is a critical driver of economic growth, responsible for creating the infrastructure that supports communities and industries worldwide. However, this sector also faces substantial challenges related to environmental degradation, social equity, and governance issues. Increasing global awareness of climate change, heightened investor scrutiny, and evolving regulatory demands have led to a fundamental shift in how construction companies approach their operations.

Read more