Written by: Keith | IANR Media
These instructions are for creating and displaying news content on your site. To pull content from another site like IANR News, please see the feeds module documentation on the Web Developer Network site.
- Step #1: Make a ‘News’ content type
- Step #2: Make a view block
- Step #3: Add your block to a page
- Step #4: Create your twig templates
Step #1: Make a ‘News’ content type
- Go to Structure -> Content Types -> Add content type
- Fill out what fields you need in the ‘Manage Fields’ tab
- Under the ‘Manage Display’ tab, in “Custom Display Setting”, select ‘Teaser’.
- Under ‘Teaser’ in the upper right, move fields out of the ‘Hidden’ area and set their label to ‘Hidden’.
- Later, we’ll set up a view using the News content type’s ‘Teaser’ display. This step is important for that portion.
Step #2: Make a view block
- Go to Structure -> Views -> Add new view
- Give the view a name, deselect ‘Create a page’ and select ‘create a block’. Select ‘Continue & edit’.
- You can make a page if you like. I find blocks are easier to work with because they can be put on other pages.
- Delete the ‘Title’, and add a ‘Block name’.
- Under ‘Format’, we’ll want to update ‘Show’ to ‘Teaser’.
- In ‘Filter Criteria’, we can set the view to only show ‘News’ content types.
- Under ‘Pager’, we can choose how many items to show.
- As you adjust, you’ll be able to see the items being pulled in by the view under the edit area.
- Under the ‘Advanced’ dropdown and ‘Theme: Information’, the last entry in ‘Style output’ will be used for the Twig template later on. It should look like ‘views-view-unformatted--news--block.tpl.php’
Step #3: Add your block to a page
- I usually set up a test page to add a new block to and adjust before moving it to its final destination.
- Under Structure -> Blocks, find the block by the ‘Block name’ and configure.
- On the configure page, select where on the page the block will be displayed with the ‘UNLedu 5.2’ dropdown in ‘Region settings’
- Under the visibility settings, select ‘Only the listed pages’ and add the end of the URL to the field below. (this field can also take node numbers, like ‘node/184’)
Step #4: Create your twig templates
- Twig templates are under Structure -> Twig Templates.
- I usually have a ‘field’ template that renders the items raw. You can also make a template for individual fields using the ‘Machine Name’ of the field in the content type, for example ‘field__field_image’:
- Your block needs two twig templates:
- The News’ teaser
- This will be titled similar to ‘node__news__teaser’
- This determines how each individual news item looks in the view
- The View’s template
- The title will be what we had in the Theme Information for the view, just replacing the hyphens with underscores: views_view_unformatted__news__block
- This will control how each item is laid out in the view. Generally, the grid goes here.
- The News’ teaser
- In Twig templates, you’ll use the ‘Machine name’ of each field of the content type to map fields to the template.