Alphaegen
Alphaegen

Reputation: 187

How would I filter a collection by translated properties?

The following function within the EntityCollection class can be used for filtering a collection based on properties:

public function filterByProperty(string $property, $value)
{
    return $this->filter(
        static function (Entity $struct) use ($property, $value) {
            return $struct->get($property) === $value;
        }
    );
}

How can I use this function to filter based on translated fields? I've created a twig extension which now does it for me, but I'd rather use the default function. This is my custom twig extension:

public function filterByTranslatedProperty(EntityCollection $entityCollection, string $property, $value): EntityCollection
{
    return $entityCollection->filter(
        static function (Entity $struct) use ($property, $value) {
            return $struct->getTranslation($property) === $value;
        }
    );
}

Upvotes: 1

Views: 62

Answers (0)

Related Questions