Ceramic Toad

Programmatically Importing Drupal 8 Field Configurations

Sometimes during development, a chicken and egg situation happens when business logic intersects with Drupal's mechanisms. For instance, a custom module, client_business_logic, is built to handle some unique, abstract bit of logic which depends on a taxonomy vocabulary that has fields added to it. Now, all of the entity and field structures can be handled via Drupal 8's Configuration API, but since the client will install this site a number of times, we would like to provide some baseline taxonomy terms at the start. If the field's configuration has not been imported before the module is enabled, then that field's data will not be saved.

To solve this, I attempted to import the configurations during client_business_logic_install() similar to how I would handle them in Drupal 7 with Features. There are examples found here and here describe programmatically importing configurations from yaml files, but when I tried their approach, I received an error: Base table or view not found. Which seemed to indicate Drupal is detecting field configuration and so it's assuming the database table has been created. Sure enough, the configuration write does not perform changes to the database.

Then, I came across this StackOverflow solution for programmatically building fields. The original post used hard-coded arrays that appeared to be the same configuration structures stored in yaml, just missing extras like UUIDs, and the configurations that I was trying import were for Drupal entities, not just arbitrary settings. So, I tried combining the two approaches and it worked without any UUID or renaming confusion in subsequent configuration imports and exports. This can also work within update scripts, hook_update_N().

use Drupal\taxonomy\Entity\Term;
use Drupal\Core\Config\FileStorage;
 * Implement hook_install().
function client_business_logic_install() {
  // Obtain configuration from yaml files
  $config_path = 'path/to/config';
  $source      = new FileStorage($config_path);
  // Obtain the storage manager for vocabularies
  // Create a new vocabulary from the yaml configuration and save
  // Obtain the storage manager for field storage bases
  // Create a new field from the yaml configuration and save
  // Obtain the storage manager for field instances
  // Create a new field instance from the yaml configuration and save
  // Create taxonomy terms
  $terms = [
    'Default' => [
      'field_machine_name' => [
        0 => ['value' => 'default'],
  $weight = 0;
  foreach ($terms as $name => $fields) {
      'name'   => $name,
      'weight' => $weight++,
      'vid'    => 'business_logic_vocab',
    ] + $fields)->save();
Filed under:

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About the Author

Marcus Bernal, Developer / FunOps

Marcus started off out of high school programming Flash-based brochure CDs and websites for apartment communities. Since then, he has been developing LAMP-stack websites and, recently, Drupal modules. With the mind of a nerd, he has studied both computer science and civil engineering, so tackling large systems is his forte.

In his spare time, Marcus loves searching for new music, hiking/camping/backpacking, skateboarding, but, most importantly, being a father.