Skip to content
Field note

Creating Custom Exposed View Filters In Drupal 8 For Workflow States

Introduction Creating workflow states with the workflow module and attaching them to entities is quite simple. Example, the transitions “draft”, “editing”, “published” for the story node type. I’m not sure about Drupal 7, but in Drupal 8, adding filters to workflow enabled nodes and filtering by them is a little tricky. The filter does appear when you are adding in the filter, but the widget is displayed in an odd way (with workflow description textarea). The solution is to create a custom filter. In this example, I'll create a phase filter where a workflow state is considered a phase of the node.

May 22, 2019·1 min read·Abhai Sasidharan, Axelerant Alumni/ Field correspondent
Creating Custom Exposed View Filters In Drupal 8 For Workflow States

Introduction

Creating workflow states with the workflow module and attaching them to entities is quite simple. Example, the transitions “draft”, “editing”, “published” for the story node type. I’m not sure about Drupal 7, but in Drupal 8, adding filters to workflow enabled nodes and filtering by them is a little tricky. The filter does appear when you are adding in the filter, but the widget is displayed in an odd way (with workflow description textarea). The solution is to create a custom filter. In this example, I'll create a phase filter where a workflow state is considered a phase of the node.

To create a new filter, you have to first define it in the hook_views_data_alter(). Insert the following piece of code in your custom_module.module file:

After that is done, clear cache and search for the filter in the view. You will see the label ‘Phase filter’, although selecting it won’t do anything because we haven’t defined it yet. That is the next step. In the custom module, create a file ‘PhaseViewsFilter.php’ in the folder Plugin/views/filter with the following code:

In the above code, we are extending the ManytoOne class to be able to add relationship and add multiple selections in the filter once exposed.

Now you can clear cache again and add the filter in the view, and even expose it.

Companion brief

Bring this dispatch into a working session - one page in, scoping memo out.

Brief Foyer
Pressure-test "Field note" with a partner

If this dispatch landed, bring it into a room with us.

The argument in "Creating Custom Exposed View Filters In Drupal 8 For Workflow States" is ours - but every situation breaks the argument differently. Send the situation in one page and a named partner will reply inside one business day with a scoping memo: where the argument holds for you, where it bends, and what we'd actually do in the first two weeks.

One-pager · Field note

By submitting this form you acknowledge that you have read Axelerant's Privacy Policy and agree to its terms.

Replied to by a named partner within one business day.