2

Sorry for ask this easy question, I know the net its full of tutorials, but the info is a bit confusing since they are always showing the diference between D7 and D8 (i didn't use a CMS before so it is not helpfull enough)

My "news" custom content displays itself without classes, with the title of every field in the content and I want to modify the creation template. Am I allowed to do that? or its supposed to reach this goal through other way?.

Thanks in advance

2
  • 1
    Which Drupal version are you using?
    – No Sssweat
    Jul 28, 2016 at 9:25
  • @NoSssweat D8 ofc
    – Jnewbie
    Jul 28, 2016 at 9:29

2 Answers 2

2

I suppose that you want to create custom template for the content type. To do this create a file called "node--content-type-name.html.twig". And save this file in your theme's template directory. Then in this template file define the html structure. Type the field name which you want to display in your template like {{ content.field_name }}.

To find the field name just goto content type -> manage fields, there you will find the machine name of that particular field.

P.S : This solution is for Drupal 8

3
  • Thank you Sachin, but to clear up things. It can be for example: <div class="{{content.news-header}}"> &nbsp; </div>
    – Jnewbie
    Jul 28, 2016 at 9:59
  • Guess its more like <div class="my-class"> {{content.news-header}}</div>
    – Jnewbie
    Jul 28, 2016 at 10:15
  • 1
    Yes, it'll be like <div class = "class-name"> {{ content.field_name }} </div>
    – Sachin
    Jul 28, 2016 at 11:32
2

It's all in here Twig Template naming conventions

Nodes
Pattern: node--[type|nodeid]--[viewmode].html.twig
Base template: node.html.twig (base location: core/modules/node/templates/node.html.twig)

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

1. node--nodeid--viewmode.html.twig
2. node--nodeid.html.twig
3. node--type--viewmode.html.twig
4. node--type.html.twig
5. node--viewmode.html.twig
6. node.html.twig
Note that underscores in a content type's machine name are replaced by hyphens.

The higher the number the less specific, For example, the last one (6) it affects everything.

While (1) affects a specific node with a specific view mode, Ex: node--1--teaser.html.twig will only affect /node/1 with teaser view.

For your case, you want to use (4), so in your case, its node--news.html.twig

0

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.