How do I add grouping, programmatically? I've added a custom field to a view and want to display results as a grid grouped by this field. How do I set that "Grouping field Nr.1" option in my code? I tried the hook_views_pre_build() using the $view-&gt;addHandler but m not able to make it work. I guess the issue lies in the $option array and the $type parameter for addHandler method. Here is my code.

function utility_views_pre_view(ViewExecutable $view, $display_id, array &amp;$args) {
  if ($view-&gt;id() == 'frontpage' &amp;&amp; $display_id == 'page_1') {
    $options = array(
      'style' =&gt; array(
        'options' =&gt; array(
          'grouping' =&gt; array(
            'field_category',
          ),
        ),
      ),
    );
    $view-&gt;addHandler("page_1", "grouping", "node__field_category", "field_category", $options);
  }
}


Update
When I use $view-&gt;addHandler("page_1", "field", "node__field_category", "field_category", $options); I get the a catgeory field added to each of the view items.

Can anyone help me on this?
Finally found a solution. The grouping is not an Eventhandler in Drupal 8 but a Plugin (Style Plugin) specifically. Here is the code

function utility_views_pre_build(ViewExecutable $view) {
  $view-&gt;style_plugin-&gt;options['grouping'][0] = array('field' =&gt; 'field_category');
}