1

I'm building a custom theme and I would like to display last three inserted news (i.e. nodes with content type = News) on the bottom of the home page from inside the home page template.

Something like this:

+-------------------------------+
| +---------------------------+ |
| |                           | |
| |          Slides           | |
| |                           | |
| +---------------------------+ |
|                               |
| +---------------------------+ |
| |                           | |
| |           Page            | |
| |           Body            | |
| |                           | |
| |                           | |
| +---------------------------+ |
|                               |
| +-------+ +-------+ +-------+ |
| | Last  | |Second | | Third | |
| | News  | | Last  | | Last  | |
| |       | | News  | | News  | |
| +-------+ +-------+ +-------+ |
+-------------------------------+

I created a content type named Home page, with fields for manage slides and the page body, then I created a new content (a new node) of type Home page.

Inside my custom theme I created a template file named node--home-page.html.twig and inside it I can handle the Home page's fields (slides and body) and how to render them inside the page.

After that I created another content type News and I added some content of such type.

Now I would like to display latest news inside the homepage template as in the figure above. What is the better/simplest way to do it?

I'm thinking to load latest news in some hook_preprocess_HOOK function (using the EntityFieldQuery object) then add them inside a variable for the Twig template. Inside the template I will perform a for loop rendering latest news at the page's bottom. Is this a good way to do it or I'm completely out of the way?


EDIT

Correction: I would like to display latest news between slides and body, in this way:

+-------------------------------+
| +---------------------------+ |
| |                           | |
| |          Slides           | |
| |                           | |
| +---------------------------+ |
|                               |
| +-------+ +-------+ +-------+ |
| | Last  | |Second | | Third | |
| | News  | | Last  | | Last  | |
| |       | | News  | | News  | |
| +-------+ +-------+ +-------+ |
|                               |
| +---------------------------+ |
| |                           | |
| |           Page            | |
| |           Body            | |
| |                           | |
| |                           | |
| +---------------------------+ |
+-------------------------------+

Where slides and body are two fields of the Home page content type.

8
  • Have you thought about including a view in config/install with a block display, and a grid style that is positioned in your custom region?
    – mradcliffe
    Commented Apr 19, 2016 at 1:01
  • @mradcliffe Thank you for your suggestion. No, I didn't thought about views and blocks. I would like to keep it as simple as possible. Do you think it is a bad approach fetching latest news from a preprocess function? Otherwise, do you have a reference with an example where I could learn more about how to do it with a view?
    – Andrea
    Commented Apr 19, 2016 at 10:40
  • It doesn't get much easier than a View providing a block display, and putting the block in that region set for the front page only (or alternatively, that content type).
    – Kevin
    Commented Apr 19, 2016 at 18:42
  • Thank you @Kevin. I'm trying to do it with Views and Blocks now. But I need to display the view's content (latest news) in the middle of the Home page's content (see my last edit on the question), between two fields of the Home page node. Do you have any idea on how I could do it inside a custom template?
    – Andrea
    Commented Apr 20, 2016 at 16:49
  • It is impractical to put regions into a node template. You might want to try creating a reference field for that block/view and theme the result. This is something Paragraphs was designed for to excel at doing. Another way to do this would be to make a Content Top region and Content Main and Content Bottom, that way you can put blocks above or below the main content output.
    – Kevin
    Commented Apr 20, 2016 at 17:22

1 Answer 1

1

I used the Twig Tweak module and I embedded a custom view, showing latest news, inside my Twig template with:

{{ drupal_view('view_name') }}
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.