Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By fonea on
To do that in our (custom) theme folder in the .theme file we have to add the following hook function: hook_theme_suggestions_HOOK_alter like below:
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function THEME_NAME_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::routeMatch()->getParameter('node')) {
$content_type = $node->bundle();
$suggestions[] = 'page__'.$content_type;
}
}
Thanks!
Comments
Thank you very much!
This was exactly the thing I was looking for. Copy paste and it worked. Thank you
Thank you!
Thank you!
Thanks a lot
Thanks a lot
I use a different method,
I use a different method, however the main difference with my code is that I use array_splice to place the suggestion one above page--node, this preserves the capability to override with page--node--% and page--node--[id]. Your suggestion will always take precedence because you have pushed it onto the array.
If I put them both in you can see this in the suggestions list:
Pimp your Drupal 8 Toolbar - make it badass.
Adaptivetheme - theming system for people who don't code.
Perfect! Thank you!
Perfect! Thank you!
Thank you.. Its really nice..
Thank you.. Its really nice..
Worked for me, Thank You
Worked for me, Thank You
Thanks Jeff Burnz for this
Thanks Jeff Burnz for this code. I have been using it for some time now. This code works to render suggestion, however it breaks viewing revisions. When trying to view revisions of content I am getting errors like this on the development site:
Error: Call to a member function getType() on string in themename_theme_suggestions_page_alter() (line 47 of themes/custom/themename/themename.theme).
Here are lines 40-49 from themename.theme:
I have modified the code to this using a suggestion from a comment on code to check that revisions wasn't present in the URL. With this code it is possible to view revision and template suggestions are presented only on the current version of the content.
Frederick
Thank you
very very very very very thank you for this code, its working like a charm !
i've tried to figure out how to make it works and with this it works just fine :D
don't use strpos with only two equal signs
I wouldn't recommend using the php strpos function with only two equal signs (==) in this case. As the PHP documentation mentions, "Note our use of ===. Simply == would not work as expected because the position of 'a' was the 0th (first) character.". A strict checking of values is required when checking the strpos return value as this function returns FALSE when not found but the actual position when it IS found. See the manual here.
Working nicely with this modification
Worked for me too.
You saved me
Thank you so much.
4 years later...
4 years later and this still works. Viewing revisions caused my site to crash but with this little bit of code everything works like a dream. THANK YOU.
Working but a glitches
Hi, The Page view is coming but Page title is coming as Page not found and it is showing page view source as
Page not found | WEBSITE_NAME
The page content is coming fine and problem is only with title.
Please assist.
Thank you .
Thank you very much ,,it's worked with me .
It works perfectly! Thank you
It works perfectly! Thank you!
Thank you!
Thank you!
Thank you!
Thank you!
how to overridinde node--(type).html.twig
Hi everyone, I'm new with drupal 8 and i would like to know how to overridinde node--(type).html.twig.
whene im using the Available variables like:
{{ label }} or {{ content.field_example }} i have an additional html conent that i dont need, i juste want the value.
someone could help me please.
thanks!
I couldn't get this to work on my D8
Though I'm new to drupal, I couldn't get this to work. Is there anything I'm doing wrong?
yeah me also.. been doing
yeah me also.. been doing this for 3 months to the point i felt im really just a stupid :<
hope you find answer and able to help me... this is my problem
I was able to figure it out
save as "theme_name.theme" that should do it
Thanks! It worked.
Though I spent time trying to figure it out. My major mistake was I was saving it as theme_name.theme.php (initially as theme_name.theme.inc). Thanks all d same
Great solution
+1 from me. It works like a charm.
the page doesn't load jquery
when i'm using this method, somehow the page doesn't load javascript from core
Can't seem to get this to work.
This is my first time messing with templates in general, and based on the comments above it's probably me doing something wrong.
First I created a content type. "adventures" the I created a template for it. "page--adventures.html.twig". After creating both I added the following to my .theme file:
/**
* Implements hook_theme_suggestions_HOOK_alter().
*/
function bootstrap_subtheme_theme_suggestions_page_alter(array &$suggestions, array $variables) {
if ($node = \Drupal::routeMatch()->getParameter('adventures')) {
$content_type = $node->bundle();
$suggestions[] = 'page__adventures'.$content_type;
}
}
Nothing seems to change. The content type still used the original template. Any suggestions? Thanks in advance! It's been driving me insane.
Delete "adventures" from
Delete "adventures" from
$suggestions[] = 'page__adventures'.$content_type;
Try to change your code in to
Try to change your code in to
Thanks and Regards
ARUN AK
www.developersdocs.com
Got this to work with revisions
I noticed that in 8.3 the node id and revision id are both strings if you're viewing a revision. In order to get the full entity so I could get the bundle value I needed. This was how the code evolved. I'm sure there are better ways but this got me there.
That worked for me, thanks!
That worked for me, thanks!
node edit form template
Hi, i am new to drupal8.
I am wondering that how to add node edit form template.
used theme suggestions alter function for node edit template
create twig templates in your theme's template directory in the form of node-edit-form--NODE-TYPE-SEPARATED-WITH-DASHES.html.twig.
Thank you
It works great.
Thanks!
Thanks!
In what file should I add the
In what file should I add the code? and should the functions be called somewhere?
I have an
THEME_NAME.theme.yml
THEME_NAME.module
??
// Thomas
@Thomashdk add the code in
@Thomashdk add the code in your: THEME_NAME.theme and refresh the cache. The function should be call automatically.
Thanks for your reply
When I have the code in my THEME_NAME.theme (cabana.theme) I only get this
https://codeshare.io/thomashdk ( it's the markup)
ok, which is the name of the
ok, which is the name of the Content type, and the page--(content type name).html.twig that you are using? @Thomashdk
How about for html.twig?
And I want to add below twig templates based on content type, How to ?
1. html.twig
2. html--article.html.twig
3. html--blog.html.twig
...
If you use the same function
If you use the same function but change *page_alter* to *html_alter*, it should work. i.e.:
-----
BeFused - Drupal tutorials
Page suggestions
Dear all, I am newer to drupal and doing my first website.
I enabled twig debug but its not showing correct page suggestions for basic pages.
It is showing page suggestions with page title for my event page.(I used views to create this page)
But its showing page suggestions without title for services page (its a basic page)
Any one can help me?
I can't seem to get it to
I can't seem to get it to work. I'm using
function mytheme_theme_suggestions_block_alter(array &$suggestions, array $variables) {
$suggestions[] = 'wide_grid_block';
}
and wide_grid_block.html.twig exists in the templates folder and even appears in the list of suggested files in debug but it's not used.
How to print the value of custom fields in the contentType
Hi, I have used this above code for Implements hook_theme_suggestions_HOOK_alter(). After that I was trying to add the field value through twig like {content.field_product_price} on the page--mypage.html.twig, its not giving the output. Please advise me how to get the custom field value. Even I have added another field(type is image/file) i.e. field_prod_image, then How will I get the image link ???
Thanks inadvance :-)
You can access content type
You can access content type fields in page templates using the node object. So for field_product_price, it should be:
-----
BeFused - Drupal tutorials
I have tried, not working
Its not Working. Infact I have used {node.field_product_price.value}, but its printing the raw output including html tag.
Template Suggestion for a form in a custom module
Hi, similar approach but I need it for a custom form that is build in a custom module. There's no content-type created just a multi-step form in a custom module. I need to make a custom twig template for the form and the fields. How can I do that?
My custom module name is: nemo and custom theme name is: nemo_theme
Thanks.
The best way to create suggestion for preview and node
There is another possibility
I came across this issue recently, and upon doing some digging, found another solution, thanks to BeFused at this link:
This works for me
Thanks karolus!! This works for me. I also appreciate the link to BeFused and their explanation on how this works.
Is there any way to create a
Is there any way to create a theme suggestion for page.html.twig for custom view modes?
I have exactly the same
I have exactly the same question.
I was able to do this using
I was able to do this using the Context module.
I created a new context with:
Hope that helps others.
When you render a page it is
When you render a page it is always the "full" view mode. Or do I miss your point?
Display Suite module allows
Display Suite module allows to select View mode for node page:
https://www.drupal.org/project/ds
I have added my code
function sphynx_theme_suggestions_node_alter(array &$suggestions, array $variables) {
$suggestions[] = 'node__helloo';
}
twig template name will be node--helloo.html.twig