Currently I have a boolean field field_private_content
, if that field is set, the content should only be shown to authenticated users, anonymous users may only see the teaser and a login form. So far it’s working well, also with views where I added the filter on that field. The filter get’s removed automatically by the implementation of hook_views_pre_view() in a custom module:
function custom_module_views_pre_view(ViewExecutable $ view, $ display_id, array &$ args) { // Remove filter on field_private_content != TRUE for authenticated users. if ($ view->getHandler($ view->current_display, 'filter', 'field_private_content') && \Drupal::currentUser()->isAuthenticated()) { $ view->removeHandler($ view->current_display, 'filter', 'field_private_content'); } }
Although that works very well, the behavior is not obvious from views ui. I would prefer to have a duplicate of the field_private_content filter which should have the mentioned behavior by default and could be labeled properly – the original filter should keep it’s original behavior.
How can that be achieved? How to add a custom views filter handler for a specific field?