How should I declare the usage of a new template.php file in a Drupal 8 sub theme?

I ask since I didn't find anything about this in the documentation, like this example.

For example, I created a template.php file and put the following function inside, saved and flushed all Drupal cache, but the desired change did not occur.

function garku_process_html(&$variables) {
  $variables['styles'] = preg_replace('/.css\?.*"/','.css"', $variables['styles']);

Drupal 8 themes don't have template.php files. Now they use a mytheme.theme file, which needs to be located inside the directory containing the theme. (Replace mytheme with the machine name used by the theme.)

Preprocessing and modifying attributes in a .theme file shows an example of preprocess function, which is placed in the mytheme.theme file, instead of the template.php file.


The file name was changed to [theme-name].theme; from the docs:


The .theme file is a PHP file that contains all the conditional logic and data (pre)processing of the output. It may also extend basic theme settings.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy