Use front end TWIG templates for Paragraphs when using Gin.

Created on 12 December 2022, almost 2 years ago
Updated 8 February 2023, almost 2 years ago

I am using the Gin theme and love every bit about it. However, I am also using Paragraphs and Paragraphs Layout. This means my Paragraphs are rendering in the Gin theme using the default Paragraph templates. I know I can just use my front end theme when editing content, but that kind of defeats the purpose of using Gin with all its greatness and trying to cover all the bases in my front end theme as far as the admin stuff goes would be a pain. So I thought I could just sub-theme Gin to add some templates, but that is proving to be difficult also. I just can't get it to work right without everything getting borked. What do you recommend as far as me just wanting to use my front end Paragraph templates within Gin? That's ALL I want to do also. I do not need to edit anything else like CSS or JS. I JUST want my front end paragraph templates to be available in Gin instead of Gin using the default Paragraph templates. I was thinking of maybe creating a custom module to do some trickery? Anyway, I'm not sure the best way to handle this. I feel like sub-theming would be the easiest way, but like I said I couldn't seem to get that working with the standard way of sub theming.

THANKS!

πŸ’¬ Support request
Status

Closed: works as designed

Component

Miscellaneous

Created by

πŸ‡ΊπŸ‡ΈUnited States socalerich

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡¨πŸ‡¦Canada dallen33

    @SoCalErich Would you mind posting an example of how you accomplished this?

  • πŸ‡ΊπŸ‡ΈUnited States socalerich

    Hey sorry I was out of town. So I basically created a custom module called admin_template_overrides. In my .module file I added a hook_theme function like this:

    <?php
    
    function admin_template_overrides_theme($existing, $type, $theme, $path) {
      return [
        'paragraph__basic_text__default' => [
          'template' => 'paragraph--basic-text--default',
          'base hook' => 'paragraph'
        ],
        'paragraph__accordion' => [
          'template' => 'paragraph--accordion',
          'base hook' => 'paragraph'
        ],
        'paragraph__accordion_item' => [
          'template' => 'paragraph--accordion-item',
          'base hook' => 'paragraph'
        ],
      ];
    }
    

    I have many more paragraph templates in my actual site, but this is a watered down version for simplicity.

    So this looks for specific theme templates and specifies which template in my custom module to use for it as an override. For my use case, I am specifically overriding paragraph templates. You may need to change the "base hook" value to something else if you are not doing this for paragraphs.

    Then I created a templates directory in my custom module that has all the various templates that I need except all I do is extend my front end themes template. This way I don't have to have the template code in two different places. I can essentially edit my front end theme template and the custom module will just automatically extend it. This may not be the BEST way to do this, but it is working and doing what I needed.

    So, for example, I have a file in the templates directory of my custom module called "paragraph--basic-text--default.html.twig" and all it has in the file is:

    {% extends "@uswds_base_subtheme/paragraphs/paragraph--basic-text--default.html.twig" %}

    where @uswds_base_subtheme is my front end theme.

  • πŸ‡ΊπŸ‡ΈUnited States alexi721

    Erich, thank you so much for providing a detailed example on how to get the front-end templates activated in the edit form generated by the 'Layout Paragraphs' module. This is very helpful and worked great.

    To make it even better I also imported the styles from the front-end theme by adding the following function to the .module file:

    <?php
    function admin_template_overrides_form_alter(&$form, FormStateInterface $form_state, $form_id) {
      switch ($form_id) {
        case 'node_page_edit_form':
          // attach the library from the frontend theme
          $form['#attached']['library'][] = '<uswds_base_subtheme>/global';
          break;
      }
    }

    The only drawback is that the fonts of the Gin theme get overwritten by the fonts of the front-end theme.

    @SoCalErich, did you come up with a way to avoid the font replacement of the Gin theme?

    Thanks again for sharing your insights with the community.

Production build 0.71.5 2024