Views development

Views is a powerful part of Drupal that allows you to quickly build and customize lists of content. A key part of it's flexibility is that each module writes it's own integration to define ways to query the database, set up relationships, add fields, filters, and sorts. These are called handler plugins.

If you want to add something to Views, like a database table or field, you will have to implement a handler plugin for it. Then in your module or views.inc file, use hook_views_data() or hook_views_data_alter() to define the integration with views. The views.api.php file is loaded with comments on how to do this.

Entities use EntityViewsData to define their Views integration. See NodeViewsData as an example. Other modules like Content Moderation have a pseudo ViewsData class that does something similar. See the getViewsData() function in those files.

You will see ensureMyTable() used a lot in plugins. The function comment actually says that. This function is used by plugins to check that the query has the tables and fields it needs, and can be used to set up those relationships if needed. See how this is done in ModerationStateJoinViewsHandlerTrait (which is loaded in Content Moderation's view plugins). Search your codebase for the function name to find more examples in plugins.

Tags
Views