How to add twig template suggestions per display mode with custom module?
Clash 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',
],
];
8 theming hooks
add a comment |Â
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',
],
];
8 theming hooks
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 Idpm($existing);
the only base hooks arefield
,field_multiple_value_form
, andblock
â Prestosaurus
7 hours ago
add a comment |Â
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',
],
];
8 theming hooks
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
8 theming hooks
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 Idpm($existing);
the only base hooks arefield
,field_multiple_value_form
, andblock
â Prestosaurus
7 hours ago
add a comment |Â
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 Idpm($existing);
the only base hooks arefield
,field_multiple_value_form
, andblock
â 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
add a comment |Â
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']
];
add a comment |Â
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',
],
];
add a comment |Â
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']
];
add a comment |Â
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']
];
add a comment |Â
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']
];
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']
];
answered 58 mins ago
Cesar Moore
749314
749314
add a comment |Â
add a comment |Â
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',
],
];
add a comment |Â
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',
],
];
add a comment |Â
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',
],
];
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',
],
];
edited 23 mins ago
answered 2 hours ago
Prestosaurus
3989
3989
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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 arefield
,field_multiple_value_form
, andblock
â Prestosaurus
7 hours ago