0

I'm using the default theme, Bootstrap Barrio, but for certain pages I'm overriding the page.html.twig template to re-arrange the layout, however I still want the pre-processing done by my theme in bootstrap_barrio_preprocess_page to execute so that I don't have to maintain a copy of that code inside my custom template's pre-process function, custom_module_preprocess_page__landing.

Is there a way to override the template, but still have the original theme's pre-process run so it can setup the default variables? I found this post that seemed to imply that adding this page suggestion hook that would then reference my page--landing in the module that is setting things up, would achieve this, but it's not working for me.

(my template is called page--landing, but the theme is registered as page__landing and i've tried both syntax)

function my_module_theme_suggestions_page(array $variables) {
  $path = explode('/', trim(\Drupal::service('path.current')->getPath(), '/'));
  if (isset($path[0]) && $path[0] == 'landing') {
    return 'page--landing';
  }
} 

1 Answer 1

0

It seems like you want to override the page.html.twig template for specific pages while still benefiting from the preprocessing logic provided by your theme's bootstrap_barrio_preprocess_page function. To achieve this, you can follow these steps:

  1. Create a Sub-theme: If you're customizing the template in your default theme (Bootstrap Barrio), you might consider creating a sub-theme. This will allow you to make modifications without altering the core theme's files and maintain the preprocessing logic. Refer to the theme's documentation on creating a sub-theme.

  2. Override page.html.twig in Sub-theme: In your sub-theme, create a file named page--landing.html.twig. This is where you'll customize the layout for the specific landing page. You're doing this to ensure the correct template is used for your specific landing page.

  3. Add Template Suggestions: You've already attempted this, but make sure you're implementing the theme suggestion correctly in your sub-theme's my_subtheme.theme file. In this file, use the correct function name (my_subtheme_theme_suggestions_page), and return an array of suggestions. It's important to ensure that the suggestion corresponds to the template file you've created (page--landing.html.twig).

    function my_subtheme_theme_suggestions_page(array $variables) {
      $suggestions = [];
    
      // Get the current path.
      $path = \Drupal::service('path.current')->getPath();
      $args = explode('/', $path);
    
      // Check if the first path component is 'landing'.
      if (!empty($args[0]) && $args[0] == 'landing') {
        $suggestions[] = 'page__landing';
      }
    
      return $suggestions;
    }
    
  4. Ensure Preprocessing Logic is Executed: Ensure that your sub-theme's preprocessing function is named correctly and is placed in the my_subtheme.theme file:

    function my_subtheme_preprocess_page__landing(array &$variables) {
      // Your preprocessing logic here.
    }
    

    The above function should be placed in the my_subtheme.theme file of your sub-theme. Replace my_subtheme with the actual machine name of your sub-theme.

  5. Clear Cache: After adding the theme suggestion function and the template, clear Drupal's cache to ensure the changes are recognized.

By following these steps, you should be able to create a custom template for your landing page while retaining the preprocessing logic from your sub-theme. Make sure that your sub-theme's preprocessing function is properly named and placed in the my_subtheme.theme file.

3
  • Thanks @Z CHAMP, I'm doing this in a custom module instead of the sub-theme, but it uses the same approach. I got all twisted up before and I think I was trying to get the page pre-process variables from a region template override, but got things working now. thanks for the clear, concise guidance.
    – micbay
    Commented Aug 31, 2023 at 14:14
  • UPDATE: just wanted to note that this works if the page--landing template is in a sub-theme, but if the template is in a custom module, it does not execute the barrio theme page pre-process function and that was the root of my problem. Not sure why it works in a sub-theme and not a custom module, but I can make do putting it in a sub-theme.
    – micbay
    Commented Aug 31, 2023 at 15:52
  • UPDATE 2: I figured out that I can move my page--landing template, back from the sub-theme into in my custom module and still get the theme's (or sub-theme's) barrio bootstrap page pre-process function to run by adding a 'base hook' => 'page' parameter to my template registration for 'page__landing' inside my_custom_module_theme hook.
    – micbay
    Commented Aug 31, 2023 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.