0
votes

I m pretty new to D8 and I m trying to do the following thing :

I have a content type : movie. In that content type I have a custom field api_id wich is simply an integer.

When I am on a movie page I want to display under the content a block with movies with the same api_id.

I have managed to create block for the same movies from the same author but I can't figure out how to to filter on api_id (I have played so much with contextual filters ...)

Any ideas ? Thx

1

1 Answers

0
votes

Ok , I manage to do what I want with hook_views_query_alter() :

function my_module_views_query_alter(\Drupal\views\ViewExecutable $view, \Drupal\views\Plugin\views\query\QueryPluginBase $query)
{
    if($view->id() == 'my_view' && $view->current_display == 'my_block'){
        $movie= Node::load($view->args[0]);
        if(is_object($movie)) {
            foreach ($query->where as &$condition_group) {
                foreach ($condition_group['conditions'] as &$condition) {
                    if ($condition['field'] == 'node__field_id_movie.field_id_movie_value') {
                        $condition = array(
                            'field' => 'node__field_id_movie.field_id_movie_value',
                            'value' => $movie->get('field_id_movie')->value,
                            'operator' => '=',
                        );
                    }
                }
            }
        }
    }
}

To do that you have to first create a view with a simple filter on the field you want to override the filter.