XF 2.1 Adding custom user field filters

Kirby

Well-known member
I might be missing smth., but as far as I can see there is no real option to add further filters for custom user fields:

PHP:
$container['customFields.users'] = $this->fromRegistry('userFieldsInfo',
    function(Container $c) { return $c['em']->getRepository('XF:UserField')->rebuildFieldCache(); },
    function(array $userFieldsInfo)
    {
        $definitionSet = new DefinitionSet($userFieldsInfo);
        $definitionSet->addFilter('registration', function(array $field)
        {
            return (!empty($field['show_registration']) || !empty($field['required']));
        });
        $definitionSet->addFilter('profile', function(array $field)
        {
            return !empty($field['viewable_profile']);
        });
        $definitionSet->addFilter('message', function(array $field)
        {
            return !empty($field['viewable_message']);
        });
        return $definitionSet;
    }
);

I could
  1. Overwrite the whole thing which isn't a great option as it could conflict with other Add-ons
  2. Invoke the loading code on app_pub_start and add my filter afterwards which causes unconditional overhead and kinda defeats DI
  3. Only add my filters where I know that I will need them, but this is also a bad option as I might not know all places that might access the filter and if it is not defined it will not be invoked at all
Does anyone have another idea on how to approach this?
 
app_pub_start should be fine if you're using the container extend method:
PHP:
$container->extend('customFields.users', function(\XF\CustomField\DefinitionSet $definitionSet)
{
    $definitionSet->addFilter('foo', ...);

    return $definitionSet;
});
 
Top Bottom