Information for: DEVELOPERS   PARTNERS

Adding external themes to your site

During normal Drupal development, when you’re developing your website’s themes, best practices would have you store a theme’s files in the sites/all/themes directory under your docroot. If you want a design team to create and maintain your websites’ theme files without granting them access to your websites’ code, you can set up an external theme repository.

Acquia Cloud Site Factory allows you to configure a link to an external Git repository containing one or more themes for use with your Drupal website. After connecting your website to a Git repository containing one or more themes, the themes will display when you click Appearance in the admin menu.

Git repository requirements

When creating your external Git repository, ensure the repository contains the following items:

  • One or more branches or tags.
  • One or more sets of Drupal theme files to use with your website. Place each theme’s file in a separate directory, named with the theme’s name.

Acquia Cloud requires deployment keys be associated with a user account. As Github does not support the use of the same deployment key for multiple repositories, create an Acquia Cloud user as described in Using deployment keys on Acquia Cloud. Associate the key with a user account, instead of adding the SSH key as a deployment key.

Creating a theme repository whitelist

To protect hosted websites from using themes containing potentially harmful settings or code, you can add a whitelist file of approved theme repositories to Acquia Cloud Site Factory. When administrative users try to add a theme repository, Acquia Cloud Site Factory compares the repository with the contents of the acsf_theme_whitelist.txt file.

To create a new whitelist file for your theme repositories, complete the following steps:

  1. Create a new file to contain your whitelist settings, based on the following conventions:
    • Enter each external Git repository on a separate line.
    • You can add comments to entries by starting lines with the number sign ( # ). The # isn’t used for whitelist filtering.
    • Use the asterisk character ( * ) for wildcard entry.
    • If you don’t end a repository line with an asterisk, its use is implied.
  2. Save the file with the name acsf_theme_whitelist.txt in the root directory of your code repository.
  3. Commit your code changes with the new whitelist file to your codebase.
  4. Deploy the code to your production environment.

Example

The following whitelist file allows only theme repositories from the Acquia organization on github.com, the Open Framework repository on github.com, and the zen.git repository on drupal.org:

# Allow any github repos from the Acquia organization
github.com:acquia/*

# Allow specific github repo
github.com:SU-SWS/open_framework

# Allow the Zen theme from Drupal.org
git.drupal.org:project/zen.git

If you don’t upload a whitelist file, or if the file does not contain any entries, Acquia Cloud Site Factory will not block any theme repositories from use.

Connecting to a theme repository

To connect to an external Git repository containing one or more themes, complete the following steps:

  1. Sign in to the Site Factory Management Console as a user with either the Platform admin or Site builder role.

  2. Find the website you want to modify, open its actions menu, and then click Manage theme repository.

  3. Click Connect site repository to enter your Git repository’s information.

  4. Click the Show SSH public key link to display the SSH public key.

  5. Select and copy the contents of the SSH public key field to your clipboard.

  6. Open a new browser tab, and sign in to your Git repository.

  7. Add the SSH public key to the theme repository, and configure the key to have read access.

  8. Return to the page in which you are entering your theme repository information, and then enter the following values for the Git repository you want to use:

    • Git URL – The Git SSH URL of the theme repository (for example, git@github.com:repo.git)
    • Branch or tag – A valid branch or tag from the theme repository

    Note

    To ensure theme changes for one website don’t affect other websites using the same theme, we recommend you use discrete tags for production websites. Acquia recommends using branches for staged websites, because it can speed your theme development process.

  9. Click Save.

Acquia Cloud Site Factory compares theme repositories to entries in the whitelist file based on the following parameters:

  • The user@ preceding the theme repository is ignored.
  • Theme repositories beginning with an exact match of one or more of the whitelist file entries are accepted. For example, a whitelist entry of github.com would match git@github.com:repo.git.

If the whitelist file blocks a theme repository, Acquia Cloud Site Factory will display an error message and keep the previous theme repository settings.

If no error messages display, your Git repository themes will be checked out for use by your website. After the checkout completes, you can view the themes by signing in to the website and clicking Appearance in the admin menu.

Note

There may be a delay between associating your Git theme repository with your website and the availability of its themes on the Appearance page. For example, delays can be more noticeable during updates or staging processes.

Refreshing your theme

Whenever you make changes to your theme files, and commit those changes to the Git theme repository, you must update your websites’ theme files based on the correct branch or tag. You must update every website using the updated theme.

To refresh your theme on a single website, complete the following steps:

  1. If you are refreshing a theme for a Drupal 8 stack, create a post-theme-deploy hook script, as described in Performing custom actions after theme deployments, to clear the Twig cache after you deploy the updated theme.
  2. Sign in to the Site Factory Management Console using an account with either the Platform admin or Site builder role.
  3. Find the website you want to modify, open its actions menu, and then click Manage theme repository.
  4. Select the appropriate method based on whether you are using a branch (staged websites) or a tag (production websites):
    • Branch – Click Refresh themes.
    • Tag
      1. Click the Change site theme repository link.
      2. In the Branch or tag list, click the tag with the new theme changes.
      3. Click Save.

Acquia Cloud Site Factory obtains your new theme changes from the Git repository and adds them to your website. After it makes copies of the theme files, the theme changes display in the website’s Appearance page (and if the theme is in use, on the website itself).

Note

You may see a delay between associating your Git theme repository with your website, and the availability of its themes on the Appearance page. For example, delays can be more noticeable during updates or staging processes.

Performing custom actions after theme deployments

You can use the post-theme-deploy Factory hook to clear Twig caches or perform other actions after deploying a theme. The post-theme-deploy Factory hook makes a set of arguments available to any script executed by the hook:

Argument Value
1 The hosting site group
2 The hosting environment
3 The theme scope (currently the only possible value is site)
4 The theme event (possible values: modify, delete)
5 The site domain

Using these arguments, you can create a script like the following example, which runs a Drush command to clear the Twig cache on the affected site on every server. Acquia Cloud Site Factory supplies the needed host, environment, and domain values:

#!/bin/bash
drush8 @$1.$2 --uri=$5 ev '\Drupal\Core\PhpStorage\PhpStorageFactory::get("twig")->deleteAll();'

To deploy or update a Drupal 8 theme from a remote repository:

  1. Create a script file with a name like ClearTwigCache, with content similar to the previous example. The script must be executable, as described in Executing your commands with a hook.
  2. Save and commit the script file in your code repository in the /factory-hooks/post-theme-deploy directory.

More information