Drupal - Create Bootstrap sub-theme using shell script

By xngo on January 19, 2020

In previous article, I wrote a step-by-step on how to manually create a Bootstrap sub-theme in Drupal. Since I created a lot of Bootstrap sub-themes, I decided to write a Bash script to automatically create them.

Create Bootstrap sub-theme

Before you run the script, make sure you change the value of variables to match your situation:

  • drupal_dir
  • theme_name
  • theme_title

Also, the following applications are installed on your computer:

  • drush
  • rename
  • sed
# Variables to change according to your case.
    drupal_dir=../../local/
    theme_name=thb_theme
    theme_title="The Holiday Break theme"
 
# Download current Bootstrap theme.
    (
        cd "${drupal_dir}";
        drush dl -y bootstrap;
        drush en -y bootstrap;
    )
 
# Create Bootstrap subtheme.
    bootstrap_theme_dir="${drupal_dir}/themes/bootstrap"
 
    rm -rf ${theme_name}
    cp -a ${bootstrap_theme_dir}/starterkits/THEMENAME/ ${theme_name}
 
    rename "s/THEMENAME/${theme_name}/" ${theme_name}/THEMENAME.*
    rename "s/THEMENAME/${theme_name}/" ${theme_name}/config/install/THEMENAME.*
    rename "s/THEMENAME/${theme_name}/" ${theme_name}/config/schema/THEMENAME.*
 
    # Change *.info.yml
        info_yml_file=${theme_name}/${theme_name}.info.yml
        mv ${theme_name}/${theme_name}.starterkit.yml ${info_yml_file}
 
        # Change THEMETITLE
        sed -i "s/THEMETITLE/${theme_title}/" ${info_yml_file}
 
        # Change THEMENAME
        sed -i "s/THEMENAME/${theme_name}/" ${info_yml_file}

Install your custom theme

theme_name=thb_theme
drupal_theme_dir="${drupal_dir}/themes"
rm -rf ${drupal_theme_dir}/${theme_name}
cp -a ${theme_name} ${drupal_theme_dir}
 
(
    cd "${drupal_dir}"; 
    drush en -y ${theme_name}; 
    drush -y config-set system.theme default ${theme_name}; 
    drush cr
)

About the author

Xuan Ngo is the founder of OpenWritings.net. He currently lives in Montreal, Canada. He loves to write about programming and open source subjects.