For the last few months, I have been working on a Drupal Views AI agent. The idea is to use the Drupal AI agent module to create and update Views on a Drupal website, so that sitebuilders can write prompts like these to create and update Views on the site without ever going to Views_UI.
Prompt to create a View
Create an entity type node listing view with the default display, title field with label Title, sort in descending order by created date, filter by published status 1, and filter by type article. Grant 'access content' permission. Create a page display with URL 'content-listing'.
Prompt to update a View
Update the default display title of view node_content_listing to "Article Listing".
After getting familiar with AI agents and the Drupal AI module framework, I started working on the views agent. To start, I developed the architectural design for the views AI agent, and broke the process of creating or updating a view into subprocesses. The workflow was divided into three sub-processes: listing, creating, and updating the view. We do not want an AI agent to delete the view.
Listing views is very easy; you can just load all the views on the site and generate the output.
Creating a view is slightly difficult, but it was made simpler by creating a bare-bones View entity with just the View name, description, and ID. Once the view is created. The next step is to update the view, which was the most difficult part.
Under the hood, the views config entity stores a giant key-value map of multiple views plugin settings. Updating a view involves three different things:
Creating a View display
Creating and updating the View Handlers, e.g. field, sort, filter
Creating and updating view plugins, e.g. style, pager, access, query
Creating a display is easier. There is a really good API out of the box provided by views for this.
Next up, creating and updating the Views display handlers. In views, the information about Views Handler comes from the Views data API . If you are not familiar with the Views data API, it is a large key-value array containing all the information about fields, filters, and sorting criteria that can be used in Views, which is provided by the Drupal entities on the site.
Each Handler type has default settings that are similar across Handler types, but each Handler also has its own unique settings. After working on the field type Handlers, I developed a generic approach. I created a plugin to add the Handler and a second plugin to update the Handler settings. Once the Handlers are added to the Views config entity. You can use the views internal API to set up the default values. Since there were default values, you can easily update the handlers' settings.
The next challenging thing was the Views plug-in settings. Views plugins don't have a very defined structure. They don't have similar defaults. They are all unique, so making a generic function call plugin for views plugins was very challenging. Some View plugin configurations are string values, others are key-value pairs, and others are conditional key-value pairs. To address these issues, I created a plugin to create Views plugins; their configuration is stored under the display options setting in Views, and a second plugin to update the configuration of these Views Plugins. This is where I spent the most time figuring out all the different settings and configurations for the Views plugins.
The good thing about the views config entity is that, once you set the values, the config schema API in Drupal core ensures the View configuration is validated. So it was impossible to save an insecure or wrong configuration using the entity API, which is a good thing.
You can follow along with the development progress and also review and test the MR.
Disclaimers
This is an experimental feature at this point, so please don’t use this in a production environment.
For the Views Agent to work, the site builder using the agent needs to have Views_UI enabled with 'administer views' permissions. This means the LLM can see and update all the enabled views and their detailed configuration.