I'm using a views block in my website, the content of this view is filtered programmatically. I now get messages from users who see the wrong information. The filter is based on the currently logged in user. Since I need this information in the regular filter and not in the contextual filter I created a simple module to change the filter value.
My filter is like this:
(
Content type = contenttype_a
AND
uid(nodes author) = */this value is programmatically added*/
)
OR
(
Content type = contenttype_b
AND
user_target_id(reference) = */this value is programmatically added*/
)
Than I use this in my module:
*/
function entity_access_views_pre_view(ViewExecutable $view, $display_id, array &$args){
$currentUser = \Drupal::currentUser();
$cuid = $currentUser->id();
/* @ Filter View: Media */
if ($view->id() == 'my_view_id') {
if ($display_id == 'block_1' || $display_id == 'page_1' || $display_id == 'page_2') {
$filters = $view->display_handler->getOption('filters');
$filters['uid']['value']['value'] = $cuid;
$filters['user_target_id']['value']['value'] = $cuid;
$view->display_handler->overrideOption('filters', $filters);
}
}
}
Since I can't seem to find the problem I'm having doubts about if above is working as I want it to. So I wanted to check if you see anything strange or false. Can this filter value be cached in some way? Or maybe there is a better way to do this.
I can't use the contextual filter, which I would prefer, because the view is showing two content types where on each content type the filter should be different.
Thanks in advance.