Skip to main content

Easily duplicate a content type in Drupal 8

File under

Assuming you already have a content type created and the config files checked into source control, duplicating the content type and all of its fields is very easy.

  1. Use rename to rename the machine name of the existing content type in your .yml files to the new content type’s machine name (whatever you want that to be). If you don’t have rename, it’s easy to install via homebrew: brew install rename.

    For example, if you want to change the article content type to article_copy, cd into your sync directory and run this:

    rename -s article article_copy *
    

    Add the -n flag to do a dry run.

  2. The previous step renamed all the files. If you want to retain the existing content type, while in the sync directory, perform a git checkout.
    git checkout .
    

    That will restore the renamed files while keeping the new ones.

  3. Use sed to change references of the old content type’s machine name to the new one. Make sure you only run the command on config files with the new machine name.
    sed -i "" 's/article/article_copy/g' *article_copy*.yml
    
  4. Use sed to get rid of the uuid line in your .yml files.
    sed -i "" 's/^uuid:.*$//g' *article_copy*.yml
    
  5. Back up your database if you haven’t already :)
  6. Run a config import.
    drush cim
    
  7. Confirm that everything looks good, change the name of the content type in the admin UI, and then run a config export to populate your .yml files with their new uuids.
    drush cex
    

You should now have a copy of the original content type.