I’m writing this up based on my notes I collected during roughly one year of working with Drupal. It took me a while to understand the relationship between the GUI-based content management and configuration system and the CLI-based drush command and how all of this relates to the actual code on the server. This was because I was first tasked with content curation and simple modifications, which meant it took me a while to analyze the whole system. I hope this is a perspective useful for people like me who come to Drupal without tons of full-stack developer experience.

What is Drupal?

Drupal is both a symfony-based PHP framework to build web applications and a content management system (CMS) with an extensive GUI geared towards site builders who are not developers. Drupal.org now also mentions Drupal CMS as a separate product, I am not writing about that one in the series. Starting to work on an existing site this duality was quite confusing to me in the beginning: How do the graphical representation and the codebase interact? How does Drupal separate content and configuration?

Conceptually, Drupal has three layers:

  1. Code (php, twig, js, css): lives in files only and is edited by developers
  2. Configuration: Content types, layouts, fields, blocks: lives in the ‘active configuration’, a DB, but can be synced. This is my entry point for this series.
  3. Content: Actual nodes, images text etc curated through the GUI and edited by content curators

Configuration: GUI and CLI

When loading your site as an admin, you will see the admin menu giving you access to all configurations on one of the sides of the page. ‘Configuration’ is indicated by a cogwheel or a wrench, depending on the admin theme that is installed. You can click on it and most configurations available to you will be shown under /admin/config. However, I found that it’s often easier to find the module you want to configure by browsing the ‘Extend’ site (indicated by a puzzle piece) /admin/modules. There you can search for your module and generally find a link to the module’s configuration pages. I quickly found clicking around, searching the correct pages by ctrl+F on the config overview annoying. This is what helped me explore the powerful drush command. drush is the commandline tool to administer your drupal site and once you learned the essential commands it’ll let you do everything that much faster. Let’s start with how those two approaches impact your site:

Configuration: DB and config files

Configuration lives in two places: The live database’s configuration tables (‘active configuration’, I’ll call it ‘DB’) and the configuration files. The database represents the live state of the site and it updates everytime you go to the configuration menu in the admin GUI and change any setting. The problem of course is that just updating a database is a bit ephemeral: How would you version-control your changes? How would you share your configuration with colleagues or between your development environment and production servers? This is why it is essential to export the database to configuration files. The three central commands to this are

drush config:status # Displays which configurations are only in the DB or only in files, gives an overview before doing anything. Shorter drush cst
drush config:export # Writes your live configuration from the config DB to the configuration files. Shorter drush cex
drush config:import # The reverse of export: Writes everything from your configuration files to the config DB. Shorter: drush cim

This means you have two ways to work: You configure the live DB and export or you edit your config files and import. Doing both at the same time will create some issues for you since cex/cim will overwrite their targets. That means if you configured e.g. some block on your page and also changed the block’s config file, drush cim will only take what’s in the config file and drush cex will only write to the file what’s in your DB. There is no git-style conflict resolution.

Speaking of git and version control: It’s advisable to commit all your updated config files after a drush cex or before a drush cim. Where do those files live? You will find them in the config_sync_directory as defined in your settings.php file. I found it easiest to put it in a clear place at the top level of my site’s root:

.
├── composer.json
├── composer.lock
├── config
│   └── sync
├── web
└── vendor

Edit and inspect the config DB using drush

The admin GUI is not the only place to change settings to your live DB. drush also allows you to inspect any configuration and even change and update it in place. The commands for this are:

drush config:get <config name> # Depending on your drush settings you might be provided with an auto-complete or a numbered list of all your site's configurations if you don't provide a name directly. In the latter case you'll have to then type the number corresponding to your configuration of choice. config:get will just cat your configuration to the terminal. Shorter: drush cget
drush config:edit <config name> # This will open the configuration to edit in your shell's default editor. Shorter: drush cedit

You will notice that configuration files and configs are written in yaml. Config files will have the .yml extension. To open the configuration using e.g. drush cget you’ll have to strip the file extension: cat config/sync/node.type.page.yml becomes drush cget node.type.page.

Summary: Options to modify your page and how to version control

  flowchart TD
  subgraph CLI
    subgraph FILES["Config files"]
      f1["vim $CONFIG_SYNC_DIR/&lt;name&gt;.yml"] --> f2["save &amp; close"]
      f2 --> f3["changes live in<br/>config file"]
    end
    subgraph DB["Live DB"]
      d1["drush cedit &lt;name&gt;"] --> d2["save &amp; close:<br/>automatic DB update"]
      d2 --> d3["changes live in<br/>live config DB"]
    end
  end

  subgraph GUI
    g1["browse to /admin/config"] --> g2["find your config &amp; edit"]
    g2 --> g3["changes live in<br/>live config DB"]
  end

  g3 --> s1["drush cex"]
  d3 --> s1
  s1 --> s2["git add config/sync<br/>git commit -m '&lt;what changed&gt;'"]

  f3 --> t1["git add config/sync<br/>git commit -m '&lt;what changed&gt;'"]
  t1 --> t2["drush cim"]