DEV Community

Ryan Robinson
Ryan Robinson

Posted on • Originally published at ryanrobinson.technology

Drupal: Include Default View in Module

You may be working on a Drupal module and want to include a default view.

Install

This is actually quite simple: put the configuration file in the config/install folder within the module, with the proper configuration name, and it will install.

This would be the full configuration file, the same as you would generate using the configuration sync tools.
You can write this file from scratch, especially if they're hidden options. If it is something that has a user interface, you can do what I tend to do: determine all your preferred settings and test it out within the interface, then export the configuration from either the UI (/admin/config/development/configuration/single/export) or drush (drush config-export but that does all configuration, not only the specific one you want).

Uninstall

What about uninstalling the configuration you've installed with the module? This will depend on the context of your module, but you may want to also uninstall this configuration when the module is uninstalled. The configuration might no longer make sense without the module, or you might create conflicts if you installed it once, uninstalled the module but left the configuration, then tried to install it again.

To do that, add a few lines like this to the uninstall hook of the .module file, for example with a config file named webform.webform.event_registration:

/**
 * Implements hook_uninstall
 * */
function module_name_uninstall() {
  if (!empty(\Drupal::configFactory()->getEditable('webform.webform.event_registration'))) {
    \Drupal::configFactory()->getEditable('webform.webform.event_registration')->delete();
  }
}
Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay