How to add twig template suggestions per display mode with custom module?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
3
down vote

favorite












I have a custom module where I am trying to use custom twig templates based on the user view mode. The templates files I am trying to add to my theme are:



  • user.html.twig

  • user--compact.html.twig

  • user--token.html.twig

These reside in a folder called templates/, within the theme folder.



This is my current module code:



module_name.module:



function module_name_theme($existing, $type, $theme, $path) 

return [
'user' => [
'template' => 'user',
'base hook' => 'user',
],
'user__compact' => [
'template' => 'user--compact',
'base hook' => 'user',
],
'user__token' => [
'template' => 'user--token',
'base hook' => 'user',
],
];











share|improve this question























  • Could you edit your question and clarify what exactly you are trying to achieve?
    – leymannx
    7 hours ago






  • 1




    I've added to my question, hopefully that helps clarify.
    – Prestosaurus
    7 hours ago










  • If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
    – Prestosaurus
    7 hours ago
















up vote
3
down vote

favorite












I have a custom module where I am trying to use custom twig templates based on the user view mode. The templates files I am trying to add to my theme are:



  • user.html.twig

  • user--compact.html.twig

  • user--token.html.twig

These reside in a folder called templates/, within the theme folder.



This is my current module code:



module_name.module:



function module_name_theme($existing, $type, $theme, $path) 

return [
'user' => [
'template' => 'user',
'base hook' => 'user',
],
'user__compact' => [
'template' => 'user--compact',
'base hook' => 'user',
],
'user__token' => [
'template' => 'user--token',
'base hook' => 'user',
],
];











share|improve this question























  • Could you edit your question and clarify what exactly you are trying to achieve?
    – leymannx
    7 hours ago






  • 1




    I've added to my question, hopefully that helps clarify.
    – Prestosaurus
    7 hours ago










  • If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
    – Prestosaurus
    7 hours ago












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I have a custom module where I am trying to use custom twig templates based on the user view mode. The templates files I am trying to add to my theme are:



  • user.html.twig

  • user--compact.html.twig

  • user--token.html.twig

These reside in a folder called templates/, within the theme folder.



This is my current module code:



module_name.module:



function module_name_theme($existing, $type, $theme, $path) 

return [
'user' => [
'template' => 'user',
'base hook' => 'user',
],
'user__compact' => [
'template' => 'user--compact',
'base hook' => 'user',
],
'user__token' => [
'template' => 'user--token',
'base hook' => 'user',
],
];











share|improve this question















I have a custom module where I am trying to use custom twig templates based on the user view mode. The templates files I am trying to add to my theme are:



  • user.html.twig

  • user--compact.html.twig

  • user--token.html.twig

These reside in a folder called templates/, within the theme folder.



This is my current module code:



module_name.module:



function module_name_theme($existing, $type, $theme, $path) 

return [
'user' => [
'template' => 'user',
'base hook' => 'user',
],
'user__compact' => [
'template' => 'user--compact',
'base hook' => 'user',
],
'user__token' => [
'template' => 'user--token',
'base hook' => 'user',
],
];








8 theming hooks






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 31 mins ago









Cesar Moore

749314




749314










asked 8 hours ago









Prestosaurus

3989




3989











  • Could you edit your question and clarify what exactly you are trying to achieve?
    – leymannx
    7 hours ago






  • 1




    I've added to my question, hopefully that helps clarify.
    – Prestosaurus
    7 hours ago










  • If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
    – Prestosaurus
    7 hours ago
















  • Could you edit your question and clarify what exactly you are trying to achieve?
    – leymannx
    7 hours ago






  • 1




    I've added to my question, hopefully that helps clarify.
    – Prestosaurus
    7 hours ago










  • If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
    – Prestosaurus
    7 hours ago















Could you edit your question and clarify what exactly you are trying to achieve?
– leymannx
7 hours ago




Could you edit your question and clarify what exactly you are trying to achieve?
– leymannx
7 hours ago




1




1




I've added to my question, hopefully that helps clarify.
– Prestosaurus
7 hours ago




I've added to my question, hopefully that helps clarify.
– Prestosaurus
7 hours ago












If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
– Prestosaurus
7 hours ago




If it helps, when I dpm($existing); the only base hooks are field, field_multiple_value_form, and block
– Prestosaurus
7 hours ago










2 Answers
2






active

oldest

votes

















up vote
2
down vote



accepted










The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.



This is how it looks using the hook_theme_suggestions_HOOK():



/**
* Implements hook_theme_suggestions_HOOK().
*/
function mymodule_theme_suggestions_user(array $variables)
return [
'user__' . $variables['elements']['#view_mode']
];





share



























    up vote
    2
    down vote













    The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_user_alter(), as well as switching one template from user to user__full. Below is the updated module code:



    function module_name_theme_suggestions_user_alter(&$suggestions, $variables, $hook) 

    $mode = $variables['elements']['#view_mode'];
    $suggestions = 'user__' . $mode;



    function module_name_theme($existing, $type, $theme, $path)

    return [
    'user__full' => [
    'template' => 'user--full',
    'base hook' => 'user',
    ],
    'user__compact' => [
    'template' => 'user--compact',
    'base hook' => 'user',
    ],
    'user__token' => [
    'template' => 'user--token',
    'base hook' => 'user',
    ],
    ];







    share|improve this answer






















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "220"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f269782%2fhow-to-add-twig-template-suggestions-per-display-mode-with-custom-module%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      2
      down vote



      accepted










      The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.



      This is how it looks using the hook_theme_suggestions_HOOK():



      /**
      * Implements hook_theme_suggestions_HOOK().
      */
      function mymodule_theme_suggestions_user(array $variables)
      return [
      'user__' . $variables['elements']['#view_mode']
      ];





      share
























        up vote
        2
        down vote



        accepted










        The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.



        This is how it looks using the hook_theme_suggestions_HOOK():



        /**
        * Implements hook_theme_suggestions_HOOK().
        */
        function mymodule_theme_suggestions_user(array $variables)
        return [
        'user__' . $variables['elements']['#view_mode']
        ];





        share






















          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.



          This is how it looks using the hook_theme_suggestions_HOOK():



          /**
          * Implements hook_theme_suggestions_HOOK().
          */
          function mymodule_theme_suggestions_user(array $variables)
          return [
          'user__' . $variables['elements']['#view_mode']
          ];





          share












          The hook_theme_suggestions_HOOK_alter() is more suited to alter existing suggestions.



          This is how it looks using the hook_theme_suggestions_HOOK():



          /**
          * Implements hook_theme_suggestions_HOOK().
          */
          function mymodule_theme_suggestions_user(array $variables)
          return [
          'user__' . $variables['elements']['#view_mode']
          ];






          share











          share


          share










          answered 58 mins ago









          Cesar Moore

          749314




          749314






















              up vote
              2
              down vote













              The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_user_alter(), as well as switching one template from user to user__full. Below is the updated module code:



              function module_name_theme_suggestions_user_alter(&$suggestions, $variables, $hook) 

              $mode = $variables['elements']['#view_mode'];
              $suggestions = 'user__' . $mode;



              function module_name_theme($existing, $type, $theme, $path)

              return [
              'user__full' => [
              'template' => 'user--full',
              'base hook' => 'user',
              ],
              'user__compact' => [
              'template' => 'user--compact',
              'base hook' => 'user',
              ],
              'user__token' => [
              'template' => 'user--token',
              'base hook' => 'user',
              ],
              ];







              share|improve this answer


























                up vote
                2
                down vote













                The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_user_alter(), as well as switching one template from user to user__full. Below is the updated module code:



                function module_name_theme_suggestions_user_alter(&$suggestions, $variables, $hook) 

                $mode = $variables['elements']['#view_mode'];
                $suggestions = 'user__' . $mode;



                function module_name_theme($existing, $type, $theme, $path)

                return [
                'user__full' => [
                'template' => 'user--full',
                'base hook' => 'user',
                ],
                'user__compact' => [
                'template' => 'user--compact',
                'base hook' => 'user',
                ],
                'user__token' => [
                'template' => 'user--token',
                'base hook' => 'user',
                ],
                ];







                share|improve this answer
























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_user_alter(), as well as switching one template from user to user__full. Below is the updated module code:



                  function module_name_theme_suggestions_user_alter(&$suggestions, $variables, $hook) 

                  $mode = $variables['elements']['#view_mode'];
                  $suggestions = 'user__' . $mode;



                  function module_name_theme($existing, $type, $theme, $path)

                  return [
                  'user__full' => [
                  'template' => 'user--full',
                  'base hook' => 'user',
                  ],
                  'user__compact' => [
                  'template' => 'user--compact',
                  'base hook' => 'user',
                  ],
                  'user__token' => [
                  'template' => 'user--token',
                  'base hook' => 'user',
                  ],
                  ];







                  share|improve this answer














                  The fix was ensuring to add the theme suggestions via HOOK_theme_suggestions_user_alter(), as well as switching one template from user to user__full. Below is the updated module code:



                  function module_name_theme_suggestions_user_alter(&$suggestions, $variables, $hook) 

                  $mode = $variables['elements']['#view_mode'];
                  $suggestions = 'user__' . $mode;



                  function module_name_theme($existing, $type, $theme, $path)

                  return [
                  'user__full' => [
                  'template' => 'user--full',
                  'base hook' => 'user',
                  ],
                  'user__compact' => [
                  'template' => 'user--compact',
                  'base hook' => 'user',
                  ],
                  'user__token' => [
                  'template' => 'user--token',
                  'base hook' => 'user',
                  ],
                  ];








                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited 23 mins ago

























                  answered 2 hours ago









                  Prestosaurus

                  3989




                  3989



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdrupal.stackexchange.com%2fquestions%2f269782%2fhow-to-add-twig-template-suggestions-per-display-mode-with-custom-module%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery