Skip to main content

Using Templates from a Custom Drupal 10 Theme in the Administration Theme

Front-end Development
Drupal

At Rapid Development Group, we use the Paragraphs module in most of our custom Drupal 10 sites to allow content editors to build custom landing pages using pre-defined and styled components and layouts. We also typically use a separate contributed administration theme to reduce the cost of custom theme development. This results in a disconnected experience between building a page and viewing a page because templates and styles from our custom theme are not used when viewing the administration theme.

To remedy this, we found a way to send certain templates from our custom theme to the administration theme. That way, content editors are able to see a more accurate representation of the page they're building while they're building it (without needing to constantly toggle between the node view and edit pages).

Here's the full snippet:

/**
 * Implements hook_theme_registry_alter().
 */
function custom_module_theme_registry_alter(&$theme_registry) {
  $theme_registry['paragraph']['theme path'] = 'themes/custom/custom_theme_name';
  $theme_registry['paragraph']['path'] = 'themes/custom/custom_theme_name/templates/paragraphs';
  $paragraph_suggestions = drupal_find_theme_templates($theme_registry, '.html.twig', 'themes/custom/custom_theme_name/templates/paragraphs');
  foreach ($paragraph_suggestions as $key => $suggestion) {
    if (!empty($suggestion['base hook']) && $suggestion['base hook'] == 'paragraph') {
      $suggestion = array_merge($theme_registry['paragraph'], $suggestion);
      $theme_registry[$key] = $suggestion;
    }
  }
}

Assuming that css and javascript libraries are attached to each paragraph twig template, this method allows us to use the exact HTML, CSS, and JS in both our custom theme and administration theme without needing to create duplicate templates.

Bonus — it's not just for paragraphs. Use this for nodes, views, blocks, and any other template needed for your specific page building requirements!