0

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->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 &$args) {
  if ($view->id() == 'frontpage' && $display_id == 'page_1') {
    $options = array(
      'style' => array(
        'options' => array(
          'grouping' => array(
            'field_category',
          ),
        ),
      ),
    );
    $view->addHandler("page_1", "grouping", "node__field_category", "field_category", $options);
  }
}

Update When I use $view->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?

1

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->style_plugin->options['grouping'][0] = array('field' => 'field_category');
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.