Creating A Custom View Field In Drupal 8 To Use With Workflow States

May 22, 2019 | 1 Minute Read

Tags: Drupal 8 , drupal development , Drupal

When it comes to the workflow module, adding the view field is simple, but again there may be scenarios where you have to add custom functionality in the query or options. Even otherwise, this article helps with creating a simple view field in Drupal 8. In this example, we will consider a workflow state as a phase of the node.

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

After that is done, clear cache and search for the field in the view. You will see the label ‘Phase’, 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 ‘PhaseViewsField.php’ in the folder Plugin/views/field with the following code:

Now you can clear cache again and add the field in the view. You can also use this field to add a custom filter. You can learn about that here.