Drupal 9
Probably not the best title, but I've created a content type and don't want it to display anything to the public. It needs to be published and to have an 'edit' page but I don't need/want it to be viewed by the public or search engines. For example, let's say I created a content type called 'slide…
Sometimes it's handy to have the node id in the <body> tag so you can add some custom styling to a specific page. The result would look like this (if the node id was 123).
<body class="page-node-123">
There will be other theme specific classes so we need to add ours rather than…
One of the best tools within Drupal is the ability to create different content types, with different fields, different display settings and also.... the ability to have a different template for each content type. If the theme you are using doesn't automatically create the 'template suggestion' for…
This is probably an unusual scenario, but let me explain where I am.... I have a custom module that processes a feed from an external source and generates content (creates nodes). For some reason <br /> tags are coming through 'converted' rather than as raw HTML. In Drupal 7 I used PHP…
views-view-unformatted--testimonials--block-2.html.twig
{% for row in rows %}
{% set row_classes = cycle(['views-row odd', 'views-row even'], loop.index0) %}
<div{{ row.attributes.addClass(row_classes) }}>
{{- row.content -}}
</div>
{% endfor %}
views-view-unformatted--home-news.html.twig
Within {% for row in rows %} we can use {{ loop.index }} like: <article id="newsitem{{ loop.index }}">.
Full output might look like:
{% for row in rows %}
<article id="newsitem{{ loop.index }}" class="newsitem col-md-4 col-lg-4 col-sm-6 col-xs…
Drupal 7 has a really handy list of view templates... which is missing in D8/D9.... it doesn't even show up in the template suggestions that show if theme debug is switched on... which is not so handy! So let me see if I can explain how this works. I'm using 'Stable' as a base theme and the initial…
In D7 the copyright line in a tpl.php file might look like this:
<p>© Copyright <?php echo date('Y'); ?> Company Name</p>
In D8/9 the TWIG template needs to be like this:
<p>© Copyright {{ currentYear|raw }} Company Name</p>
page.html.twig
{% include directory ~ '/templates/region--footer.html.twig' %}
So this will start att he root of the theme folder (/themes/custom/mytheme), and then inside the 'templates' folder (/themes/custom/mytheme/templates), for the file 'region--footer.html.twig'.
D7 to D8/9 Differences
D7: print render($page['help']);
D8/9: {{ page.help }}
D7:
<?php if($page['sidebar_first']){ ?>
<div class="sidebar-first">
<?php print render($page['sidebar_first']); ?>
</div>
<?php } ?>
D8/9:
{% if page.…